Chapter6 CPU Scheduling
Chapter6 CPU Scheduling
Chapter 6
Outline
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling(opt)
Operating Systems Examples(Linux)
Algorithm Evaluation
Basic Concepts
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one
of them
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this
involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
• Waiting time for P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 = 3 ms
• Much better than previous case
• Convoy effect: short process behind long process
Shortest-Job-First (SJF) Scheduling
P4 P1 P3 P2
0 3 9 16 24
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms
Determining Length of Next CPU Burst
Let n+1 denote the predicted value for the next CPU burst
Define to be:
0 <= <= 1
= t + (1 - )
Prediction of the Length of the Next CPU
Burst
Example
T0 = 10 ms
Measured CPU bursts: t0 = 8ms, t1=16ms, t2=20ms, t3=10ms
Assume = ½
T1= ½ x 8 + ½ x 10 = 9
T2 = ½ x 16 + ½ x 9 = 12.5
T3 = ½ x 20 + ½ x 12.5 = 16.25
T4 = ½ x 10 + ½ x 16.25 = 13.125
0 1 5 10 17 26
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.
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
Example of RR with Time Quantum = 4
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
A process can move between the various queues; aging can be implemented this way
Multilevel-feedback-queue scheduler defined by the following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter when that process needs service
Example of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served RR (q=8). When it gains CPU, job receives 8
milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served RR and receives 16 additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q2.
Multilevel Feedback Queues