You are on page 1of 23

Uniprocessor Scheduling

1
0 Priorities
• Scheduler will always choose a process of
higher priority over one of lower priority

• Have multiple ready queues to represent each


level of priority

• Lower-priority may suffer starvation


– To overcome that allow a process to change its
priority based on its age or execution history
2
3
Process Scheduling Example

4
1- First-Come-First-Served
(FCFS)
• Each process joins the Ready queue
• When the current process finishes to execute, the
oldest process in the Ready queue is selected
• A short process may have to wait a very long
time before it can execute
• Favors CPU-bound processes
– I/O processes have to wait until CPU-bound
process completes

5
Simple Gantt Chart

6
Example (FIFO)
Job # 1 2 3 4 5
Jobtime
Arrival # 01 22 43 64 85
Arrival
Finish time
time 30 92 134 186 208
Servicetime
Response time 33 76 94 125 122
Service time 3 6 4 5 2
Wait time 0 1 5 7 10
Average response time = (3+7+9+12+12)/5 = 8.6
Throughput = 5/20 = 1/4
Ave (RespTime/ServTime) = (1+7/6+9/4+12/5+12/2)/5 = 2.564
2- Round-Robin
• Uses preemption based on a clock
• An amount of time is determined that allows each
process to use the processor for that length of time
• Clock interrupt is generated at periodic intervals
• When an interrupt occurs, the currently running
process is placed in the ready queue
– Next ready job is selected
• Known as time slicing

8
Example (Round Robin)
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 43 18
6 17
4 20
5 15
2
Response time 4 16 13 14 7
Service time 3 6 4 5 2
WaitJ1time
Assume
time
J2 J3
time
quantum
J2 1enters
J2J4
enters,
quantum
timeJ2 J5
enters,
expires
J1
time 10
enters,
before
expires
J3
finishes
expires
J2
then
time then
J1
expires
J5
time
J4
J3
time 9J4
expires
J3
time time
expires
J2
time
expires
J5
expires 9expires,
expires 5
finishes
expires
Average
Queue:response
Queue:
Queue:Queue:
J1Queue: time
J2,Queue:
J1Queue:J1Queue:
J1, J2, =Queue:
J3,
J3 (4+16+13+14+7)/5
J2Queue:
J2,
J2
(J2 J3,
Queue:
J4,
J4, J2,
Queue:
was
J3,
J3
Queue:
J2,J2
in J5,
Queue:
J5,
J5, J4
queue)
Queue:
J4,
J4,
J4,
Queue:
J3,
J3,
J3,
J2,
J3
J2,
J2,
J5,
J2
J5,=
J5,
J4,
J5
J4,10.8
J4,
J4
J3,
J3,
J3J2J2
J1 J1 Switch
starts to
continues
Switch J2
Switch
Switch
to J1 Switch
Switch
Switch
to J2
to Switch
J3 to
to Switch
to J3 Switch
J2Switch
J4 Switch
to to
J2 Switch
to
J5 Switch
to
J4 to
J3 to
J2 to
J5 J4

Round
Robin
T=1
Example (Round Robin)
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 53 17
6 13
4 20
5 15
2
Response time 5 15 9 14 7
Service time 3 6 4 5 2
Wait time
J2 enters,
J3 J1 J42J3
then
finishes
enters, J1
J5
enters
then
time
J2 9J2
time
enters expires
time
expires
J4
time
time5 finishes
expires
J3
expires
expires 9 finishes
J5 finishes
J2 5 expires
J4 time
Average response
Queue:
Queue: time
Queue:
J2,Queue:
J1Queue: =Queue:
J1Queue: (5+15+9+14+7)/5
J1,Queue:
J3,
J3,J2
J2,J2
J2,J4, J3,
J4Queue:
J4,
J4,
J3 J5
Queue:
J3,
J3,J5,
Queue:
J5,J5,
J2J2,=J2,
J410
Queue:
J2, J4
Queue:
J4J4 J4
Switch
J1 starts to
Switch
J2 Switch
Switch
J3 to J2
to Switch
J3
continues
continues
J1 to Switch
J2 to Switch
J4 to Switch
J3 to Switch
J5 to J2
J4 to
Continues
J4

Round
Robin
T=2
• Round-Robin
– Process take turns
– Each executes till its time-quantum expires
– Gives relatively reasonable turnaround time
• Better in standard deviation of turnaround time
– Useful when it is hard to estimate the execution time
• No need to know execution time in advance
– Overhead on process switching
• More process switch  more context switch
– I/O bound jobs may not use up their time quantum
• Not fair to I/O bound jobs
12
3- Shortest Process Next
• Nonpreemptive policy
• Process with shortest expected processing time is
selected next
• Short process jumps ahead of longer processes
• Predictability of longer processes is reduced
• If estimated time for process not correct, the operating
system may abort it
• Possibility of starvation for longer processes

13
Example (Shortest Job Next)
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 33 96 15
4 20
5 11
2
Response time 3 7 11 14 3
Service time 3 6 4 5 2
Wait time
Average response time0 =Job1 2Job 5 7finishes 9
finishes
(3+7+11+14+3)/5 = 7.6 1
Job 2Job 3 Job
entersenters
4JobJobs
enters Jobs
3, 4, 53,in4 queue
5 enters in queue
Throughput = 5/20 = 1/4
Buttohas
But has Jobs
finish Job
to Jobs
finish
3,Job Job
54,
1Job
4 both
3, 3allisshortest
isqueued
52the inthe shortest
queue
Ave (RespTime/ServTime) = (1+7/6+11/4+14/5+3/2)/5 = 1.844

SJF
4- Shortest Remaining Time

• Preemptive version of shortest process


next policy
• Must estimate processing time

15
Example (Shortest Remaining
Time)
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 33 15
6 84 20
5 10
2
Response time 3 Job13
5 enters 4 14 2
Service time 3 4 Job
Job enters63 finishes4 5 2
Job 2 Job 3Remaining
enters enters Job
Remaining 5 finishes
time time
of Jobof2 Job
= 52 = 5
Wait time
Average response 0
time
JobRemaining
1 finishes
Remaining =time7
timeRemaining
Remaining
of Jobtime 0
(3+13+4+14+2)/5
Remaining
1time
= of
of1 Job =time
2Jobof =of
53 Job 29 4=
Job52 =05
=7.2
Throughput =Remaining
Job 5/20
Remaining = 1/4
2 isRemaining
the
time only Remaining
job
Remaining
oftimeJobtime 5time
= of
of2 Job =time
3Jobof =of
4 Job
4Job Job
= 24 = 5
35 5finishes
Ave (RespTime/ServTime)
Switch
Continue
Switchto to
Job
Continue
with Job
Job = (1+13/6+1+14/5+1)/5
Choose
2 13with
Choose Job 3 2Switch to Job 4= 1.6
Job5Job

SRTF
5- Highest Response Ratio Next
(HRRN)

• Choose next process with the greatest ratio


(time spent waiting + expected service time)

expected service time

•The process with the greatest value will be scheduled to run.


17
Example (Highest Response Ratio)
RR = (waiting-time + service-time) / service-time
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 33 96 13
4 20
5 15
2
Response time 3 7 9 14 7
Service time 3 Job
6 2 finishes
4 5 2
RR of JobJob 3 =3((9–4)+4)/4
finishes = 2.25
Wait time
Average response 0
time
Job 1 finishes
Job =RR
4Job 0 7
(3+7+9+14+7)/5
enters of JobRR
5 enters 4 =of Job9=4 8
((9–6)+5)/5 = 01.6
= ((13–6)+5)/5 = 2.4
Throughput 2=Job
JobJob 5/20
2 is = 1/4
the
3Jobs
enters only
enters3,
Jobs4RR job
3,of
in 4,Job
queue
5 in5queue
RR=of
((9–8)+2)/2 = 1.5
Job 5 = ((13–8)+2)/2 = 3.5
Ave (RespTime/ServTime)
JobSwitch
1 Has
has totofinsih
Has Job
to
Has
finish to=finish
2Choose
finish
Job 2(1+7/6+9/4+14/5+7/2)/5
JobJob 3 2 Job 5
2Choose
Job = 2.144

HRRF
• 6- Multilevel Feedback Queue
– The system maintains multiple ready queues
• For example, N priority levels
– CPU always serves the jobs in the highest priority
queue
– A new process will be assigned a high priority
– If a process used up its time quantum
• priority decremented by one (placed in a lower level queue)
– Favor I/O bound jobs
• Not use up time quantum  stay at the same priority level
– Good for time sharing systems
• There are many variations of the algorithm
20
21
22
Example (Multi-level Queue)
Job # 1 2 3 4 55
Arrival time 0 2 4 6 88
Finish time
Service time 11
3 20
6 16
4 19
5 10
2
Response
J1 J1 time
enters
J2
time
J2
enters,
J3
time
expJ311
enters,
J4
J1
time
expJ4
enters,
time
J5
J2
time
exp18J5
J5
enters,
time
exp
J3
time
exp
J1
time
exp
J2
J412
finishes
finishes
expJ3
time
expJ4
time
J2
exp
time13expexp 2
time
exp
Q(a):
Service J1Q(a):
Q(a):
Q(a):
time Q(a):
J2Q(a):
Q(a):
3J3Q(a): 6Q(a):
Q(a):
J4Q(a):
J5Q(a):
Q(a):
Q(a):
4 Q(a):
Q(a):5 2
Q(b):
Q(b):
Q(b):
Q(b):
J1Q(b):
Q(b):
J2Q(b):
Q(b):
J3Q(b):J4Q(b):
Q(b): Q(b):
J5Q(b):
Q(b):
Q(b):
Q(b):
Wait time
Q(c):
Q(c):
Q(c):
Q(c):
Q(c): 8J1,Q(c):
J1Q(c):
J1Q(c):
J1,
J2
Q(c):
J1,
J2 9J2,
Q(c):
Q(c):
J1, J1,
J2,
Q(c):
J3
J1,
J2, 5J2,
J1,
Q(c):
J3J2,J2,
Q(c):
J3, J3,
Q(c):
J3,
J3,
J4
J3,
Q(c):
J4
J4
J4J49
J4 5
Q(d):
Average Q(d):
Q(d):
Q(d)L
Q(d):
response Q(d):
Q(d):
Q(d):
time Q(d): Q(d):
= Q(d): Q(d):
Q(d):
Q(d):
Q(d):
(11+18+12+13+2)/5J2Q(d):
J2,J2,
J3J3,
J3,=J4,
J4 J2
11.2
J1 J1
starts
Switch
continues
J2 Switch
continues
J3
to Switch
J2
continues
J4 to Switch
to Switch
J3
continues
J5 J4 J5 to
continues
to
Switch
SwitchJ1
Switch
to
Switch
to
Switch
J2 J3
to J4
to to
J2 J3

Feedback
Queue
Level = 4
1a 1b 2a 1c 1c 1c 1c 1c 1c 1c 1c
1c 2b 3a 2c 2c 2c 2c 2c 2c
2c 3b 4a 3c 3c 3c 3c
3c 4b 5a 4c 4c
4c 5b

You might also like