Processes

Overview

A process is an instance of a program in execution. Each program in the system runs in the context of some process. The context consists of the state that the program needs to run correctly. Every process is uniquely identified with a process ID (PID).

States

The STAT header outputted by ps shows the state a Linux process is in. These are one of:

Additional attributes may be appended to the state. These include:

The zombie state is especially useful as it allows the parent process to examine the return code of the process before cleanup.

Limited Direct Execution

Processors typically have a mode bit set in a control register that characterizes the privileges the currently running process has. When the mode bit is set, the process is said to run in kernel mode. Otherwise the process is said to run in user mode. The only way for a process to change from user mode to kernel mode is via an exception.

Since processes are run on the CPU directly but in a "restricted" manner as designated by the current mode, the mechanism of program execution is called limited direct execution.

System Calls

A system call is a procedure-like interface between user programs and the kernel. They are traps, allowing lifts from user mode to kernel mode.

Context

The kernel maintains a context for each process containing the state needed to restart a preempted process. The C structure that stores a process's context is usually called the Process Control Block (PCB).

A process operates as if it has exclusive access to the CPU. The sequence of program counter values seen from the perspective of the process is called its logical control flow.

time-slices.png

Switching

In actuality, a process runs for a short time slice after which a context switch happens. This mechanism refers to:

  1. Saving the context of the current process,
  2. Restoring the saved context of a previously preempted process, and
  3. Passing control to the newly restored process.

context-switching.png

Note the hardware must save any register information it would otherwise clobber before switching to kernel code. The OS is responsible for saving all register state (and other context) when switching between two processes in its process list.

API

fork

The fork system call creates a new process. The child process spawned is a near exact copy of the calling process.The primary difference between the child and the parent is that the child returns a 0 from the fork system call whereas the parent returns the PID of the child.

This also means the child process gets a copy of all the parent's open file descriptors. Each FD in the child refers to the same underlying open file.

wait

The wait system call pauses execution until the first child process dies. The waitpid system call can be used instead to specify a specific process to wait on.

exec

The exec system call allows transforming the currently running program into a different one. It loads code and static data from an executable and overwrites the current code segment and static data with it. It also reinitializes the heap, stack, and other parts of the memory space of the program.

There are six variants of exec: execl, execle, execlp, execv, execve, and execvp.

Powered by Forestry.md