Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 66 ยท Processes

Process States and the Process Control Block

Learn the five process states (new, ready, running, waiting, terminated), the transitions between them, the PCB fields and the process table, with a state machine simulator.

A process does not run continuously from start to finish. It runs for a while, waits for a disk or a keypress, waits for its turn on the CPU, and eventually ends. To manage this, the OS tracks the state of every process and keeps all the information about it in a record called the process control block (PCB).

Understanding states makes scheduling easy to follow, because a scheduler is really a program that moves processes between states. In this lesson you will learn the five-state model, what causes each transition, what the PCB contains, and how the OS keeps all PCBs in a process table.

Five process states

New Being created.
Ready Waiting for a CPU.
Running Executing now.
Waiting Blocked on I/O or an event.

Terminated is the fifth state, after exit.

1 / 2

The five-state model

stateDiagram-v2
    [*] --> New
    New --> Ready: admitted
    Ready --> Running: scheduler dispatch
    Running --> Ready: time slice over
    Running --> Waiting: I/O or event wait
    Waiting --> Ready: I/O or event completes
    Running --> Terminated: exit
    Terminated --> [*]

A process moves between states; only the transitions drawn here are legal.

  • New: the OS is creating the process (allocating a PCB and memory).
  • Ready: the process is loaded and could run, but no CPU is free. Ready processes sit in the ready queue.
  • Running: its instructions are executing on a CPU.
  • Waiting (blocked): it cannot continue until something happens, like a disk read finishing or a signal arriving.
  • Terminated: it has finished; the OS is cleaning up.

The transitions have a clear meaning:

Transition Cause
New to Ready OS admits the process
Ready to Running The scheduler picks it and the dispatcher gives it the CPU
Running to Ready Preemption: timer interrupt or a higher-priority process arrives
Running to Waiting The process requests I/O or waits for an event
Waiting to Ready The I/O finishes (an interrupt), so the process can compete for the CPU again
Running to Terminated It calls exit or crashes

Notice that a waiting process never goes straight to running. It must pass through ready and win a CPU through scheduling. Also, there is no transition from ready to waiting: a process only blocks when it is itself running and asks for something.

Many systems add two more states: suspended ready and suspended waiting, for processes swapped out of RAM to disk when memory is short.

A state machine in code

The simulator enforces the legal transitions. Try adding an illegal one (for example waiting to running) to see the error.

Python Process state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The process control block

The PCB (also called a task control block; in Linux it is the task_struct) is the kernel's record of one process. Everything the OS must remember to stop the process and later resume it exactly where it was lives here:

PCB field What it holds
Process ID and parent ID Unique identity and who created it
Process state New, ready, running, waiting, terminated
Program counter Address of the next instruction
CPU registers Contents of all registers when the process was last stopped
Scheduling information Priority, queue pointers, CPU time used
Memory-management information Page table or segment table, memory limits
Accounting information CPU time used, time limits, owner
I/O status Open files, devices allocated, pending I/O requests

The program counter and registers are the context of the process. Saving them into the PCB and restoring another process's values is the heart of context switching. The program counter itself is a CPU register that always holds the address of the next instruction to execute.

The process table

The OS keeps all PCBs in a process table (an array or linked structure). Its size limits how many processes can exist at once, which is why a "fork bomb" that creates processes endlessly can bring a system down. Queues such as the ready queue and the waiting queues are linked lists of PCBs; moving a process between states mostly means unlinking its PCB from one queue and linking it into another.

The code below builds a tiny process table with ready and wait queues made of PCBs, and a dispatcher that moves them around.

Python PCBs and queues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A process is waiting for a disk read to finish. When the read completes, which state does it enter?

    It becomes ready and must wait for the scheduler to give it a CPU; waiting never leads directly to running.
  2. 2. Which transition happens when a timer interrupt ends a process's time slice?

    The process still wants the CPU, so it goes back to the ready queue.
  3. 3. Which of these is stored in the PCB?

    The PCB holds the context needed to resume the process: program counter, registers, state, memory info and more.
  4. 4. What is the process table?

    The kernel keeps one PCB per process in the process table; its size limits the number of processes.

Summary

  • Processes cycle through new, ready, running, waiting and terminated states.
  • Only transitions caused by scheduling, preemption, I/O request/completion, admission and exit are legal.
  • The PCB stores PID, state, program counter, registers, scheduling, memory, accounting and I/O data.
  • The process table holds all PCBs; ready and waiting queues are lists of PCBs.

Next: Context Switching