CS111 - Winter 2008
Lecture 7 (January 30, 2008)

by Caleb Kapusta and Amarin Phaosawasdi


Scheduling

Which resources? 

Scale of scheduling

  1. Long term:
  2. Medium term:
  3. Short term:

Scheduling metrics


First Come First Served (FCFS)

Job Names Arrival Times (AT) Run Times (RT) Wait Times (WT) Turnaround Times (TT)
A 0 5 0 5
B 1 2 4 + x 6 + x
C 2 9 5 + 2x 14 + 2x
D 3 4 13 + 3x 17 + 3x

Advantages

Disadvantages


Shortest Job First (SJF)

Job Names Arrival Times (AT) Run Times (RT) Wait Times (WT) Turnaround Times (TT)
A 0 5 0 5
B 1 2 4 + x 6 + x
C 2 9 9 + 3x 18 + 3x
D 3 4 4 + 2x 8 + 2x

Advantages

Disadvantages

Can we improve even more on average wait time?

Problems

Job Names Arrival Times Run Times
A 0 3
B 0 2
C 1 2
D 3 2
E 5 2
F 7 2
X 2x + 1 2

Job A waits forever.

Round Robin

If one process uses the CPU more than a quantum time Q (usually configurable), it is preempted and has to continue its job in the next round.

Suppose Q = 2, then the jobs in the table below would be scheduled as follows.
AA.BB.CC.DD.AA.CC.DD.A.CC.CC.C
where the dot represents context-switching.

Job Names Arrival Times Run Times Wait Times Turnaround Times
A 0 5 0 5
B 1 2 4 + x 6 + x
C 2 9 9 + 3x 18 + 3x
D 3 4 4 + 2x 8 + 2x

When we compare round robin to FCFS and SJF, we can see that the waiting time is better, but the turnaround time gets worse.

The waiting and turnaround time actually depends on Q.

Observations

Problems

Job Names Arrival Times Run Times
A 0 3
B 2 2
C 4 2
D 6 2
E 8 2
F 10 2
X 2x 2

The round robin algorithm schedules the jobs as follows.

AA.BB.CC.DD.EE.FF... and so on, but A never gets executed again (starvation).

Solution

Each job arrives at the end of the queue. This way, jobs that have already started would be executed first if its round coincides with an arriving process.

Priority Scheduling

In the real world, some jobs have more importance than others and should be executed first. Examples include interactive applications.

Convention

Small number = high priority. For example, priority 1 is more important than priority 2.

Observations

We now introduce other priorities which are

Problems

Job Names Arrival Times Run Times Priorities
A 0 5 2
B 1 2 2
C 2 9 0
D 3 4 1

The priority column is the external priority set by the user.

Suppose Q = 2, the job scheduling would be as follows.

AA.CC.CC.CC.CC.C.DD.DD.BB.AA.A

Solution

We can see that jobs with low priority can still starve, so we need what is called priority aging, which means that the longer the process lives, the lower its priority becomes. Priority aging is an example of internal priorities.

Real world scheduling problems

Problem 1: Priority Inversion

Suppose we have three jobs, namely, IO, Med, and High.

Tio is an input/output job with the lowest priority. It acquires a lock, computes, and returns the lock.
Tmed is a medium priority job.
Thigh is a high priority job and also needs the same lock.

Time Tio Tmed Thigh
0 (runnable) (waiting) (waiting)
1 lock ( &m )    
2 (context switch)   (runnable) (runnable)
3     lock ( &m )
4 (context switch) (runnable) (runnable) (blocked)

At time 0, only IO is runnable, so IO is scheduled.
At time 1, IO acquires the lock.
At time 2, Med and High become runnable. Context switch, and High gets its turn.
At time 3, High needs the lock, but IO has it, so High blocks.
At time 4, Medium gets its turn because it has higher importance than IO.

The problem is that Medium should not be scheduled when High is still in the loop.

Solution

High can "lend" its priority to IO when it wants some resources from IO.
This way IO will have higher priority than Med until it releases the lock.

Problem 2: Receive Livelock

Suppose we have a buffer that receives external network packets. The OS reads these packets and service them (by which point, the packets will be removed from the buffer.)

Packet entries cause interrupts which are more important than servicing.

During busy times, when packets arrive rapidly, instead of doing servicable work, we are only handling interrupts.
This can also be viewed as a denial of service.

Valid HTML 4.01 Transitional