Chapter 05 of 38

Processes & Process States

When you double-click VS Code, a file called code.exe (or code on Linux) is sitting on your disk. That file by itself does nothing. The moment you open it, the OS loads it into memory, gives it a CPU to run on, and tracks everything about it. Now it is alive. It is a process.

The process is the most important idea in an operating system. CPU scheduling, synchronization, deadlocks and memory management are all about managing processes. In this chapter we will understand what a process is, how it looks in memory, and the states it moves through during its life, from creation to termination.


What Is a Process?

A process is a program in execution. It is the unit of work in a modern operating system: the program's code together with its current activity, which includes the program counter, CPU register values, stack, data, heap, and the resources (like open files) the OS has given it.

The difference between a program and a process is like the difference between a recipe and the act of cooking. The recipe (program) is written once and sits in a book (on disk). Cooking (the process) is happening right now, at a particular step, using particular ingredients. Two people can cook the same recipe at the same time in two kitchens: two processes from one program.

Program

Process

A passive entity: a file of instructions on disk

An active entity: those instructions being executed

Exists until deleted

Has a lifetime: created, runs, terminates

Needs only storage space

Needs CPU time, memory, open files, I/O devices

No program counter or state

Has a program counter, registers and a state

One program file...

...can create many processes (e.g. three open terminal windows)

Every process gets a unique number from the OS called the process ID (PID). On Linux, you can see the PID and other details of processes with commands like ps and top; on Windows, Task Manager shows the same idea in its Details tab.


A Process in Memory

When a program becomes a process, the OS gives it an address space, which is typically laid out in these sections:

  High addresses
  +-----------------------------+
  |           Stack             |  function calls: local variables,
  |             |               |  parameters, return addresses
  |             v  grows down   |
  |                             |
  |        (free space)         |
  |                             |
  |             ^  grows up     |
  |             |               |
  |           Heap              |  dynamic memory: malloc / new
  +-----------------------------+
  |           BSS               |  uninitialized global/static variables
  +-----------------------------+
  |           Data              |  initialized global/static variables
  +-----------------------------+
  |       Text (code)           |  the program's machine instructions
  +-----------------------------+
  Low addresses

Section

Contains

Size

Text

Compiled machine code; usually read-only and can be shared between processes running the same program

Fixed

Data

Global and static variables that have an initial value, e.g. int count = 42;

Fixed

BSS

Global and static variables without an initial value; the OS fills them with zeros

Fixed

Heap

Memory allocated at run time with malloc/new

Grows and shrinks

Stack

One frame per active function call: local variables, arguments, return address

Grows and shrinks

Why do the heap and stack grow towards each other? Because neither knows in advance how much it will need. Starting them at opposite ends of the free space lets either one use the space as needed. (In real 64-bit systems the gap is enormous, and shared libraries are also mapped in the middle, but the idea stays the same.)

See It in a Real Program

#include <stdio.h>
#include <stdlib.h>

int initialized_global = 42;   /* data segment */
int uninitialized_global;      /* BSS segment  */

int main(void) {
    int local = 7;                          /* stack */
    int *dynamic = malloc(sizeof *dynamic); /* heap  */
    if (dynamic == NULL) return 1;

    printf("text  (code)   main()               : %p\n", (void *)main);
    printf("data  (init)   initialized_global   : %p\n", (void *)&initialized_global);
    printf("bss   (uninit) uninitialized_global : %p\n", (void *)&uninitialized_global);
    printf("heap           *dynamic             : %p\n", (void *)dynamic);
    printf("stack          local                : %p\n", (void *)&local);

    free(dynamic);
    return 0;
}

Output from one run on x86-64 Linux (GCC):

text  (code)   main()               : 0x570e48e431a9
data  (init)   initialized_global   : 0x570e48e46010
bss   (uninit) uninitialized_global : 0x570e48e46018
heap           *dynamic             : 0x570e5f30c010
stack          local                : 0x7ffec893a9cc

The order matches the diagram: text is lowest, then data, then BSS, then the heap, and the stack is far above at a very high address. If you run it again, the exact numbers change, because Linux uses ASLR (Address Space Layout Randomization), which places sections at random addresses each run to make attacks harder. The order stays the same.

The size command shows how big the fixed sections are in the compiled file:

   text	   data	    bss	    dec	    hex	filename
   2121	    628	     12	   2761	    ac9	layout

There is no heap or stack in that output, because they do not exist in the file; they are created only when the program runs as a process.


Process States

A process does not run continuously from start to finish. It runs for a while, waits for the disk, waits for the CPU, runs again, and so on. The OS keeps track of what each process is currently doing using its state.

The Five-State Model

State

Meaning

New

The process is being created. The OS is setting up its data structures and memory, but it is not yet allowed to run.

Ready

The process has everything it needs to run and is waiting only for a CPU. It sits in the ready queue.

Running

Its instructions are being executed on a CPU right now.

Waiting (Blocked)

It cannot continue until some event happens, such as an I/O completion, a key press, or a signal. Even if a CPU is free, it cannot use it.

Terminated (Exit)

It has finished execution. The OS is cleaning up and collecting its exit status.

State Transition Diagram

                 admitted                          exit
     +-------+ ------------> +-------+  dispatch  +---------+ ------> +------------+
     |  NEW  |               | READY | ---------> | RUNNING |          | TERMINATED |
     +-------+               +-------+ <--------- +---------+          +------------+
                                 ^      interrupt /    |
                                 |      time slice     |
                                 |      expired        |
                                 |                     | I/O or event wait
                I/O or event     |                     | (e.g. read() from disk)
                completion       |     +---------+     |
                                 +---- | WAITING | <---+
                                       +---------+

The Transitions and Why They Happen

Transition

When it happens

New → Ready (admitted)

The OS has finished creating the process and accepts it for execution.

Ready → Running (dispatch)

The CPU scheduler picks this process, and the dispatcher gives it the CPU.

Running → Ready (preemption)

Its time slice expired (timer interrupt), or a higher-priority process became ready. It still could run, but someone else gets the CPU.

Running → Waiting (block)

It asked for something that is not available yet: read from disk, wait for user input, wait for a lock, sleep(), wait for a child process.

Waiting → Ready (wake up)

The event it was waiting for has happened (the disk read finished, a key was pressed).

Running → Terminated (exit)

It finished normally (exit()/return from main) or was killed (error, signal).

Two transitions that do not exist, and that are popular trick questions:

  • Waiting → Running directly: not allowed. When the event happens, the process goes to Ready and must wait for the scheduler like everyone else.

  • Ready → Waiting: not possible. A process can only start waiting for an event by asking for something, and it can only ask while it is running.

Walkthrough: A Process That Reads a File

Let us follow one process, P1, on a single-CPU machine with another process P2 also present:

Step  Event                                      P1 state      P2 state
----  -----------------------------------------  ------------  ---------
 1    User starts P1; OS creates it              New           Running
 2    OS finishes setup                          Ready         Running
 3    P2's time slice ends (timer interrupt)     Ready         Ready
 4    Scheduler picks P1                         Running       Ready
 5    P1 calls read() on a file not in memory    Waiting       Ready
 6    CPU is free, scheduler picks P2            Waiting       Running
 7    Disk finishes; interrupt wakes P1          Ready         Running
 8    P2's time slice ends                       Ready         Ready
 9    Scheduler picks P1                         Running       Ready
10    P1 finishes, calls exit()                  Terminated    Ready
11    Scheduler picks P2                         (gone)        Running

Notice step 7: even though P1's data is now ready, it does not run immediately. It goes back to the ready queue. Also note that on a single CPU, at most one process is Running at any instant. With n CPU cores, at most n processes can be running at once; any number can be ready or waiting.


The Seven-State Model: Suspended Processes

What if memory is full of processes that are all waiting for slow I/O? They take up RAM but cannot use the CPU. The OS can swap some of them out of RAM to disk, making room for others. Such a process is said to be suspended. Adding suspended states gives the seven-state model:

  • Blocked Suspend (Waiting Suspend): the process is waiting for an event and is swapped out to disk.

  • Ready Suspend: the process is ready to run (its event happened) but is still on disk; it must be brought back into memory before it can run.

                admit               dispatch
    NEW  ---------------->  READY  ----------->  RUNNING  -------> TERMINATED
     |                      ^   |  <-----------    |        exit
     |                      |   |    timeout       |
     |             activate |   | suspend          | event wait
     |                      |   v                  v
     +---- admit -----> READY SUSPEND           BLOCKED
       (memory full)           ^                 |    ^
                               |         suspend |    | activate
                  event occurs |                 v    |
                               +----------- BLOCKED SUSPEND

   (Not drawn, but still present from the five-state model:
    BLOCKED --event occurs--> READY)

The important transitions:

  • Blocked → Blocked Suspend: OS swaps out a waiting process to free memory.

  • Blocked Suspend → Ready Suspend: the event it waited for happens while it is still on disk.

  • Ready Suspend → Ready (activate): OS brings it back into memory.

  • Ready → Ready Suspend: rarely, the OS may even swap out a ready process to free a large block of memory.

  • New → Ready Suspend: a newly created process can be admitted directly to disk if memory is full.

The decision of which processes to swap out and bring back belongs to the medium-term scheduler. The long-term scheduler controls New → Ready (how many processes are admitted), and the short-term (CPU) scheduler controls Ready → Running. These are explained in detail in CPU Scheduling Basics. Modern desktop systems rely more on virtual memory and paging (see Virtual Memory & Demand Paging) than on swapping whole processes, but the concept of suspension is still a standard exam topic.


Process States on a Real Linux System

Linux uses its own letters for states, shown in the STAT column of ps:

Code

Linux name

Maps to textbook state

R

Running or runnable

Running or Ready (Linux does not separate them)

S

Interruptible sleep

Waiting (can be woken by a signal)

D

Uninterruptible sleep

Waiting, usually for disk I/O; signals cannot wake it

T

Stopped

Paused by a signal (e.g. Ctrl+Z or SIGSTOP)

Z

Zombie

Terminated, but the parent has not yet collected its exit status

We can create one process in each state with a small bash script:

#!/bin/bash
sleep 100 &  S=$!                          # waiting for a timer      -> S
( while :; do :; done ) &  R=$!            # always wants the CPU     -> R
sleep 100 &  T=$!                          # will be stopped          -> T
( sleep 0.2 & exec sleep 100 ) &  Z=$!     # its child exits, nobody waits -> Z
sleep 1
kill -STOP $T
ps -o pid,ppid,stat,comm -p $S,$R,$T,$Z --ppid $Z
kill -9 $S $R $T $Z

Real output:

    PID    PPID STAT COMMAND
   1458    1457 S+   sleep
   1459    1457 R+   bash
   1460    1457 T+   sleep
   1461    1457 S+   sleep
   1462    1461 Z+   sleep
  • 1458 (S): sleep is waiting for its timer, so it is sleeping (waiting).

  • 1459 (R): the infinite loop always wants the CPU, so it is running or ready.

  • 1460 (T): we sent it SIGSTOP, so it is stopped.

  • 1462 (Z): this sleep 0.2 has already finished, but its parent (1461) never calls wait() to collect its exit status, so it remains as a zombie.

The + just means the process is in the foreground process group of the terminal. PIDs will be different on your machine.

You can also read a single process's state from the /proc file system. Each process has a directory /proc/<PID>/, and its status file lists the state along with many other fields (here we keep only five of them):

sleep 100 &
sleep 0.2
grep -E '^(Name|State|Pid|PPid|Threads):' /proc/$!/status
Name:	sleep
State:	S (sleeping)
Pid:	777
PPid:	770
Threads:	1

Zombie and Orphan Processes (Quick Look)

  • Zombie: a process that has terminated, but whose entry still exists because its parent has not yet read its exit status with wait(). It uses no CPU and almost no memory, just a slot in the process table. Many zombies can fill that table.

  • Orphan: a process whose parent has terminated while it is still running. On Linux it is adopted by PID 1 (or a designated "subreaper" process), which later collects its exit status.

Both are covered properly with code in Process Creation (fork & exec).


CPU-Bound and I/O-Bound Processes

Processes behave differently while running:

  • An I/O-bound process spends most of its time waiting for I/O and uses the CPU in short bursts (a text editor, a web server waiting on the network). It moves between Running and Waiting very often.

  • A CPU-bound process spends most of its time computing and uses the CPU in long bursts (video encoding, training a machine-learning model, compiling). It mostly moves between Running and Ready.

A good system mixes both kinds, so the CPU and the I/O devices are both kept busy. This idea is the foundation of CPU scheduling.


Where Does the OS Keep All This Information?

For every process, the OS must remember its state, PID, program counter, registers, memory information, open files and more, so it can stop the process and later resume it exactly where it left off. This information is stored in a data structure called the Process Control Block (PCB), and the act of saving one process's state and loading another's is a context switch. Both are the subject of the next chapter, Process Control Block & Context Switching.


Common Mistakes

  • "A program and a process are the same." A program is a passive file; a process is a running instance with state. One program can be running as many processes.

  • Drawing Waiting → Running. A woken process always goes to Ready first.

  • Confusing Ready and Waiting. Ready means "give me a CPU and I can run". Waiting means "even with a free CPU, I cannot continue until an event happens".

  • Thinking a zombie is still running. A zombie has already terminated; it only has an exit status waiting to be collected.

  • Thinking a process in the Running state never stops. It can be preempted (back to Ready) at any timer interrupt.


Interview Questions

What is a process? How is it different from a program?

A process is a program in execution, with its own program counter, registers, stack, heap, data and OS resources. A program is a passive set of instructions on disk; a process is an active entity with a lifetime. One program can create many processes.

What are the sections of a process in memory?

Text (code), data (initialized globals), BSS (uninitialized globals), heap (dynamic allocation, grows up) and stack (function calls and local variables, grows down).

Explain the process states.

New (being created), Ready (waiting for CPU), Running (executing), Waiting/Blocked (waiting for an event like I/O), Terminated (finished). Transitions: admit, dispatch, preempt (timeout), block (I/O wait), wake up (event completion), exit.

Can a process go directly from Waiting to Running?

No. When the awaited event occurs, it moves to Ready and must be dispatched by the scheduler.

What are suspended states?

Ready Suspend and Blocked Suspend: the process has been swapped out of main memory to disk to free memory. The medium-term scheduler handles swapping out and in.

How many processes can be in the Running state?

At most one per CPU core at any instant. Any number can be Ready or Waiting.

What is a zombie process?

A process that has terminated but whose exit status has not yet been collected by its parent via wait(); it keeps only a process-table entry (shown as Z in ps).


Summary

  • A process is a program in execution; each one has a unique PID.

  • In memory a process has text, data, BSS, heap and stack; heap grows up, stack grows down; ASLR randomises the exact addresses.

  • Five-state model: New → Ready → Running → Waiting → Terminated, with transitions admit, dispatch, preempt, block, wake up and exit.

  • There is no Waiting → Running or Ready → Waiting transition.

  • The seven-state model adds Ready Suspend and Blocked Suspend for processes swapped out to disk.

  • Linux shows states as R, S, D, T, Z in ps.

  • Processes are CPU-bound or I/O-bound; mixing both keeps the system efficient.

Next, in Process Control Block & Context Switching, we will see how the OS stores each process's information and switches the CPU from one process to another.