Part 5 of 8 · 2 min read
Multiprogramming
Early computers ran one job at a time. While that job waited for the disk or the keyboard, the processor sat idle. Multiprogramming fixes this. The OS keeps several programs in memory. When one must wait for input or output, the OS switches the processor to another, and the processor stays busy as long as some program is ready.
Think of a lawyer with several clients, who works this way. While one case waits on the court, the lawyer works on another.
In plain words: MOS gives a simple model. Say each program waits for I/O (input and output, such as disk reads) during a share p of its running time. With n programs in memory, the chance that every one is waiting at once is p to the power n. The processor is idle only then, so:
CPU utilisation = 1 − p^n
A worked example.
Here n is called the degree of multiprogramming. A worked example (made up to show the idea). Let p = 0.8, so each program waits 80 per cent of the time.
| n (programs) | p^n | CPU use = 1 − p^n |
|---|---|---|
| 1 | 0.8 | 20 per cent |
| 2 | 0.64 | 36 per cent |
| 3 | 0.512 | 48.8 per cent |
| 4 | 0.4096 | 59 per cent |
| 5 | 0.32768 | 67 per cent |
| 10 | 0.1074 | 89 per cent |
One program uses a fifth of the processor. Ten programs use almost nine tenths. The model is rough, because it treats the waits as independent and ignores the cost of switching. It still explains why running many programs at once is worth the effort. Multitasking takes the idea further: the processor switches so often that users can interact with every program while it runs.
Multiprogramming
Multiprogramming is the technique of keeping two or more programs in memory at the same time, so that when the running program has to wait (for input or output, for example) the processor is handed to another program instead of standing idle.
Example: a program waiting for a disk read pauses, and the OS runs a second program until the read finishes.
Check yourself: 1. With p = 0.5 and n = 3, what is the CPU utilisation? 2. Why does adding more programs help less and less? 3. What is the one situation in which the processor is idle in this model?