Chapter 5-CPU Scheduling
Chapter 5-CPU Scheduling
CPU Scheduling
CPU- and I/O-bound processes
• Long term scheduler should select a good mix of I/O bound and CPU bound
processes.
• I/O bound -> ready queue almost empty, CPU bound -> I/O wait queue empty (wasting
resources)
Remember: Diagram of
Process State
CPU Scheduler
• Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them
• Non- preemptive
Non-preemptive scheduling is an act of never initiating a context
switch from a running process to another process. In this case the
task can self-interrupt and voluntarily give control to other tasks.
When non preemptive is used, a process that receives such
resources can not be interrupted until it is finished.
In non preemptive we allow the current process to finish its CPU burst time.
Scheduling Criteria
Scheduling Criteria
Scheduling Algorithm
Optimization Criteria
Scheduling Algorithms
Scheduling Algorithms
1. FCFS Scheduling
Convoy Effect
P1 P2 P3
0 24 27 30
0 3 6 30
• 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 know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time for a given set
of processes
– The difficulty is knowing the length of the next CPU request.
Example of SJF (non-
preemptive)
ProcessArrival TimeBurst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• SJF scheduling chart
P4 P1 P3 P2
0 3 9 16 24
0 3 7 8 12 16
0 2 4 5 7 11 16
• FCFS
• SJF
• SRTF
• RR with Q=3
• Preemptive Priority (A higher number indicates a greater priority)
• The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
– Preemptive – can preempt the CPU if the priority of the newly
arrived process is higher than the priority of the currently running
process.
– No-npreemptive – simply put the new process at the head of the
ready queue.
• Priority Scheduling
P2 P5 P1 P3 P4
0 1 6 16 18 19
• 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 FCFS / FIFO
– q small q must be large with respect to context switch, otherwise
overhead is too high
Example of RR with Time
Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Example of RR with Time
Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
• Time quantum should be large compared with context switch time, but
not too large else >> FCFS / FIFO
P P4 P1 P3
Best FCFS: [8]2 [24] [53] [68]
0 8 32 85 153
P3 P1 P4 P2
Worst FCFS: [68] [53] [24] [8]
0 68 121 145 153
Quantum P1 P2 P3 P4 Average
Best FCFS 32 0 85 8 31¼
Q=1 84 22 85 57 62
Q=5 82 20 85 58 61¼
Wait
Q=8 80 8 85 56 57¼
Time
Q = 10 82 10 85 68 61¼
Q = 20 72 20 85 88 66¼
Worst FCFS 68 145 0 121 83½
Best FCFS 85 8 153 32 69½
Q=1 137 30 153 81 100½
Q=5 135 28 153 82 99½
Completion
Q=8 133 16 153 80 95½
Time
Q = 10 135 18 153 92 99½
Q = 20 125 28 153 112 104½
Worst FCFS 121 153 68 145 121¾
Highest Response Ratio Next
• Choose next process with the greatest ratio
• Scheduling
– A new job enters queue Q0 which is served FCFS. 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 FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted and
moved to queue Q2.
Multilevel Feedback Queues
Question
• On a system using multilevel feedback
queues, a totally CPU-bound process
requires 40 seconds to execute. If the first
queue uses a time quantum of 2 and at
each level the time quantum increases by
5 time-units, how many times will the job
be interrupted and on what queue will it be
when it terminates?
Question
• On a system using multilevel feedback
queues, a totally CPU-bound process
requires 50 seconds to execute. If the first
queue uses a time quantum of 5, and at
each lower level the time quantum
doubles, how many times will the job be
interrupted and on what queue will it be
when it terminates?
Question
Process Arrival time Burst Time
1 0 4
2 3 4
3 4 3
4 9 2
Question
• Imagine we have a multi-level feedback queue
with 2 queues. The highest priority queue is a
RR scheduler with a quantum of 2. The second
priority queue runs as a FCFS queue. Processes
start in the RR queue and are demoted to the
FCFS queue if they exceed their quantum.
Processes in the RR queue are always
prioritized over the FCFS queue. Calculate the
average waiting time by using the multi level
feed back queue and draw the Gantt chart
Determining Length of Next CPU
Burst
• Can only estimate the length
• Can be done by using the length of
previous CPU bursts, using exponential
averaging
n 1 t n 1 n .