Title: Last Time
1Last Time
- Processes
- Mechanisms Dispatcher
- while (1) stop A, store A, load B, run B ?
Easy! - But, not so straightforward
- How does the dispatcher gain control of the
processor? - Saving context, registers really tricky?
- Whos B? ? Today
2CPU Scheduling
UNIVERSITY of WISCONSIN-MADISONComputer Sciences
Department
CS 537Introduction to Operating Systems
Andrea C. Arpaci-DusseauRemzi H.
Arpaci-Dusseau Haryadi S. Gunawi
- Questions answered in this lecture
- What is scheduling vs. allocation?
- What is preemptive vs. non-preemptive scheduling?
- What are FCFS, SJF, STCF, RR?
- What are their advantages and disadvantages?
3Types of Resources
- Resources can be classified into one of two
groups - 1) Preemptible resources
- Can take resource away, give it back later
- Resource has little state associated with it
- May only have one of this resource
- Example CPU, memory, i/o channel
- 2) Non-preemptible resources
- Once given resource, cannot be reused until
voluntarily relinquished - Need many instances of this resource
- Example Blocks on disk
- Cant we make disk space preemptible?
- Yes, if we have backup tapes. But since, disk is
the last level of storage in our PCs, disk space
is not preemptible
4OS Management
- Type of resource determines how
- the OS manages it
- Non-preemptible allocation policy
- Decide which process gets which resource
- Ex store a 1-GB file, the OS (specifically file
system) will allocate the space - Preemptible scheduling policy
- Decide order in which requests are serviced
- Decide how long process keeps resource
- Ex CPU scheduling (this lecture)
- Ex Memory swapping (put most recently-used data
in the memory, and put least-recently-used data
in the disk)
5Levels of CPU Management
- Dispatcher
- Low-level mechanism
- Performs context-switch
- Scheduler
- Policy to determine which process gets CPU when
- Allocator
- Policy to determine which processes compete for
which CPU - Needed for multiprocessor, parallel, and
distributed systems
6CPU Workload Model
- Workload contains collection of jobs (processes)
- Job model
- Job alternates between CPU and I/O bursts (i.e.,
moves between ready and blocked queues) - CPU-bound job Long CPU bursts
- I/O-bound job Short CPU bursts
73 states of a processWho controls which
transition
Running
Ready
Blocked
- Running ? Blocked
- controlled by the application (e.g. could only
happen if the application calls an I/O operation,
or waiting for some events to happen) - Blocked ? Ready
- controlled by the I/O or event mechanism.
- Ready ? Running
- controlled by the scheduler
- Running ? Ready
- controlled by the scheduler, could only happen
if theres a hardware interrupt
8Non-preemptive vs. preemptive scheduler
- Although CPU is a preemptible resource, we can
design a non-preemptive scheduler or a preemptive
scheduler - Non-preemptive scheduler
- Process remains scheduled until voluntarily
relinquishes CPU - Preemptive scheduler
- Process may be descheduled at any time
9Scheduling Performance Metrics
- Minimize waiting time
- Do not want to spend much time in Ready queue
- (In this course, we simplify the definition of
wait time. Wait time the difference between the
time a process arrives and the time the process
gets the CPU for the first time) - Minimize turnaround time
- Do not want to wait long for job to complete
- Maximize throughput
- Want many jobs to complete per unit of time
- Maximize resource utilization
- Keep expensive devices busy (CPU, disks)
- How can CPU scheduling affects I/O (e.g. disk)
utilization??? See 2 examples in this notes - Minimize response time
- Schedule interactive jobs promptly so users see
output quickly - Minimize overhead
- Reduce number of context switches
- Maximize fairness
- All jobs get same amount of CPU over some time
interval
10Gantt Chart
- Illustrates how jobs are scheduled over time on
CPUExample
11First-Come-First-Served (FCFS)
- Idea Maintain FIFO list of jobs as they arrive
- Non-preemptive policy
- Allocate CPU to job at head of list
Average wait time(0 (10-1)
(12-2))/36.33 Average turnaround time (10
(12-1) (16-2))/311.67
12FCFS Discussion
- Advantage Very simple implementation
- Disadvantage
- Waiting time depends on arrival order
- Potentially long wait for jobs that arrive later
- Convoy effect Short jobs stuck waiting for long
jobs - Hurts waiting time of short jobs
- Reduces utilization of I/O devices
- Example 1 mostly CPU-bound job (process A), 3
mostly I/O-bound jobs (process B, C, D) - Process A uses CPU a lot, and calls I/O rarely
CPU
Disk
Idle
Time
13Advanced FIFOPreemptive vs. non-preemptive FIFO
- FIFO with I/Os
- In the last 2 slides, two examples were given for
FCFS. The processes in the first example perform
no I/O (hence FIFO looks simple!). - In the second one, the processes perform some
I/Os. - Preemptive or no-preemptive FIFO?
- Thus, if a process A is currently running and
then performs an I/O, it will go to the blocked
queues. After the I/O finishes, A is ready, but
the process B is hugging the CPU. The question is
should A take over the CPU or not? - The answer depends whether you want to create a
preemptive or non-preemptive FIFO. - Example
- Process A, at time 1-10 uses the CPU, at time
11-15 uses the disk, and needs to continue for
another 5 CPU time - Process B needs 10 CPU time, and it comes in
later than A - Gantt Chart (see next slide)
14Preemptive vs. non-preemptive FIFO
- Non-preemptive FIFO will look like this
- Why? Because at t15, the scheduler cannot
preempt B, thus A must wait in the ready queue - Thus, FIFO does not look like a FIFO since B is
out first than A
A
B
A
25
0
10
20
Time
- Preemptive FIFO will look like this
- Preemptive means the scheduler can take over the
CPU anytime it wants - In this case, my preemptive scheduler policy
specifies that if a process (A) in a ready queue
arrived earlier than the arrival time of running
process (B). The earlier process (A) should take
over the CPU.
A
B
A
B
15
25
0
10
20
Time
15Shortest-Job-First (SJF)
- Idea Minimize average wait time by running
shortest CPU-burst next - Minimize convoy effect
- Use FCFS if jobs are of same length
- Non-preemptive SJF looks like this
Average wait (0 2 6) / 3 Average
turnaround (2 6 16) / 3
A
B
C
0
2
6
16
Time
16SJF Discussion
- Advantages
- Provably optimal for minimizing average wait time
(with no preemption) - Moving shorter job before longer job improves
waiting time of short job more than it harms
waiting time of long job - Helps keep I/O devices busy
- Disadvantages
- Not practical Cannot predict future CPU burst
time - OS solution Use past behavior to predict future
behavior (i.e. adaptive priority, next lecture) - Is this a stupid policy? No, 40 years ago, in
batch programming system, SJF was literally used
in real systems. - Starvation Long jobs may never be scheduled
17Non-preemptive vs. Preemptive SJF
- Given the arrival time like described in the
table below, show the Gantt chart for
non-preemptive and preemptive SJF. The preemptive
one is shown below. How about for non-preemptive?
If you think about this for a while, preemptive
SJF doesnt make sense because the scheduler
always uses the initial CPU burst for making a
decision. However, some of the CPU burst may have
been served. Thus, preemptive SJF should use the
remaining CPU burst for making the decision.
Hence, STCF comes into play (see next slide)
A
B
C
A
B
0
1
7
2
4
16
Time
18Shortest-Time-to-Completion-First (STCF or SCTF)
- Idea Add preemption to SJF
- Schedule newly ready job if shorter than
remaining burst for running job
Non-preemptive SJF Average wait (0 8 12
17) / 4 STCF Average wait (0 1 5 17) / 4 ?
win a lot!
? Non-preemptive SJF
12
26
17
0
8
- STCF (see
- next slide for
- more details)
26
17
10
1
5
0
Time
19STCF (timing details)
- Colored CPU time means the process is being served
26
17
10
1
5
0
20STCF
- Advantages
- Better than SJF in terms of wait time
- Similar to SJF, overall turnaround time is much
better than FIFO (the idea is give the most to
those that need the least) - Disadvantages same with SJF
- Not practical, no future information
- Starvation (solution? Round-robin ? next slide)
21Round-Robin (RR)
- Idea Run each job for a time-slice (quantum) and
then move to back of FIFO queue - Preempt job if still running at end of time-slice
- Example quantum 1 CPU unit
Average wait (0 0 0) / 3 Turnaround time
(16 (5-1) (10-2)) / 3
A
B
C
A
B
C
A
C
A
C
A
Time
22RR timing details
How about if quantum 2 unit?
23RR Discussion
- Advantages
- Jobs get fair share of CPU
- Shortest jobs finish relatively quickly (compare
the turnaround time of RR and FIFO using the
previous example) - Disadvantages
- Poor average waiting time with similar job
lengths - Example 10 jobs each requiring 10 time slices,
and quantum 1 - RR All complete after about 100 time slices
- P1 ends at 91, P2 at 92, P3 at 93, and so on
- FCFS performs better!
- P1 ends at 10, P2 at 20, P3 at 30, P4 at 40
- Is this another stupid policy?
- No, in the old days, only 2-3 people working on
the machine, and only small number of processes - Performance depends on length of time-slice
- If time-slice too short, pay overhead of context
switch - If time-slice too long, degenerate to FCFS
- How to choose a good quantum?
24RR Time-Slice
- IF time-slice too long, degenerate to FCFS
- Example
- Job A w/ 1 ms compute and 10ms I/O
- Job B always computes
- Time-slice is 100 ms ? underutilize I/O ! Disk
utilization is about 10
CPU
B
A
B
A
Disk
A
Idle
A
Idle
Time
25RR Time-Slice (contd)
- Goal Adjust length of time-slice to match I/O
burst - In this example, e.g. 10 ms
- We want something like this
- I/O is not underutilized! Disk utilization is
high - Lots of overlapping between CPU and I/O
utilization ? good
CPU
B
A
B
A
B
A
B
A
Disk
A
A
A
A
Time
26Summary
- So far SJF, STCF, Round-Robin all bad
- Solution priority-based? (next lecture)
- Recap
- No best general-purpose scheduling policy
- All depends on the workload
- Need to know which workload fits with which
scheduler - Rule of thumbs
- History and future are important
- Unfortunately future information not always
available - Hence, predict the future (next lecture)