OS 06 07 Scheduling12
OS 06 07 Scheduling12
CS31202 / CS30002
Why process scheduling: Recap
load store
add store CPU burst
read from file
•
•
•
Characteristics of CPU bursts
• Typically, CPU bursts follow an exponential or hyper-
exponential distribution
• Large number of short CPU bursts
• Small number of large CPU bursts
# short term schedular is part of kernel
Schedulers
• Short-term scheduler (or CPU scheduler) –
• selects which process from the ready queue should be
executed next in CPU
• Sometimes the only scheduler in a system
• Short-term scheduler is invoked frequently (milliseconds),
hence must be fast
• A process terminates
• E.g., exit() call
Non-preemptive scheduling
• Scheduling happens only when
• A processes switches from RUNNING to WAITING
• A process terminates
# preemptive : involuntary
non preemptive: volunatry
Dispatcher
• Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler.
• Functions
• switching context
• switching to user mode
• jumping to the proper location in the user program to
restart that program
# Race condition:
process was in some important operation
and cpu was taken away and when process
resumes again some things may go wrong.
ex: writing to buffer and incrementing (cpu
taken away before incrementing)
# Response time:
time b/w when process comes in ready queue and first time
when it is allocated CPU burst.
Tell that a program is runnning (first time we see the program
running) so more important
Scheduling criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – number of processes that complete their execution
per time unit (on average)
Arrival time 0 0 0
CPU burst 24ms 3ms 3ms
Draw Gantt chart and calculate average waiting time for two
schedules: P1, P2, P3 and P2, P3, P1
Algo 1. First Come First Serve
scheduling (FCFS)
Example 1
Process P1 P2 P3
Arrival time 0 0 0
CPU burst 24ms 3ms 3ms
Draw Gantt chart and calculate average waiting time for two
schedules: P1, P2, P3 and P2, P3, P1 (Ans: 17 ms and 3 ms)
Problems with FCFS
• Average waiting time can be long
• Convoy effect - a process with large CPU burst can delay
several processes with shorter CPU bursts (see previous
example)
Example 2
Process P1 P2 P3 P4 P5
Arrival 0 2ms 3ms 5ms 9ms
time
CPU 3ms 3ms 2ms 5ms 3ms
burst
Example 2
Process P1 P2 P3 P4 P5
Arrival 0 2ms 3ms 5ms 9ms
time
CPU 3ms 3ms 2ms 5ms 3ms
burst
Process P1 P2 P3 P4
Arrival 0 0 0 0
time
CPU burst 6ms 8ms 7ms 3ms
Process P1 P2 P3 P4
Arrival 0 0 0 0
time
CPU burst 6ms 8ms 7ms 3ms
Process P1 P2 P3 P4
Arrival 0 0 0 0
time
CPU burst 6ms 8ms 7ms 3ms
Process P1 P2 P3 P4
Arrival 0 0 0 0
time
CPU burst 6ms 3ms 1ms 7ms
• Q1 – RR with ! = 16ms
• Q2 – FCFS
• Q1 – RR with ! = 16ms
• Q2 – FCFS
• Q1 – RR with ! = 16ms
• Q2 – FCFS
• Q1 – RR with ! = 16ms
• Q2 – FCFS
• A new process enters Q0; when it gains CPU, receives 8 ms CPU time
• If does not finish in 8 ms, process is moved to Q1
• When process in Q1 gains CPU, receives additional 16 ms CPU time
• If it still does not finish, process moved to Q2
Issue with Multi level feedback queue
scheduling
• Long running processes may starve
• Permanent demotion of priority hurts processes that change
their behavior (e.g., lots of computation only at beginning)
• Eventually all long-running processes move to FCFS
Issue with Multi level feedback queue
scheduling
• Long running processes may starve
• Permanent demotion of priority hurts processes that change
their behavior (e.g., lots of computation only at beginning)
• Eventually all long-running processes move to FCFS
• Solution
• Periodic priority boost: all processes moved to high priority
queue
• Priority boost with aging: recompute priority based on
scheduling history of a process
Summary