CSL373: Lecture 6 CPU Scheduling
CSL373: Lecture 6 CPU Scheduling
CSL373: Lecture 6 CPU Scheduling
CPU Scheduling
First come first served (FCFS or FIFO)
• Simplest scheduling algorithm
Run jobs in order that they arrive
Disadvantage: wait time depends on arrival order. Unfair
to later jobs (worst case: long job arrives first)
e.g.,: three jobs (A, B, C) arrive nearly simultaneously)
100 units 2 1
cpu Job
Job a Job b
c
0 time 103
what’s the average completion time?
1 2 100 units
cpu Job
Job b Job a
c
0 and now? time 103
FCFS and I/O utilization
• A CPU bound job will hold CPU until done, or it
causes an I/O burst (rare occurrence, since the
thread is CPU-bound) aka convoy effect
• Iong periods where no I/O requests issued, and CPU held
• Result: poor I/O device utilization
• Example: one CPU bound job, many I/O bound
CPU bound runs (I/O devices idle)
CPU bound blocks
I/O bound job(s) run, quickly block on I/O
CPU bound runs again
I/O completes
CPU bound still runs while I/O devices idle (continues…)
Possible solution: run process whose I/O completed?
Will it always work?
Round robin (RR)
• Solution to job monopolizing CPU? Interrupt
it.
Run job on some “time slice”, when time is up, or it
blocks, move it to back of a FIFO queue
Most systems do some flavor of this
• Advantage:
• fair allocation of CPU across jobs
• low average waiting time when job lengths vary:
1 1 1 1 1 98 units
cpu Job Job Job Job Job Job a
a b c a b
0 time 103
What is the avg completion time?
Round Robin’s Big Disadvantage
• Varying sized jobs are good, but what about
same-sized jobs? Assume 2 jobs of time=100
each:
1 2 3 4 5 199 200
cpu A B A B A A B A B
If it’s set too low, you’re spending all of your time context
switching between threads.
emacs
emacs
If previous CPU jobs in the sequence have run quickly, future
ones will too (“usually”)
Approximate STCF
• ~STCF: predict length of current CPU burst
using length of previous burst
Record length of previous burst (0 when just created)
At scheduling event (unblock, block, exit, …) pick smallest
“past run length” off of ready queue.
time
pick
19 10 3 1 100
pick
19 10 3 100 9
pick
19 10 9 100 2
Elevator in Bharti Bldg.
• To choose direction:
– Uses FCFS
• In each direction:
– Follows STCF
Disk drive head
• A disk drive receives many r/w requests for different
sectors simultaneously.
• Disk organized as concentric circles (called cylinders).
• The disk rotates around the center
• The disk head positions itself appropriately to read the
requested sector. This positioning is also called “disk
seek” and the time taken, “seek time”
Requested sectors:
231, 245, 636, 354
Disk drive (STCF in action)
• Disk can predict length of next “job”!
– Job = request to disk
– Job length ≈ cost of moving disk arm to position of the
requested disk block. (Farther away = more costly.)
• STCF for disks: shortest-seek-time-first (SSTF)
• Do read/write request closest to current position
• Preemptive: if new jobs arrive that can be serviced on the way, do
these too.
• However, do not change direction (just like an elevator). Hence,
also called “elevator algorithm”
• Elevator algorithm:
– Disk arm has direction, do closest request in that direction.
Sweeps from one end to other
~STCF vs RR
• Three processes P1, P2, P3
10ms 10ms 10ms 10ms 10ms 10ms 10ms 10ms
P1
I/O busy
Problem: Full I/O utilization, but P3 gets starved!
Generalizing: priorities + history
• ~STCF good core idea but doesn’t have enough state
The usual STCF problem: starvation
Solution: compute priority as a function of both CPU time P
has consumed and time since P last ran
priority