OCR H446, section 1.2.1 (d)
A processor core can only run one process at a time, but many processes want it. Scheduling is how the operating system decides which process runs next, and for how long. A good scheduler makes the best use of the processor, gives fast responses to users, and is fair.
Key terms
- Process: a program that is being run.
- Burst time: the processor time a process needs.
- Waiting time: the time a process spends in the ready queue, waiting for the processor.
- Turnaround time: the total time from arrival to completion.
- Pre-emptive scheduling: the OS can stop a running process to give the processor to another. Non-pre-emptive: a process keeps the processor until it finishes.
- Starvation: a process that never gets a turn, because others keep going first.
The five algorithms
The examples use these processes, all arriving at time 0:
| Process | Burst time |
|---|---|
| P1 | 6 |
| P2 | 3 |
| P3 | 1 |
First come first served (FCFS)
Processes run in the order that they arrive, each to completion. It uses a queue, and is non-pre-emptive.
Waiting times: P1 = 0, P2 = 6, P3 = 9. Average = .
- Advantage: simple and fair in order.
- Disadvantage: a long job at the front makes short jobs wait (the "convoy effect"), so the average wait can be long.
Shortest job first (SJF)
The process with the smallest burst time runs next. It is non-pre-emptive.
Waiting times: P3 = 0, P2 = 1, P1 = 4. Average = .
- Advantage: gives the lowest average waiting time.
- Disadvantages: the burst time must be known or estimated in advance, and long jobs may starve if short jobs keep arriving.
Shortest remaining time (SRT)
A pre-emptive version of SJF. Whenever a new process arrives, the OS compares the remaining time of the running process with the new one's burst time, and switches to whichever is shortest.
| Process | Arrives | Burst |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
- At time 2, P2 arrives (burst 4) and P1 has 5 left, so P2 pre-empts P1.
- At time 4, P3 arrives (burst 1), and P2 has 2 left, so P3 pre-empts P2.
- P3 finishes at 5. P2 (2 left) then runs and finishes at 7. P1 (5 left) finishes at 12.
Waiting times: P1 = , P2 = , P3 = . Average = 2.
- Advantage: very low waiting times for short jobs.
- Disadvantages: long jobs may starve, and there are more switches (overhead).
Round robin (RR)
Each process gets a fixed time slice (quantum) in turn. If it isn't finished when the slice ends, it is pre-empted and goes to the back of the queue.
With a quantum of 2:
- P1 runs 0 to 2 (4 left), P2 runs 2 to 4 (1 left), P3 runs 4 to 5 and finishes (it needed only 1).
- P1 runs 5 to 7 (2 left), P2 runs 7 to 8 and finishes, P1 runs 8 to 10 and finishes.
Waiting times: P1 = , P2 = , P3 = . Average .
- Advantages: fair, no starvation, and good response time for interactive users. Suits multi-user and multitasking systems.
- Disadvantages: many context switches waste time. If the quantum is too small, the overhead is large. If it is too big, it behaves like FCFS.
Multi-level feedback queues (MLFQ)
There are several queues, each with a different priority and often a different quantum.
-
A new process enters the top queue.
-
If it uses its whole time slice, it is judged to be a long job, and is moved down to a lower queue.
-
If it gives up the processor early (for example waiting for input), it stays high (or moves up). So short, interactive jobs get quick responses.
-
The scheduler always runs a process from the highest non-empty queue.
-
To stop low-priority jobs from starving, a process that has waited a long time is moved up (aging).
-
Advantages: adapts to the behaviour of each process, with no need to know burst times in advance, and gives a good mix of responsiveness and throughput.
-
Disadvantage: more complicated to set up and run.
Comparison
| Algorithm | Pre-emptive? | Good for | Weakness |
|---|---|---|---|
| FCFS | No | Simplicity | Short jobs wait behind long ones |
| SJF | No | Lowest average wait | Needs burst times, long jobs can starve |
| SRT | Yes | Short jobs, low waits | Starvation, overhead |
| Round robin | Yes | Fair sharing, interactive systems | Context switch overhead |
| MLFQ | Yes | Mixed workloads | Complex |
Worked example
Task: which algorithm would suit (a) a multi-user server with many interactive users, (b) a batch system running long jobs overnight?
- (a) Round robin or multi-level feedback queues: every user gets a regular turn and quick responses, and short interactive jobs aren't held up.
- (b) FCFS or SJF: there's no need for quick responses, so the simplicity (or the low average waiting time of SJF) is fine, and the overhead of constant switching is avoided.
Exam tip: you may be asked to draw a Gantt chart and work out waiting times. Take care over the order and the time slices, and say whether the algorithm is pre-emptive. For each, give one advantage and one disadvantage.
Check yourself
What is the difference between pre-emptive and non-pre-emptive scheduling?
In pre-emptive scheduling the operating system can stop a running process to give the processor to another. In non-pre-emptive scheduling a process keeps the processor until it finishes.
What is a disadvantage of shortest job first?
Long jobs may starve if short jobs keep arriving, and the burst times must be known or estimated.
In round robin, what happens to a process that does not finish within its time slice?
It is pre-empted and goes to the back of the queue to wait for another turn.
How do multi-level feedback queues treat a process that uses its whole time slice?
It is moved down to a lower priority queue, because it looks like a long-running job.