5_1__Process_Scheduling
5_1__Process_Scheduling
u l
e d
c h
S
Scheduling
• Processes in different state maintain Queue.
• The different queues are maintained for different
purpose eg.
– Ready Queue : Processes waiting for CPU
– Blocked : processes waiting for I/O to complete
• Transition form a state where queue is
maintained to next state involves decision
making such as
– When to move process from one state to another
– Which process to move
• When transitions occur, OS may be required to
carry out some house keeping activity such as
context switch, Mode switch etc. These activities
are considered as overhead and must be carried
out in efficient manner
What is scheduling ?
• Scheduling is matter of managing queues
to minimize queuing delay and to optimize
performance in queuing environment
P2 P3 P1
0 3 6 30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case.
Convoy effect short process behind long
process
Favors CPU bound processes
FCFS scheduling
• In early systems, FCFS meant one
program
scheduled until done (including I/O)
• Now, means keep CPU until thread blocks
FCFS scheduling
• FCFS is non preemptive : once the CPU
has been allocated to a process, the
process keeps the CPU till it finishes or it
requests the I/O
• It is not suited for time sharing system
• Average wait time is not minimal as it
depends on arrival and CPU burst of
arriving processes
Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest time.
• Two schemes:
– Non preemptive (SPN) – 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.
• Optimal average response time
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 7 8 12 16
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
• α =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.
Priority Scheduling
• A priority number (integer) is associated with
each process
• The CPU is allocated to the process with the
highest priority (smallest integer ≡ highest
priority).
– Preemptive
– Non preemptive
• SJF is a priority scheduling where priority is
the predicted next CPU burst time.
• Problem ≡ Starvation – low priority processes
may never execute.
• Solution ≡ Aging – as time progresses
increase the priority of the process.
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.
• If there are n processes in the ready queue
– Time quantum is q
– 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.
Process Burst Time
P1 53
P2 8
P3 68
P4 24
• The Gantt chart is: Q=20
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 28 48 68 88 108 112 125 145 153
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
35
Highest Response Ratio Next