Sei sulla pagina 1di 40

CPU Scheduling

Source: Operating System Concepts by by Silberschatz, Galvin and Gagne

CSC 4103: Operating System 6.1 BB Karki, LSU


CPU Scheduling

 CPU scheduling is fundamental to multiprogramming OS


 Deals with problem of deciding which of processes in the
ready queue is to be allocated the CPU.

 What we cover:
 Basic concepts
 Scheduling Criteria
 Scheduling Algorithms
 Multiple-Processor Scheduling
 Real-Time Scheduling
 Algorithm Evaluation

CSC 4103: Operating System 6.2 BB Karki, LSU


CPU-I/O Burst Cycle

 Process execution consists of


a cycle of CPU execution and
I/O wait.

 Process alternates between


these two states.

 Process always begins and


ends with a CPU burst.

CSC 4103: Operating System 6.3 BB Karki, LSU


Histogram of CPU-Burst Times
 Durations of CPU bursts has a typical frequency curve (exponential
or hyper-exponential):
 many short bursts – an I/O bound program
 a few long bursts – a CPU bound program.

CSC 4103: Operating System 6.4 BB Karki, LSU


CPU Scheduler

 CPU scheduler (also called short-term scheduler) selects from among


the processes in ready queue, and allocates the CPU to one of them.

 CPU scheduling decision may take place when a process:


1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.

 Scheduling under 1 and 4 is nonpreemptive.


 Once the CPU is allocated to a process, the process keeps the CPU until
it switches to waiting state or terminates.

 All other scheduling is preemptive.


 Incurs a cost.

CSC 4103: Operating System 6.5 BB Karki, LSU


Dispatcher

 Dispatcher gives control of the CPU to the scheduled


process. This involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart
that program

 Dispatch latency – time the dispatcher takes to stop one


process and start another running.

CSC 4103: Operating System 6.6 BB Karki, LSU


Scheduling Criteria

Criteria for comparing CPU-scheduling algorithms:

 CPU utilization – keep the CPU as busy as possible (0 to 100%


efficiency).

 Throughput – # of processes that complete their execution per time


unit

 Turnaround time – amount of time to execute a particular process


 waiting for memory + ready queue + execution + I/O

 Waiting time – amount of time a process waits in the ready queue

 Response time – amount of time it takes from when a request was


submitted until the first response is produced.

CSC 4103: Operating System 6.7 BB Karki, LSU


Optimization Criteria

 Max CPU utilization

 Max throughput

 Min turnaround time

 Min waiting time

 Min response time

 Optimize the max or min values or average measure or


variance.

CSC 4103: Operating System 6.8 BB Karki, LSU


First-Come, First-Served (FCFS) Scheduling

 FCFS is the simplest CPU-scheduling algorithm.


 The process that requests the CPU first is allocated the CPU first.

 Implemented with a FIFO queue


 PCB of a new process is linked onto the tail of the ready queue.
 Process at the head of the queue gets CPU first.

 Average waiting time varies a lot and is quite long.

 FCFS is non-preemptive.

CSC 4103: Operating System 6.9 BB Karki, LSU


FCFS Scheduling (Contd.)

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

 Average waiting time: (0 + 24 + 27)/3 = 17

CSC 4103: Operating System 6.10 BB Karki, LSU


FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order


P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

 Average waiting time: (6 + 0 + 3)/3 = 3


 Much better than previous case.

 Convoy effect - short process behind long process

CSC 4103: Operating System 6.11 BB Karki, LSU


Shortest-Job-First (SJF) Scheduling

 Associate with each process the length of Process BurstTime


its next CPU burst. Use these lengths to
schedule the process with the shortest P1 6
time first. P2 8
P3 7
 SJF is optimal – gives minimum average
waiting time for a given set of processes. P4 3

Average waiting time is


(3 + 16 + 9 + 0)/4 = 7
ms
P4 P1 P3 P2
But with FCFS scheme,
0 3 9 16 24 average waiting time
would be 10.25 ms

CSC 4103: Operating System 6.12 BB Karki, LSU


Determining Length of Next CPU Burst

 Challenge is to know the length of the next CPU request:

 Approximate prediction of the length of next CPU burst:


 from the lengths of previous CPU bursts by using
exponential averaging
 running average of each burst for each process.

1. t n = actual (observed) length of n th CPU burst


2. τ n +1 = predicted value for the next CPU burst
3. α , 0 ≤ α ≤ 1
4. Define :
τ n +1 = α t n + (1− α )τ n .
CSC 4103: Operating System 6.13 BB Karki, LSU
Examples of Exponential Averaging

α =0
τn+1 = τn
Recent history does not count.
α =1
τn+1 = tn
Only the actual last CPU burst counts.

If we expand the formula, we get:


τn+1 = α tn+(1 - α) α tn-1 + …
+ (1 - α )j α tn-j + …
+ (1 - α )n+1 τ0

Since both α and (1 - α) are less than or equal to 1, each successive


term has less weight than its predecessor.

CSC 4103: Operating System 6.14 BB Karki, LSU


Prediction of the Length of the Next CPU Burst

CSC 4103: Operating System 6.15 BB Karki, LSU


SJF Scheduling (Contd.)

 Two schemes:
 nonpreemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst.
 preemptive – if a new process arrives with CPU burst length less
than remaining time of current executing process, preempt. This
scheme is known as the Shortest-Remaining-Time-First (SRTF).

 Preemptive improves average waiting time.

CSC 4103: Operating System 6.16 BB Karki, LSU


Example of Non-Preemptive SJF

Process Arrival Time Burst Time


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

 SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 milliseconds.

CSC 4103: Operating System 6.17 BB Karki, LSU


Example of Preemptive SJF

Process Arrival Time Burst Time


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 Average waiting time = (9 + 1 + 0 +2)/4 = 3 milliseconds.

 Preemptive SJF suffers from starvation

 Highest response ratio next (HRRN) scheduling: R = (w + e) / e.


 As time elapses, the ratio increases so that a longer process will
eventually get past competing shorter processes.

CSC 4103: Operating System 6.18 BB Karki, LSU


Priority Scheduling

 A priority is associated with


each process, and the CPU is Process Burst Time Priority
allocated to the process with
the highest priority. P1 10 3
 smallest integer ≡ highest
priority. P2 1 1

 SJF is a priority scheduling


P3 2 4
where priority is the inverse of P4 1 5
the predicted next CPU burst
time. P5 5 2

Average waiting time is 8.2 ms

P2 P5 P1 P3 P4

0 1 6 16 18 19
CSC 4103: Operating System 6.19 BB Karki, LSU
Priority Scheduling

 Priorities can be defined internally or externally

 Priority scheduling can be preemptive or non-preemptive

 Problem ≡ Starvation
Low priority processes may never be executed.
e.g., a low-priority process submitted in 1967 never ran until
the shutdown of IBM 7094 at MIT in 1973.

 Solution ≡ Aging
As time progresses, increase the priority of the process.

CSC 4103: Operating System 6.20 BB Karki, LSU


Round Robin (RR)

 Each process gets a small unit of CPU time (time quantum)


 Usually 10-100 milliseconds.
 After this time has elapsed, the process is preempted and added to
the end of the ready queue.
 Ready queue treated as a circular queue.

 If there are n processes in the ready queue and the time


quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once.
 No process waits more than (n-1)q time units.

 Performance
 q large ⇒ FIFO
q small ⇒ q must be large with respect to context switch, otherwise
overhead is too high.

CSC 4103: Operating System 6.21 BB Karki, LSU


Example of RR with Time Quantum = 20

Process Burst Time


P1 53
P2 17
P3 68
P4 24

 The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

 Typically, higher average turnaround than SJF, but better response.


 Useful in time sharing systems.

CSC 4103: Operating System 6.22 BB Karki, LSU


Time Quantum and Context Switch Time

Time quantum needs to be large enough with respect to the context switch time.
Context switch increases the waiting and turnaround time.

CSC 4103: Operating System 6.23 BB Karki, LSU


Turnaround Time Varies With The Time Quantum

CSC 4103: Operating System 6.24 BB Karki, LSU


Multilevel Queue

 Ready queue is partitioned into separate queues:


foreground (interactive)
background (batch)

 Each queue has its own scheduling algorithm,


foreground – RR
background – FCFS

 Scheduling must be done between the queues.


 Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes;
 i.e., 80% to foreground in RR and 20% to background in FCFS.

CSC 4103: Operating System 6.25 BB Karki, LSU


Multilevel Queue Scheduling

Each queue has absolute priority over lower-priority queues.


CSC 4103: Operating System 6.26 BB Karki, LSU
Multilevel Feedback Queue

 A process can move between the various queues:


 Move a process to a lower priority queue if it uses too much
CPU time.
 Move a process to a higher priority queue if it waits too long -
aging can be implemented this way.

 Multilevel-feedback-queue scheduler defined by the


following parameters:
 number of queues
 scheduling algorithm for each queue
 method used to determine when to upgrade a process to a
higher-priority queue
 method used to determine when to demote a process to a
lower-priority queue
 method used to determine which queue a process will enter
when that process needs service.

CSC 4103: Operating System 6.27 BB Karki, LSU


Example of Multilevel Feedback Queue
 Three queues:
 Q0 – time quantum 8 milliseconds
 Q1 – time quantum 16 milliseconds
 Q2 – FCFS

CSC 4103: Operating System 6.28 BB Karki, LSU


Multiple-Processor Scheduling

 CPU scheduling more complex when multiple CPUs are available.

 Homogeneous processors within a multiprocessor

 Asymmetric multiprocessing – one single processor (called master server)


does all scheduling including I/O processing
 Other processors only execute user codes.

 Symmetric multiprocessing (SMP) - Each processor is self-scheduling


 Provide a separate queue for each processor
 Use a common ready queue
 Two issues: Processor affinity and load balancing

 Symmetric multithreading (SMT) - runs several threads at a time by


providing multiple logical - rather than physical - processors
 Each logical processor has its own architecture state (registers, interrupt
handling) supported in hardware level.

CSC 4103: Operating System 6.29 BB Karki, LSU


Thread Scheduling

 Lightweight process (LWP) maps an user-level thread to an


associated kernel level thread

 Contention scope: competition among threads at two level


 Process-contention scope (PCS): schedules user-level threads
to run on an available LWP
 System-contention scope (SCS): schedules kernel-level
threads on a physical CPU

 Pthread scheduling: POSIX Pthread AIP supports PCS and


SCS scheduling with
 PTHREAD_SCOPE_PROCESS
 PTHREAD_SCOPE_SYSTEM.

CSC 4103: Operating System 6.30 BB Karki, LSU


Real-Time Scheduling

 Hard real-time systems – required to complete a critical


task within a guaranteed amount of time.
 Scheduling should admit or not the process by examining the
process requirements.
 Deadline scheduling.

 Soft real-time computing – uses priority scheduling


 Critical processes receive priority over less fortunate ones.
 Priority should not degrade with time.
 Minimize latency (interrupt and dispatch).
 Make kernel preemptive.

CSC 4103: Operating System 6.31 BB Karki, LSU


Algorithm Evaluation

 Many scheduling algorithms:


 FCFS, SJF, RR

 Define the selection criteria:


 Relative importance of CPU utilization, throughput or response time.
 Maximize CPU utilization under constraint that the maximum response
time is 1 second.
 Maximize throughput such that turnaround time is (on average) linearly
proportional to total execution time.

 Different methods:
 Deterministic modeling
 Queuing models
 Simulations
 Implementation.

CSC 4103: Operating System 6.32 BB Karki, LSU


Deterministic Modeling

 Analytical evaluation of an algorithm:


 Takes a particular predetermined workload.
 Produces a formula or number to define the performance of the
algorithm for that workload

 Process Burst Time


P1 10
P2 29
P3 3
P4 7
P5 12

 Consider FCFS, SJF, and RR (quantum = 10 ms):


which algorithm would give the minimum average waiting time?

CSC 4103: Operating System 6.33 BB Karki, LSU


Queuing Algorithm

 Queuing models
 CPU burst distribution
 Arrival time distribution

 Little’s formula:
average queue length = average arrival rate X
average waiting time in the queue.
 Compute one variable if you know the other two.

 Knowing arrival rates and service rates, one can compute


utilization, average queue length, average wait time.

CSC 4103: Operating System 6.34 BB Karki, LSU


Simulations

 A more accurate evaluation


 Program a model of the computer system – A Simulator.

 OSP – operating system project


 A collection of C modules that together implement an OS.

 As the simulation executes, statistics that indicate algorithm


performance are gathered and printed.

 Data to drive simulation:


 Random-number generator
 Trace tapes – recorded sequence of actual events.

CSC 4103: Operating System 6.35 BB Karki, LSU


Implementation

 Most accurate evaluation


 Code the algorithm, put in the real OS, and see how it
works.

 Too frequent implementation is inconvenient.

CSC 4103: Operating System 6.36 BB Karki, LSU


Solaris Scheduling

Priority-based
scheduling
Algorithm

Interactive and time


sharing scheduling
use 60 priority
levels

Inverse relationship
between priorities
and time quanta.

CSC 4103: Operating System 6.37 BB Karki, LSU


Windows XP Scheduling

Priority-based, preemptive scheduling algorithm


Dispatcher (scheduler) uses a 32-level priority scheme
Variable class (1 to 15) and real-time class (16 to 31)

Relationship between Windows XP and Win32 API.


CSC 4103: Operating System 6.38 BB Karki, LSU
Linux Scheduling

 Preemptive, priority-based scheduling algorithm

 Priority ranges
 Real-time range (0 to 99)
 Nice value range (100 to 140)

 Priority is proportional to time quantum (slice)

 Priority arrays
 Active array - tasks with time remaining in their time slices
 Expired array - all expired tasks
 Once active array is done, the expired array is changed to the
active array.

CSC 4103: Operating System 6.39 BB Karki, LSU


Summary
 CPU scheduling selects a process from the ready queue and the dispatcher allocates the
CPU to the selected process.

 FCFS scheduling is the simplest approach but it can hurt short processes

 SJF scheduling is optimal, providing shortest average waiting time. It suffers from
problems of predicting the length of the next CPU burst and starvation. SJF can be
preemptive or non-preemptive.

 Priority-scheduling algorithm allocates the CPU to the highest-priority processes.

 RR allocates the CPU to all processes in time slice so it is appropriate for time-sharing
system. RR is always preemptive.

 Multilevel queue algorithms allow different algorithms in different queues (interactive and
background) and also allow processes to move from one queue to another.

 Multiprocessor and real-time scheduling are more challenging.

 Four ways of evaluating scheduling algorithms: Deterministic modeling, queuing models,


simulations and implementation.
CSC 4103: Operating System 6.40 BB Karki, LSU

Potrebbero piacerti anche