Processes
From a file on disk to a running program and back: what the operating system adds to make a process, how it lays out its memory, names it, schedules it and cleans up after it.
In 30 seconds
A program is a file of instructions on disk. A process is that program running: the operating system loads it into memory and lets the processor execute it.
Each process has its own private memory, the address space, divided into code, data, heap and stack, and a number, the process ID, that identifies it.
Because there are more processes than processor cores, each one moves between ready, running and waiting. Processes are created by other processes and release everything when they exit.
Processes, step by step
Simplified: one thread per process, and no virtual memory details. Real layouts also hold shared libraries, and a process is ended by a signal as often as by its own exit call.
- Step 1: A program is a file. A program, such as a text editor, is a file stored on disk: a list of instructions plus some starting data. While it only sits there it does nothing. It uses no memory, the processor ignores it, and it will be the same file tomorrow.
- Step 2: A process is a program running. When you start the editor, the operating system copies the program into memory, sets aside working memory for it and lets the processor begin executing its instructions. That running instance is a process. The file on disk stays as it was.
- Step 3: Its own address space. Each process gets a private range of memory addresses, its address space, split into four regions. Code holds the instructions. Data holds global variables. The heap is memory the program requests as it runs, and the stack tracks the function calls in progress. Heap and stack grow toward each other.
- Step 4: A process ID. The operating system gives every process a number, the process ID or PID, and uses it to track and control that process. Open the editor twice and you get two processes with two PIDs. They come from the same file, yet each has its own address space and cannot see the other’s data.
- Step 5: Process states. A processor core runs one process at a time, so every process moves between states. Ready means it could run and is queuing for the processor. Running means it has it. Waiting means it is paused until something arrives, such as a keypress or data from disk. The operating system switches between them many times a second.
- Step 6: Creating a process. Only a process can create a process. The creator is the parent and the new one is its child. On Unix-like systems it takes two calls: fork makes the child as a copy of the parent with a new PID, then exec replaces the child’s code and data with a program from disk. The shell starts the editor this way.
- Step 7: When a process ends. When the editor finishes, or crashes, its process is terminated. The operating system takes back all four memory regions and closes its files. The exit code goes to the parent; by convention 0 means success. Once the parent has read it, the PID is free to be reused. The program file is still on disk, unchanged.
Try it yourself
Learn this first
Spotted a mistake on this page?