Managing the CPU

The OS is used to run programs by clicking on an icon, selecting the program from a menu, or typing in an instruction at the command line.

When the OS runs a piece of software it has to find the program files on the storage drive, load them into main memory, and instruct the CPU to start executing the program from the beginning.

In each case, the OS performs the same sequence of steps:

  1. the program code is found on the storage drive
  2. a section of RAM is reserved for the program and space is allocated for the program's data
  3. the program code is copied from storage into the reserved space in the memory
  4. the CPU program counter is set to the memory location of the first instruction in the program, and execution begins
When a program is selected from the hard drive, the space in RAM is reserved. The program is then copied to RAM, and the CPU starts the program

Multitasking

The OS makes it possible to run several programs at once. Several programs can be stored in RAM at the same time, however only one program at a time is processed by the CPU. Programs can be in one of three states:

  • running
  • waiting
  • runnable

Only one process can be running at any one time. CPUs are extremely fast, so if a program is processed for even a short time it can do quite a lot. The OS decides the best way to swap between running, runnable and waiting processes. It controls which process is being executed by the CPU at any point in time, and shares access to the CPU between processes. The job of working out when to swap processes is known as scheduling.

Swapping happens so fast that it appears that all processes are running at the same time. When there are too many processes, or some of them are making the CPU work especially hard, it can look as though some or all of them have stopped.