Recall the states of a process
- running --- using CPU
- ready --- in memory, ready for the CPU
- waiting --- waiting for an I/O device or interrupt
What causes a process to move out of the CPU?
- I/O request
- fork and wait for child
- an interrupt or signal
Processes are moved from one state to another using queues
- The job queue --- All processes in mass storage waiting for
allocation to main memory.
- The ready queue --- All processes in main memory waiting for the
CPU. Usually a linked list of PCB's (process control blocks) with pointers
to the first and last PCBs.
- Several device queues --- One for each device containing all
processes waiting for a particular device.
These are mainly disk drives tape drives, and terminals.
For shareable devices such as disk drives the
queue may have several entries.
For dedicated devices such as tape drives the queue would have at most one
entry, say waiting for the device to be ready.
Why are tape drives different from disk drives?
Schedulers
- Long-term scheduler or job scheduler decides which processes are admitted to the
system.
This is mainly for a batch system.
Determines the degree of multiprogramming.
Under stable conditions, only needs to work when a process terminates.
This is allowed to take a long time.
May not exist on a time-sharing system.
- Medium-term scheduler does swapping --- moving processes in and out
of memory.
Too may jobs in memory may lead to a lot of paging and decrease
performance.
- Short-term scheduler or CPU scheduler decides which process is
selected from the ready queue(s).
Must operate very quickly, several times a second.
Note: this may not be a FIFO queue.
When might the CPU scheduler need to make a decision?
When a process:
- switches from running to waiting --- say for I/O
- terminates
- switches from running to ready --- because of an interrupt
- switches from waiting to ready --- I/O has completed
Non-preemptive scheduling: Only 1 and 2 cause scheduler action.\
Once a process has the CPU, it keeps it until it needs I/O or is finished.
Not suitable for time-sharing.
Preemptive scheduling: 3 or 4 may cause scheduler action.
Note: 4 is a special case of 3.
preemptive examples 1
Context switch --- Switching the CPU to another process requires saving
the state of the old process and loading the saved state of the new process.
This can take from 1 microsecond to 100's of microseconds depending on
the hardware support.
Dispatcher --- The module that gives control of the CPU to the
process selected by the CPU scheduler. It does:
- context switching
- switching to user mode
- setting to program counter
Performance Criteria
Methods of measuring performance of CPU schedulers:
- Fairness
Each process should get a fair share of the CPU.
- Efficiency = CPU Utilization
Percentage of time CPU is busy. May be 40% to 90%.
- Throughput
The number of processes completed per unit time.
10 per second to 1 per hour.
- Turnaround Time
Time to execute a process from submission to termination.
This is the sum of CPU time, I/O time, and waiting time.
- Waiting Time
time in ready queue
The scheduling algorithm does not affect CPU time or I/O time.
- Response Time
Time between submission of a request and the first response.
This may be more appropriate for an interactive system.
- Response Time Variance
For an interactive system, it may be important for the response time to
not vary too much.
Performance Criteria
A simple multiprogramming example:
Assume 2 processes, each requires 60 seconds of CPU time and waits 1 second
for I/O every second.
Without multiprogramming, this takes 2 minutes for each process.
With multiprogramming the first finishes in 2 minutes and the
second one finishes 1 second later.
Without multiprogramming, CPU utilization is about 50%.
Scheduling Algorithms
First-Come/First-Served (FCFS)
FCFS is managed by a strict FIFO queue.
Example 1:
Prcocess | Burst Time |
1 | 24 |
2 | 3 |
3 | 3 |
CPU Gantt chart
Shows which process is using the CPU at any time.
Average waiting time = (0 + 24 + 27)/3 = 17.
Example 2: suppose the processes arrive in reverse order. We get:
AWT = (0 + 3 + 6)/3.
Original example:
Process Gantt Chart
For each process, shows that state of the process at any time.
The same chart may be represented as:
P1: RRRRRRRRRRRRRRRRRRRRRRRR
P2: rrrrrrrrrrrrrrrrrrrrrrrrRRR
P3: rrrrrrrrrrrrrrrrrrrrrrrrrrrRRR
Example 3:
Process | CPU Burst | I/O Burst | CPU Burst |
1 | 8 | 7 | 3 |
2 | 6 | 3 | 2 |
P1: RRRRRRRRwwwwwwwRRR
P2: rrrrrrrrRRRRRRwwwrRR
Note:
- The waiting time for a process is the number of r's in the string.
- The average waiting time is the total number of r's divided by
the number of processes.
- The CPU utilization is the total number of R's divided by the
total time (the length of the longer string).
Example 3 FCFS AWT = (0 + 9)/2 = 4.5
FCFS Example 3a
Shortest Job First (SJF)
Take the job with has the shortest CPU burst and run it next.
Example 4:
Prcocess | Burst Time |
1 | 6 |
2 | 8 |
3 | 7 |
4 | 3 |
AWT = (0 + 3 + 9 + 16)/4 = 7.
How do you know what the shortest job is?
Use past history to do the prediction.
Let τn+1 be the predicted time of the next cycle and tn the actual time of the current cycle.
Use τn+1 = αtn + (1 - α)τn
Usualy α = 1/2.
The initial τ0 can be defined as a constant which is the system average.
This mehtod is called the exponential average.
To see this, expand out the recurrence relation:
τn+1 = αtn + (1 - α)τn
τn+1 = αtn + (1 - α)(αtn-1 + (1 - α)τn-1)
τn+1 = αtn + (1 - α)αtn-1 + (1 - α)2τn-1
τn+1 = αtn + (1 - α)αtn-1 + (1 - α)2αtn-2 + (1 - α)3τn-2
τn+1 = αtn + (1 - α)αtn-1 + ... + (1 - α)jαtn-j + ... (1 - α)nαt0
Each successive term has a smaller weight.
Example 3 for SJF:
P1: rrrrrrRRRRRRRRwwwwwwwRRR
P2: RRRRRRwwwrrrrrRR
Example 3 SJF AWT = (6 + 5)/2 = 5.5
Preemptive Shortest Job First (PSJF)
The SJF algorithm may be preemptive or non-preepmtive.
In the preemptive version, if a new process enters the ready queue
with a shorter next CPU burst than
what is (expected to be) left of the currently executing process, the
current job with be replaced by the new one.
This is also called
shortest-remaining-time-first scheduling.
P1: rrrrrrRRRrrRRRRRwwwwwwwRRR
P2: RRRRRRwwwRR
Example 3 PSJF AWT = (8 + 0)/2 = 4
In non-preemptive scheduling, once the CPU has been allocated to a process,
it keeps the CPU until it terminates or requests I/O.
Pre-emptive scheduling allows a higher priority process to preempt
an executing process if its priority is higher.
In time sharing systems you don't want to have CPU intensive processes,
grab the CPU and keep it.
This is usually handled by the following preemptive algorithm.
PSJF Examples 1
Round Robin Scheduling (RR)
In round-robin scheduling, a small unit of time called a quantum is
defined (originally 10 to 100 milliseconds).
Example 1 using a quantum of 4:
P1: RRRRrrrrrrRRRRRRRRRRRRRRRRRRRR
P2: rrrrRRR
P3: rrrrrrrRRR
AWT = (6 + 4 + 7)/3 = 17/3 = 5.67.
If the quantum is small, n processes, each appear to have their own CPU which is
running at 1/n of the speed of the original machine.
Each time a process is switched out (context switch) there is overhead.
If the quantum is too small, no useful work will be done.
Usually there is some hardware assistance for context switching.
Rule of thumb 80% of the cpu bursts should be shorter than the quantum.
If the process executes its entire CPU burst during most quantums, you
will get much better turnaround time for the process.
The quantum affects both the efficiency of CPU utilization and the response time.
If the quantum is too long, response time suffers.
If it is too short, too much time is spent on context switches.
Example 3 with Round Robin and a quantum of 3:
P1: RRRrrrRRRrrrRRwwwwwwwRRR
P2: rrrRRRrrrRRRwwwRR
Example 3 RR3 AWT = (6 + 6)/2 = 6.0
RR Examples 1
Summary
We looked at four major scheduling algorithms:
- FCFS: simple, non-preemptive
Processes with long CPU bursts can starve out shorter processes
- SJF: short processes go first, non-preemptive
We have not way of knowning the next CPU burst, must be approximated.
Processes with long CPU bursts can delay shorter processes
- PSJF: preemptive
Still needs to be approximated
- RR: preemptive
Uses a quantum. Common algorithm for time sharing.
Next Notes
Back to CS 3733 Notes Table of Contents