Skip to main content
Be Bitwise

Process Lifecycle

System Calls & Processes · Assembly & OS track

4 min read

In this module

What Is a Process?

A process is a running instance of a program. When you double-click an application or type a command in the terminal, the OS spins up a process. On disk, the program is inert — a static blob of instructions. A process is that program brought to life: loaded into memory, given resources, executing.

Each process gets its own isolated environment: a chunk of memory, a set of open files, its own register values. The OS tracks every process using a data structure usually called a process control block (PCB), and hands each one a unique process ID (PID).

Process States

A process is not always running. At any given moment, a process is in one of several states:

  • Ready - the process is loaded and waiting for CPU time
  • Running - the process is currently executing on a CPU core
  • Blocked (or waiting) - the process is waiting for something external, like a disk read to complete or a network packet to arrive
  • Terminated - the process has finished execution and is being cleaned up

The OS constantly shuffles processes between these states. A running process gets blocked the moment it asks the disk for something. A ready process gets promoted to running when the scheduler picks it. Constant churn.

Each arrow below is one of those moves, labelled with the event that causes it. Notice there's no arrow from Blocked straight to Running: when the disk read finishes, the process only becomes ready again and has to wait for the scheduler like everyone else. The last state, the zombie, is explained under termination at the end of this lesson.

One CPU Core, One Process

A single CPU core can only execute one process at a time. The illusion of multitasking comes from the OS rapidly switching between processes, fast enough that they all appear to be running at once. This trick is called time-sharing.

Creating Processes: fork and exec

On Unix-like systems, new processes are created using two syscalls that work together:

fork() clones the current process. The original becomes the parent, the clone is the child. They start out nearly identical (same code, same data), but they get different PIDs, and fork() returns different values to each: 0 to the child, the child's PID to the parent. The kernel doesn't copy the parent's memory up front: both share the same physical pages, marked copy-on-write, and a page is only duplicated when one of them writes to it.

exec() replaces the current process's code and data with a new program. After exec(), you're running entirely different code, but the PID stays the same.

The usual pattern: fork to make a child, then exec in the child to run a new program. That's how your shell launches every command you type.

Here's the whole dance in C. Before you run it, predict which lines the child prints and which the parent prints. The PIDs change every run, and the parent's and child's lines can interleave differently from one run to the next.

Try itRun it a few times: which lines can swap places, and which can't?
C (GCC 9.2.0)

The parent's last line always comes last, because waitpid() blocks until the child has finished. Everything before it is up to the scheduler.

Process Termination

A process ends when it calls exit(), returns from main(), or gets killed by a signal. The OS reclaims its memory and closes its files, but it keeps a small entry in the process table holding the exit status. Until the parent fetches that status with wait() or waitpid(), the dead process is a zombie. The kernel sends the parent a SIGCHLD signal to tell it a child has died. If the parent exits first, the orphaned child is adopted by init (PID 1) or a designated subreaper, which reaps it.