You are on page 1of 28

Processor Scheduling

What Are We Covering?


Processor Scheduler
Short Term - Schedule ready processes to processor(s) Long Term - Schedule processes to reside in memory

How to design a good processor scheduling algorithm Performance Metrics

Quality of Scheduling Algorithms


Performance Metrics
Turnaround Time: average (waiting time + service time) Response Time
Similar to turnaround time Used in interactive environment

Throughput: Number of jobs completed per unit time Utilization: the percentage of time the processor is busy Turnaround time / Service time
Best matches with users expectation A longer job can have a longer wait time

Fairness - no starvation

Constraints on Scheduling
Nonpreemptive
Process will run till terminated or blocked

Preemptive
Process can be interrupted while in execution E.g., preempted by time quantum

Real-Time Scheduling
Each process has to finish before its deadline

Job Characteristics
CPU bound
Takes long computation time Little I/O

I/O bound
Repeat: run for short time; need I/O

Scheduling Algorithms
First-In-First-Out (FIFO, FCFS) Shortest Job First Highest Response Ratio First Shortest Remaining Time First Round-Robin Multilevel Feedback Queue

Scheduling Algorithms
First-in-first-out
Easy to implement Short jobs may be stuck behind a long job

Shortest Job First


Non-preemptive Minimize average turnaround time if all jobs arrive at the same time
A short job still can get stuck behind a long job

Standard deviation of turnaround time may not be good May cause starvation Need to predict execution time of each job

Scheduling Algorithms
Shortest Remaining Time First
Preemptive Allow a job with shorter remaining time to preempt current running job Minimize average turnaround time Similar problems with SJF

Scheduling Algorithms
Highest Response Ratio Next
The ready process with the greatest response ratio (RR) is selected to run RR = (waiting-time + service-time) / service-time Shorter jobs are favored Take waiting time in to account
Avoid starvation problem More fair

Need to predict execution time of each job Can be non-preemptive or preemptive

Example (FIFO)
Job # Arrival time Finish time Servicetime Response time Service time 1 0 3 3 3 3 2 2 9 6 7 6 3 4 13 4 9 4 4 6 18 5 12 5 5 5 8 8 20 2 12 2

Wait time 0 1 5 7 Average response time = (3+7+9+12+12)/5 = 8.6 10 Throughput = 5/20 = 1/4 Ave (RespTime/ServTime) = (1+7/6+9/4+12/5+12/2)/5 = 2.564

FIFO

Example (Shortest Job First)


Job # Arrival time Finish time Servicetime Response time Service time 1 0 3 3 3 3 2 2 9 6 7 6 3 4 15 4 11 4 4 6 20 5 14 5 5 5 8 8 11 2 3 2

finishes 9 Wait time 0 1 finishes Average response time =Job 2Job 5 7 (3+7+11+14+3)/5 = 7.6 1 Jobs Jobs entersenters 3, 4, 3, 4 in queue Job 2Job 3 Job 4Job 5 enters 5 in queue enters Throughput = 5/20 = 1/4 But has to 3,Job3,Job 2allshortest finish Jobthe the But has to Jobs Jobsbothis5 3 isin queue finish Job154,queued shortest 4 Ave (RespTime/ServTime) = (1+7/6+11/4+14/5+3/2)/5 = 1.844

SJF

Example (Shortest Remaining Time)


Job # Arrival time Finish time Servicetime Response time Service time 1 0 3 3 3 2 2 3 4 4 6 20 5 14 5 5 8 8 10 2 2

15 8 6 4 Job13enters 4 5

3 Job 4 Job 6 finishes 3 enters 4 5 2 Remaining time of Job 2 Job 3Remaining timefinishes2 Job 2 = 5 entersenters Job 5 of Job = 5 Wait timeJob 1 finishesof (3+13+4+14+2)/5 4 = 52 =05 0 Remaining of0Job53 Job = 7.2 9 Average response Remaining time 1 2 =time of Job Remaining Remainingtime = Job 1 Job Remaining time of = time of = 2 time 7 Throughput = 5/20the 1/4 Job 2 Job 3Job44 Job 5 = 24 = 5 Job 2 is = only job = 5 Remaining time time Job 5 Remaining timeRemaining of =time of finishes Remaining of Remaining of = 3 Job time of Ave (RespTime/ServTime)Choose53 2Switch to Job 4= 1.6 Switch Continue with JobJob to Job 2 Choose Job Continue with Job 13 = (1+13/6+1+14/5+1)/5 Switch to Job

SRTF

Example (Highest Response Ratio)


RR = (waiting-time + service-time) / service-time Job # Arrival time Finish time Servicetime Response time Service time 1 0 3 3 3 3 2 2 9 6 7 3 4 13 4 9 4 6 20 5 14 5 5 8 8 15 2 7

Job 2 finishes 6 4 5 2 RR of JobJob 3 finishes = 2.25 3 = ((94)+4)/4 Wait timeJob 1 finishes=RRenters RR of Job94 8 ((136)+5)/5 = 2.4 0 4Job(3+7+9+14+7)/5 = = = 0 0 of Job 7 = ((96)+5)/5 1.6 Average response Job enters time 5 4 Throughput 2 Job is enters 4 in of JobRR of Job 5 = ((138)+2)/2 = 3.5 =enters = only job4, 5 in = ((98)+2)/2 = 1.5 2 3Jobs 3, 3, Jobs JobJob 5/20the 1/4RR queue 5queue Ave (RespTime/ServTime) =finish2Job 2 Job 5 Job 2 to Has JobSwitchto finsihChoose Job 3 1 Has Has to finish2Job Choose has tofinish Job (1+7/6+9/4+14/5+7/2)/5 = 2.144

HRRF

Scheduling Algorithms
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

Example (Round Robin)


Job # Arrival time Finish time Servicetime Response time Service time 1 0 4 3 4 3 2 2 18 6 16 6 3 4 17 4 13 4 4 6 20 5 14 5 5 5 8 8 15 2 7 2

WaitJ1 AssumeJ2 J4J2expires J5 J4 J3 J4 J5 finishes 5 time J3 enters, J5 J3expires time time 9 1 enters, then J3 expires expires J2time time J2 J1 9 time expires, enters finishestime before time time quantum10 time expires expires J2 quantum J1 enters, then J2 expires expires expires time expires Average responseQueue:Queue: Queue:J4,J3,J2,J5,J4,J3,J2 Queue: Queue: Queue: Queue: queue) J5,J5 10.8 J1 J2 = (4+16+13+14+7)/5 J4 Queue: Queue: Queue: Queue: inJ5,J4,J3,J2,J5,J4,J3, (J2 J3,J2,J5,J4,J3,J2, J4, J3, Queue: J3 Queue: Queue: J1,J2,J3,J2, J4, J3J2 J5,Queue: J4,J4 J3 J2 J1 J1 J2, time J3 J2 wasJ2, Queue: J2 =
continues J2 J1 J2 Switch Switch Switch Switch Switch J3 to to to to to to J3 Switch to to J2J4 J3 J2 J5 J4 J1 J1 Switch to to Switch Switch Switch to to to J4 starts Switch to Switch Switch Switch J2 J5

Round Robin T=1

Example (Round Robin)


Job # Arrival time Finish time Servicetime Response time Service time 1 0 5 3 5 3 2 2 17 6 15 6 3 4 13 4 9 4 4 6 20 5 14 5 5 5 8 8 15 2 7 2

Wait timeenters, J4 J3 J1 J29 J4 time finishes9 finishes expires 2 enters time expires J2 J3 J1 then J5 time time J3 expiresJ2 J4 time finishesenters expires enters, then J2 expires J5 finishes 5 time expires 5 Average responseQueue:=J2,J2,J4,J4, J3, J5, J5, J2, 10 J4 J4 Queue: Queue: Queue: J4,J3, J3, J5, J2, J2, J4 J2, time J3,J2 J4 J3 J1 J3, (5+15+9+14+7)/5 J4Queue: J1, Queue: Queue: J2 Queue: Queue: J1Queue: Queue:J2 Queue:J5 Queue:= J4
Switch to J3 to J2 Switch to Switch to Switch Continues Switch J1 J3 J2 J4 J3 J5 J4 J2 continues to continues J1 starts Switch Switch to Switch to Switch to J2 to J4

Round Robin T=2

Scheduling Algorithms
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

Example (Multi-level Queue)


Job # Arrival time 1 0 2 2 3 4 4 6 5 5 8 8

Finish time 11 20 16 19 10 Servicetime 3 6 4 5 2 Response J2 J2 J3 J311 J4 J5 J5 J5 J1 J212 J4 J2 time exp 2 18 J1 J1 time time J4 enters, J3 finishestime exp enters exp J1 time enters, J4 J3 exp13 time enters, J2 time time time time exp enters, time time time finishes exp exp exp exp exp exp exp
Q(b):Q(b):Q(b):Q(b): Q(b):Q(b):Q(b):Q(b): Q(b):Q(b):Q(b):Q(b): J4 J5 J1 J2 J3 Q(b):Q(b):Q(b):Q(b): Wait time Q(c):Q(c):Q(c):Q(c):Q(c):Q(c):Q(c): 8 J2 J29 J2,J2,J2,J2,J3,J4 9 5 J4 J4 5 Q(c):Q(c):Q(c):Q(c):Q(c):Q(c):Q(c):Q(c):J4 Q(c): J1 J1 J1,J1,J1,J1,J1,J1,J1,J2,J3,J4 J4 J2, J3 J3 J3,J3,J3, Q(d):Q(d):Q(d):Q(d): = Q(d):Q(d):Q(d):Q(d): J3,J4, Q(d):Q(d)L Q(d):Q(d): (11+18+12+13+2)/5 11.2 J2 J2, J3 Average response timeQ(d):Q(d):Q(d):Q(d): J2,J3,= J4 J2 J1 J1 Switch J3 Switch J5 Switch Switch J3 J4 to J3 starts Switch J4 Switch to Switch to to to J2 continues J2 to J3 to J4 Switch to Switch J2 continues continues J5 to J1 J2 to continues continues Switch Feedback Queue Level = 4
1a 1b 2a 1c 1c 1c 1c 1c 1c 2b 3a 2c 2c 2c 2c 3b 4a 3c 3c 4b 1c 2c 3c 5a 4c 1c 2c 3c 4c 5b 1c 2c 3c 4c

Q(a):Q(a): J2 3 Q(a):Q(a):Q(a):Q(a):Q(a): J1 Q(a):Q(a):Q(a): J4 6 J5 J3 Service time Q(a):Q(a):Q(a):Q(a):Q(a):Q(a): 5 4

Example (Multi-level Queue)


Job # 1 2 3 4 5

Arrival time
Service time

0
3

2
6

4
4

6
5

8
2

J1 J1 J2 J2 J3 J3 J4 J4 J5 J5 J2 time time exp enters time J1 enters, time J3 exp time exp time J1 enters, exp enters, exp time exp enters, exp time finishes exp Continue the pattern Q(a):Q(a):Q(a):Q(a):Q(a): J5 Q(a):Q(a):Q(a):Q(a):Q(a): J1 J2 J3 J4 Q(b):Q(b):Q(b):Q(b):Q(b): J2,J2, J4, J3, J5 J2, J4, J3, J5 Q(b):Q(b):Q(b):Q(b):Q(b): J4, J3 J1 J1 J1,J2 J2,J3,J3, J2, J4 J2 J3 J2 Till jobs finish J1 J1 Switch Switch Switch to to J2 starts Switch Switch to J3 J5 continuesSwitch Switch Switch to to to to J4 to J2 J1 J3 J2

Feedback Queue Level = 2

Example (Multi-level Q, from Book)


Job # 1 2 3 4 5

Arrival time
Service time

0
3

2
6

4
4

6
5

8
2

When exp time time exp J1 J1 J2 J2 J3 J3time quantum expires, exp enters time J4 J4 J5 J5 J4 J5 J3 time enters, exp exp time exp J1 enters, J2 time exp enters, time J1 enters, finishes time finishes exp whetherJ3 Q(a):Q(a):in the there is a J5 Q(a):Q(a):Q(a):Q(a): J4 job J1 J1 J2 Q(a):Q(a):Q(a):Q(a):Q(a):Q(a): queue, the J2,J3 J3,J4 J4, J5 Q(b):Q(b):Q(b):Q(b):Q(b):Q(b): J5 be Q(b):Q(b):Q(b):Q(b):Q(b):Q(b): J1 J1,J2 process J4 J2 J3 should moved to next level.J2, J3 J2 J2 J2, J3 Q(c):Q(c):Q(c):Q(c):Q(c):Q(c): J2,J2, J3, J4 Continue the pattern Q(c):Q(c):Q(c):Q(c):Q(c):Q(c): J3, J4 The policy in the book J2, J3, J4 Q(d):Q(d):Q(d):Q(d):Q(d):Q(d): does Q(d):Q(d):Q(d):Q(d):Q(d):Q(d): J1 J1 Switchhave a good reasoning. to J2 Till jobs finish startsnot Switch Switch Switch J5 Switch to to to to to J5 continuesSwitch Switch to to Switch to Switch Switch J4 to J2 J1 J3 J2 J4 J3

Feedback Queue Level = 4

Example (Multi-level Queue)


Job # 1 2 3 4 5

Arrival time
Service time

0
3

2
6

4
4

6
5

8
2

J1 J1 J2 J2 J3 J3 J4 J2 J5 J5 time J4 J4 J5 finishesfinishes enters time J1 enters expires time exp J2 J3 finishes enters, exp time exp time J1 enters, time time exp enters, exp time J3 exp exp time finishes exp Queue level i: has time quantum 2i Q(a):Q(a):Q(a):Q(a):Q(a): J5 J1 J1 J2 J3 J4 J4 Q(a): Q(a): Q(a):Q(a):Q(a):Q(a):Q(a):Q(a):Q(a): Q(a): - Anyway there is no higher priority jobs Q(b):Q(b):Q(b):Q(b):Q(b): J3,J3, J4, Q(b): Q(b): Q(b):Q(b):Q(b):Q(b):Q(b):Q(b):Q(b): J5 J1 J1,J2 J2,J3 J3 J4 J4, J5 J2 J3 J5 Q(b): - Jobs in lower level queue need more CPU time Q(c):Q(c):Q(c):Q(c):Q(c): J2 J2 Q(c): J2,J2, J3, J4 J4 J2 Q(c): Q(c): J3, Q(c):Q(c):Q(c):Q(c):Q(c):Q(c): J2, J3 J3, Q(c): J4 J4 - Increasing time quantum reduces context switch Q(d):Q(d):Q(d):Q(d):Q(d): Q(d):Q(d): Q(d): Q(d): Q(d):Q(d):Q(d):Q(d):Q(d): Q(d): J1 J1 Switch Switch Switch Switch Switch Switch to to J4 starts Switch Switch to to to Switch to to Switch J3 continuesJ2 to to Switch J5 J3 to J4 J5 J2 Switch to J1 J3 J2 J4 to continues Feedback Queue Level = 4
Use the policy in book, so that it is consistent

Unix Processor Scheduling


Similar to multilevel feedback queue
Use a number to indicate job priority Priority level: 0-127 negative priority value: no interruption

time quantum = 0.1 sec A process


When enters the system, gets a base priority Priority changes depending on CPU usage

Unix Processor Scheduling


Priority computation Pi+1 = base + Ci / 2 + nice Ci = Ui + Ci1 / 2
Ci a count to keep track of the CPU usage history Ui CPU time units used in a time quantum i
A time quantum is divided into 60 units

The priority of a job rises when waiting


Ui will be 0 when waiting

I/O-bound jobs tend to have higher priorities than CPU-bound jobs

Unix Processor Scheduling Example


Priority computation Pi+1 = base + Ci / 2 + nice Ci = Ui + Ci1 / 2 Process A enters the system
Assume nice = 0 base = 60, U0 = 0 C0 = 0 P1 = 60

Process A gets to run immediately


base = 60, U1 = 60, C0 = 0 C1 = U1 + C0 / 2 = 60 Priority P2 = base + C1 / 2 = 60 + 30 = 90

Unix Processor Scheduling Example


Process A gets to run immediately
base = 60, U1 = 60, C0 = 0 C1 = U1 + C0 / 2 = 60 Priority P2 = base + C1 / 2 = 60 + 30 = 90

Process A waits afterwards


base = 60, U2 = 0, C1 = 60 C2 = U2 + C1 / 2 = 0 + 30 = 30 Priority P3 = base + C2 / 2 = 60 + 15 = 75

Process A runs again


base = 60, U3 = 20, C2 = 30 C3 = U3 + C2 / 2 = 20 + 15 = 35 Priority P4 = base + C3 / 2 = 60 + 17 = 77

Unix Processor Scheduling Example


Process A enters the system
Assume nice = 0 base = 60, U0 = 0 C0 = 0 P1 = 60

Process A waits
base = 60, U1 = 0, C0 = 0 C1 = U1 + C0 / 2 = 0 Priority P2 = base + C1 / 2 = 60 + 0 = 60

Process A runs for a short time


base = 60, U1 = 12, C0 = 0 C1 = U1 + C0 / 2 = 12 Priority P2 = base + C1 / 2 = 60 + 6 = 66

Unix Processor Scheduling Example


Process A runs for a short time
base = 60, U1 = 12, C0 = 0 C1 = U1 + C0 / 2 = 12 Priority P2 = base + C1 / 2 = 60 + 6 = 66

Process A runs for a short time again


base = 60, U2 = 10, C1 = 12 C1 = U1 + C0 / 2 = 12 Priority P3 = base + C1 / 2 = 60 + 6 = 66

Readings
Section 9.2, 9.3

You might also like