Edexcel International Advanced Level Computer Science, point 1.2.3
a. Process
- Definition: a process is a program that is being executed, together with its current state (PC, registers, memory and resources).
- Program or process? A program is a file of instructions stored on disk. A process is that program running in memory. Two windows of the same program are two processes.
- Each process has a process control block (PCB) holding its ID, state, saved registers and PC, priority, and memory details.
b. Process states
A process state is the current status of a process as it runs. A process is always in one state:
| State | Meaning |
|---|---|
| Ready | Loaded into memory and able to run, waiting in the ready queue for its turn on the CPU. A new process starts here |
| Running | Being executed by the CPU now |
| Waiting (or blocked) | Can't continue until an event happens, such as user input, a file being read or a device finishing |
| Terminating | Finished, or stopped by an error, and being removed from memory so its resources are freed |
Transitions:
- Ready to Running: the CPU is available, and the scheduler gives it to the process.
- Running to Ready: its time slice expires (or a higher-priority process needs the CPU).
- Running to Waiting: it is blocked, waiting for I/O or another event.
- Waiting to Ready: it is unblocked when the event happens. It doesn't go straight to Running: it waits in the ready queue again.
- Running to Terminating: it has completed, or an error has occurred.
c. Scheduling
- Definition: scheduling is deciding which ready process gets the CPU next, and for how long. The part of the OS that does this is the scheduler.
- How it works:
- Processes that are ready to run are placed in a ready queue.
- The scheduler selects a process from the queue and gives it the CPU.
- The process runs, then either completes or goes back to the queue if it needs more CPU time (or has to wait).
- Aims:
- Keep the CPU busy (high utilisation).
- Fairness: no process is starved of time.
- Short waiting and turnaround times.
- Good response time for interactive users.
- Handle priorities.
- Terms:
- Burst time: CPU time a process needs.
- Waiting time: time spent ready but not running.
- Turnaround time: from arrival to completion (waiting + burst).
- Pre-emptive: the OS can take the CPU away from a running process. Non-pre-emptive: a process keeps the CPU until it finishes or blocks.
d. Scheduling algorithms
First-come, first-served (FCFS)
- How: processes run in the order they arrive, each to completion. A queue.
- Benefits: simple and fair in order, with no starvation.
- Drawbacks: a long process at the front makes all the short ones wait (the convoy effect), so the average waiting time can be high. It can't prioritise urgent tasks, since every process is treated the same. Poor for interactive use. Non-pre-emptive.
Shortest job first (SJF)
- How: the ready process with the shortest burst time runs next.
- Benefits: gives the lowest average waiting time of these methods, and short jobs finish quickly.
- Drawbacks: the burst time has to be known or estimated, and long processes can starve if short ones keep arriving. Sorting the queue by burst time adds overhead.
- Diagram: the same as FCFS above, but the ready queue is kept in order of burst time, shortest at the front.
Round robin (RR)
- How: each process gets a fixed time slice (quantum), in turn, in a circular queue. If it hasn't finished when the quantum ends, it goes to the back of the queue. Pre-emptive.
- Benefits: fair, good response time, no starvation. Suits time-sharing systems.
- Drawbacks: context-switching overhead. If the quantum is too small, most time is wasted switching. If it's too big, it behaves like FCFS. Average turnaround can be longer than SJF, and long processes need many rounds to finish. Every process gets the same time slice, so urgent tasks can't be prioritised.
Multi-level queue
- How: the ready processes are split into several queues by type or priority (for example system processes, interactive, batch). Each queue can have its own scheduling algorithm (round robin for interactive, FCFS for batch). The queues themselves are served by priority, or are given a share of CPU time.
- Benefits: different kinds of work get suitable treatment, and important work is served first.
- Drawbacks: more complex, and low-priority queues can starve if higher ones are always busy. A process is fixed in its queue.
Worked example
Task: three processes arrive together, in the order P1, P2, P3, with burst times 6, 3 and 1 units. Compare the average waiting time for FCFS, SJF and round robin with a quantum of 2.
FCFS: P1 runs 0 to 6, P2 6 to 9, P3 9 to 10.
| P1 | P2 | P3 | |
|---|---|---|---|
| Waiting time | 0 | 6 | 9 |
Average =
SJF: P3 runs 0 to 1, P2 1 to 4, P1 4 to 10.
| P1 | P2 | P3 | |
|---|---|---|---|
| Waiting time | 4 | 1 | 0 |
Average =
Round robin, quantum 2:
| Slice | 0 to 2 | 2 to 4 | 4 to 5 | 5 to 7 | 7 to 8 | 8 to 10 |
|---|---|---|---|---|---|---|
| Runs | P1 | P2 | P3 | P1 | P2 | P1 |
| Time left afterwards | 4 | 1 | 0 | 2 | 0 | 0 |
Completion times: P3 at 5, P2 at 8, P1 at 10. Waiting time = completion burst.
| P1 | P2 | P3 | |
|---|---|---|---|
| Waiting time |
Average =
| Algorithm | Average waiting time |
|---|---|
| FCFS | 5.0 |
| SJF | 1.67 (lowest) |
| Round robin | 4.33 |
SJF is best for average waiting time, but round robin is fairer and more responsive, because no process waits long for a first turn.
Exam tip: you may be asked to complete a diagram of the process states or of a scheduling algorithm. Learn the arrows: Waiting goes back to Ready, never straight to Running, and Running goes to Terminating when the process completes or has an error. For each algorithm, give how it chooses, one benefit and one drawback. When working out waiting times, draw a time line and check that the total adds up.
Check yourself
What is the difference between a program and a process?
A program is a file of instructions stored on disk. A process is a program that is being executed, with its current state and resources.
Describe what moves a process from Running to Waiting, and from Waiting to Ready
It moves to Waiting when it is blocked, for example waiting for I/O. It moves back to Ready when it is unblocked, because the event it was waiting for has happened, such as the I/O finishing.
Give one drawback of first-come, first-served scheduling
A long process at the front makes short processes wait a long time (the convoy effect), so the average waiting time can be high.
Why can shortest job first cause starvation?
A long process may never be chosen if shorter processes keep arriving.
What happens if the time quantum in round robin is too small?
Too much time is spent on context switching, so less useful work is done.