Skip to main content
Be Bitwise

Process Lifecycle & States

Process Management & Scheduling · OS Core Internals track

7 min read

Part ofSystems Programming Companion, week 9

In this module

The Life of a Process

A process doesn't simply appear and run continuously until it finishes. It moves through a series of well-defined states, each capturing what it's currently doing and whether it can use the CPU. The kernel tracks these transitions meticulously because scheduling decisions hang off them.

Process States in Linux

The state field in task_struct holds one of several values. The most important are:

  • TASK_RUNNING - the process is either currently executing on a CPU or sitting in the run queue ready to. Despite the name, it doesn't necessarily mean the process is running right this second. Only that it's runnable.
  • TASK_INTERRUPTIBLE - the process is sleeping, waiting for some event (like data arriving on a socket). It can be woken up by that event or by a signal.
  • TASK_UNINTERRUPTIBLE - the process is sleeping and can't be interrupted, even by signals. Used for hard I/O operations where the process has to wait for hardware. Processes stuck here show up as D in ps output.
  • EXIT_ZOMBIE - the process has terminated, but its parent hasn't yet collected its exit status. The process is dead, but its task_struct lingers so the parent can call wait().
Key Concept

A process is always in exactly one state. The kernel uses these labels to decide which processes are eligible to run, which are blocked on I/O, and which have finished but still need their exit status reaped.

State Transitions

Processes move between states in response to specific events:

Linux keeps one TASK_RUNNING value for "on a CPU now" and "waiting in the run queue", but it helps to draw them apart, because that's where preemption happens. The letter in brackets is what ps prints in its STAT column for each state.

Notice that execve() isn't a state change at all. The process keeps its PID and stays runnable; only the program it's running is replaced.

The path from creation to termination is the process lifecycle. A process is born via fork(), ping-pongs between running and sleeping many times during its life, and eventually exits.

Zombie Processes

When a process calls exit(), it doesn't vanish immediately. The kernel keeps its task_struct around with the exit status intact, waiting for the parent to retrieve it via wait() or waitpid(). Until that happens, the process is a zombie.

Zombies consume no CPU and almost no memory beyond their process table entry. The catch: if a parent never reaps its children, zombies accumulate. Each one holds a PID, and on systems with a finite PID space, a flood of zombies can block new processes from being created at all.

bash
# Spotting zombies in process listings
ps -eo pid,ppid,stat,comm | awk '$3 ~ /^Z/'
# A zombie shows state 'Z' and '<defunct>' next to its name in ps aux

Orphan Processes

What happens when a parent exits before its children? The children become orphans. The kernel doesn't leave them parentless. It re-parents them to the init process (PID 1), or on modern systemd-based systems, to a subreaper process.

The adoptive parent is responsible for calling wait() on these orphans when they eventually terminate, keeping them from becoming permanent zombies.

Creating Processes: fork and exec

On Unix-like systems, new processes are born in two steps:

  1. fork() clones the calling process exactly. The child gets a new PID but inherits the parent's memory layout, open files, and register state.
  2. exec() replaces the child's memory image with a new program. PID stays the same, but the code, data, and stack are replaced wholesale.

The two-step model is elegant: fork() handles duplication, exec() handles loading. Shells use this pattern for every command you type. Fork a child, then exec the requested program.

Worked Example: One Call, Two Returns

fork() is the only common function that returns twice: once in the parent and once in the child. The return value is how each copy knows which one it is.

c
pid_t pid = fork();
if (pid < 0) {
perror("fork"); /* no child was created */
} else if (pid == 0) {
/* child: fork() returned 0 */
execlp("ls", "ls", "-l", NULL);
perror("execlp"); /* only reached if exec failed */
_exit(127);
} else {
/* parent: fork() returned the child's PID */
int status;
waitpid(pid, &status, 0); /* reap it, so no zombie */
printf("child %d exited with %d\n", pid, WEXITSTATUS(status));
}

Trace the states. After fork(), both processes are TASK_RUNNING. The parent calls waitpid and drops into TASK_INTERRUPTIBLE, asleep until its child changes state. The child calls execlp, which keeps its PID but swaps in the ls program. When ls finishes, the child becomes a zombie for the brief moment before the parent wakes up, collects the exit status and lets the kernel free it. Remove the waitpid call and the child stays a zombie until the parent itself exits.

Two details that catch people out. A successful exec never returns, so any code after it only runs on failure. And the child uses _exit rather than exit, so it doesn't flush the parent's copied output buffers a second time, which leads to the next puzzle.

Predict: printf("hello "); fork(); printf("world\n"); with output going to a file. What ends up in the file?

hello world twice. When stdout is a file or a pipe, the C library buffers output and only writes it out later. "hello " is still sitting in the buffer, inside the process's memory, when fork() runs, so the child gets its own copy of the unflushed buffer. Each process then adds "world\n" and flushes on exit. Run it at a terminal instead and stdout is line-buffered, but "hello " has no newline, so you'll see the doubling there too. Call fflush(stdout) before fork() to avoid it.

Predict: how many lines does fork(); fork(); printf("hi\n"); print?

Four. The first fork() turns one process into two. Both of them run the second fork(), so each splits again: four processes, each printing once. A run of n forks gives 2n processes, which is why a fork() inside a loop gets out of hand so quickly.

Here it is with real PIDs. Each process prints its own PID and its parent's, so you can rebuild the family tree: the original process, two children and one grandchild. Every process reaps its own children before it exits, which keeps the parent PIDs accurate and leaves no zombies behind. The output order changes from run to run, because the scheduler decides who prints first.

Try itSketch the process tree from the PIDs, then delete the first fflush line and count how many times start is printed.
C (GCC 9.2.0)

Delete that fflush and you're back in the buffering puzzle above: the runner sends stdout to a file, so start is still sitting in the buffer when the processes split, and all four print it.

The zombie state is just as easy to catch. Here the child exits straight away while the parent sleeps for a second, so for that second the child is dead but unreaped. The parent reads the child's state from /proc/<pid>/stat before and after calling waitpid.

Try itRun it to see the child in state Z, then move the waitpid call above the first show_state and run it again.
C (GCC 9.2.0)

Why This Matters

Understanding process states pays off the moment you start debugging. A process stuck in TASK_UNINTERRUPTIBLE usually can't be killed even with SIGKILL: it's waiting for hardware I/O that simply has to complete. (Linux added a TASK_KILLABLE variant for many of these waits, so fatal signals can get through.) Zombie processes flag a parent that isn't reaping its children. These details earn their keep when you're analysing system behaviour, whether you're diagnosing a hung server or studying how a piece of malware manages its child processes.