Back

OS and Processes

February 2021

Operating Systems

A machine does work by executing instructions. The operating system (OS) is a piece of software that is in charge of making sure the machine executes instructions correctly and efficiently.

The OS achieves this by providing a host of things: CPU virtualization, memory virtualization, resource management, standard library (syscalls), file system, and concurrency management.

Processes

One of the most fundamental abstractions provided by the OS is the process. Before it is ready to do some work, a process has to first be created by the OS. This involves loading the program's instructions from disk to memory and allocating some additional memory for the program's stack and heap.

During its lifetime, a process may transition between several states. For instance, the OS may schedule or deschedule a process, causing it to transition to the running state or ready state, respectively. A process can also enter the blocked state, one example is when it initiates an I/O operation. Once a process has finished executing the entire program, the OS destroys the process and frees up all memory used by the process.

The OS keeps track of all running processes in a process list, along with some additional accounting information, such as which program is currently running, for instance. Furthermore, the OS also keeps track of information about each individual process. For example, the register context of a stopped process holds the contents of the registers just before the process transitions into the stop state.

Limited Direct Execution

The process abstraction itself does not solve the problem of running m programs on n processors, where m > n. This section discusses how the OS is able to virtualize the CPU efficiently while, at the same time, retaining control over the machine.

To ensure that a process does only the things it is supposed to, we introduce two modes that the processor can run in: user mode and kernel mode. Code that runs in user mode is restricted in what it can do. For instance, a piece of code running in user mode cannot issue I/O requests. In contrast, code that runs in kernel mode is free to do whatever it wants.

The OS (or kernel) always runs in kernel mode. Most processes in the process list run in user mode. A process that wants to perform some privileged operation must give up control and let the OS perform the operation on its behalf.

To do this, the process performs a system call which executes a trap instruction. This raises the privilege level and causes control to be passed over to the OS. When the OS has finished performing the requested operation, it executes a return-from-trap instruction, which lowers the privilege level back and returns control to the calling program.

For security reasons, a program performs a system call by specifying an identifer called the system-call number, as opposed to jumping to the address of the syscall. The syscall number is used to index a trap table, which specifies the location in memory of the trap handlers. The trap table is set up by the kernel at boot time.

Regaining Control

How can the OS regain control of the processor when another process is running on it? One approach is based on trust, that is, the OS assumes that any process running in user mode will issue a trap instruction eventually and pass control back to the OS. This approach is less than ideal. For example, the only fix to a program that is stuck in an infinite loop is to reboot the system.

A better approach is to use a timer interrupt. A timer interrupt is an interrupt that is raised in regular intervals. When the interrupt is raised, the current process halts, and control is passed back to the operating system, which runs a boot time-configured interrupt handler.

Context Switches

Anytime the OS regains control of the machine, it has to make the decision of whether to continue running the current process or switch to a new one. This decision is made by the scheduler. When the OS decides to switch to a new process, it has to perform a context switch.

Simply put, the OS performs a context switch by saving the values of some registers for the currently-executing process, and restore them for the new process.