Processes and Their Life Cycle
Learn what a process is, how its memory is laid out, what the process control block stores, and how fork, exec and wait create and manage processes.
Welcome back!
Step 1 of 13Learn
A program in action
A program is a file: machine code and data sitting on disk, doing nothing. A process is a program in execution. Double-click the same text editor icon twice and you get two processes from one program, each with its own open document, its own memory and its own position in the code.
The process is the OS's basic unit of isolation and resource ownership. Each process has:
- A private address space: the range of memory addresses it can use, invisible to other processes.
- One or more threads of execution, each with its own program counter, registers and stack.
- Resources the kernel tracks on its behalf: open files, network connections, a current working directory, a user identity used for permission checks.
The address space of a typical process is laid out in regions:
- Text (code): the program's machine instructions, usually read-only.
- Data: global and static variables.
- Heap: memory allocated at run time with
mallocin C ornewin Java and C++. It grows upward as the program asks for more. - Stack: local variables, function arguments and return addresses. Each function call pushes a frame; each return pops it. It grows downward from high addresses.
The heap and the stack grow toward each other through a large unused gap. Recurse without end and the stack eventually hits its size limit, producing the famous stack overflow crash.
Step 2 of 13Practice · Match the pairs
Match each part of a process's memory to what it holds.
Tap an answer, then tap the place where it belongs (or drag it there).
- Text segment
- Data segment
- Heap
- Stack
Step 3 of 13Diagram
A process's address space, high to low addresses
Tap a shape with to see more.
- Stack
Function calls and local variables; grows downward.
- Free space
Room for the stack and heap to grow toward each other.
- Heap
Memory from malloc or new; grows upward.
- Data
Global and static variables.
- Code (text)
The machine instructions, usually read-only.
Step 4 of 13Learn
The process control block and process states
For each process, the kernel keeps a record called the process control block (PCB); Linux calls it task_struct. It holds everything the kernel needs to manage and resume the process:
- Process ID (PID) and parent's PID
- Current state
- Saved CPU registers and program counter
- Memory-management information, such as a pointer to its page table
- Open file table, user and group IDs, scheduling priority, accounting data
A process moves through a set of states:
- New: being created.
- Ready: able to run, waiting for a CPU core.
- Running: its instructions are executing on a core right now. On a 4-core machine, at most 4 threads are running at any instant.
- Waiting (blocked): it can't continue until some event happens, such as a disk read finishing, a network packet arriving or a key being pressed.
- Terminated: finished, waiting for its parent to collect its exit status.
The transitions tell a story. The scheduler moves a process from ready to running. A timer interrupt can push it back from running to ready. A system call that must wait for I/O moves it from running to waiting. When the I/O completes, an interrupt moves it from waiting to ready, not straight back to running, because a core might be busy with someone else.
Step 5 of 13Practice · What would you do?
What would you do?
A process is running when it calls read() on a file whose data isn't in memory, so the kernel must fetch it from the SSD. Which state does the process enter, and what happens when the data arrives?
Step 6 of 13Practice · Diagram
The core process states
New processes enter at Ready; finished ones leave from Running. Tap each state.
The disk data a blocked process needed has arrived. Which state does it move to?
Tap the shape that answers the question, then press Check.
- Ready
Could run now; waiting for a core.
- Running
Executing on a core. A timer interrupt sends it back to Ready.
- Waiting (blocked)
Needs an event, such as disk data or a keypress. When it arrives, back to Ready, not straight to Running.
Step 7 of 13Learn
Context switches
When the kernel stops one process and starts another on the same core, it performs a context switch:
- Save the current process's registers, program counter and stack pointer into its PCB.
- Update its state, for example from running to ready or waiting.
- Choose the next process to run (the scheduler's job).
- Switch the memory mappings to the new process's address space.
- Load the new process's saved registers from its PCB and jump back into its code.
To the processes, nothing happened: each resumes exactly where it stopped. But context switches have a cost. The direct work takes on the order of a few microseconds, and the indirect cost is often bigger: the new process finds the CPU caches and the address translation cache full of the old process's data, so its first memory accesses are slow.
This creates a tension the scheduler must balance. Switch often and the system feels responsive, because every process gets a turn quickly, but more time is lost to overhead. Switch rarely and less time is wasted, but interactive programs feel sluggish. A busy desktop typically performs thousands of context switches per second.
Step 8 of 13Practice · Work it out
A core performs 2,000 context switches per second, and each costs about 5 microseconds of direct overhead. What percentage of the core's time goes to that overhead?
Step 9 of 13Learn
Creating processes: fork, exec and wait
On Unix-like systems such as Linux and macOS, new processes are created in two steps.
fork() makes a near-exact copy of the calling process. After it returns, there are two processes running the same code from the same point. The only difference is the return value: in the child, fork returns 0; in the parent, it returns the child's PID. Programs use that value to choose different paths.
exec() (in practice one of the execve family) replaces the current process's program with a new one. The PID stays the same, but the code, data, heap and stack are thrown away and the new program starts from its beginning.
wait() lets a parent pause until a child finishes and collect its exit status.
A shell runs a command like ls using exactly this pattern: fork a child, have the child exec ls, and have the parent wait. Copying a whole address space just to discard it might seem wasteful, so kernels use copy-on-write: parent and child share the same physical memory pages until one of them writes to a page, and only then is that page copied.
Two oddities follow. A child that has exited but hasn't been waited for is a zombie: it uses no CPU or memory but keeps its PCB entry. A child whose parent dies first is an orphan and is adopted by the init process, which waits for it. Windows takes a different approach: a single CreateProcess call creates a process and loads a program in one step.
Step 10 of 13Diagram
Two fork() calls in a row
Every existing process runs each fork, so the count doubles per call. Tap the nodes.
Tap a shape with to see more.
- Original (P)
- Child of 1st fork (C1)
Created by fork #1; it then also runs fork #2.
- P's child from 2nd fork
- C1's child from 2nd fork
Step 11 of 13Practice · Work it out
A program starts as one process and executes three fork() calls in a row, with no if statements, so every process runs every remaining fork. How many processes exist after all forks complete, including the original?
Step 12 of 13Note
Step 13 of 13Practice · Fill in the blanks
Fill in the blanks
After a successful fork(), the call returns in the child process and the child's in the parent process.
Lesson complete!