Modern operating systems have the ability to switch between processes. This is used in multiprogramming (also known as cooperative multitasking) and multitasking (or pre-emptive multitasking). The idea is that the CPU is currently concentrating on one process. But at some point, it needs to switch to another. To do so, it must save its register values (PC, IR, status flags, perhaps the AC and other registers) to memory and then restore the stored register values of another process before that process can be resumed (or started). The switching from one process to another is called a context switch.

In multitasking (pre-emptive), we add a timer. The timer is a piece of hardware which counts downward from some starting number of cycles until it reaches 0. For instance, it might start at 10,000. When it reaches 0, it triggers a context switch. In this way, no one process can monopolize the CPU. Instead, the CPU is switching off between all processes. While 10,000 sounds like a large number, keep in mind that a machine cycle may take place in as little as a billionth of a second (or less). We can control to some extent the number of cycles given to each process by changing the process' priority. A higher priority process will have more cycles allocated to it. The processes wait in a queue for the CPU to pay attention to them in turn. Usually, an operating system will switch off between processes using a round robin scheme.

This example comes from the textbook figure 4.5. This example is similar to that of figure 4.4 except for the added timer. We see that when the timer hits 0, an interrupt occurs causing the CPU to switch from P3 to P7. The timer is probably going to be initialized to a large value, but we start the animation at 4 just to keep it short.


NOTE: to replay, right click and select Play