Labs ICT
โญ Pro Login

Process Control Block

How the OS keeps track of every running process.

The Process Control Block

Every process in the system needs to be tracked. The OS needs to know its state, its memory, its open files, its program counter โ€” everything. All of this information is stored in a data structure called the Process Control Block (PCB).

Think of the PCB as a process's resume and medical chart combined. When the OS switches from one process to another (called a context switch), it needs to save the current process's state and load the next one's. The PCB is where all that information lives.

What's in a PCB?

A typical PCB contains:


  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚         PROCESS CONTROL BLOCK           โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚  Process State      โ”‚ Running/Ready/... โ”‚
  โ”‚  Process ID (PID)   โ”‚ 1234              โ”‚
  โ”‚  Parent PID         โ”‚ 567               โ”‚
  โ”‚  Program Counter    โ”‚ 0x00402FA0        โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚  CPU Registers                               โ”‚
  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚
  โ”‚  โ”‚ Accum    โ”‚ Stack Ptrโ”‚ Gen Reg  โ”‚      โ”‚
  โ”‚  โ”‚ 0x000A   โ”‚ 0x7FFF   โ”‚ ...      โ”‚      โ”‚
  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚  Memory Management                          โ”‚
  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”‚
  โ”‚  โ”‚ Page Tab โ”‚ Base/Lim โ”‚                โ”‚
  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚  Accounting                                 โ”‚
  โ”‚  CPU Time: 45ms  โ”‚ Priority: 10         โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚  I/O Status                                โ”‚
  โ”‚  Open Files: [stdin, report.txt]          โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  • Process State โ€” Whether the process is new, ready, running, waiting, or terminated.
  • Program Counter โ€” The address of the next instruction to execute. This tells the CPU exactly where to pick up when the process resumes.
  • CPU Register Values โ€” The contents of all registers that were in use when the process was last paused. This includes the accumulator, stack pointer, and any general-purpose registers.
  • Memory Management Information โ€” Page tables, segment tables, or base/limit registers that define the process's memory space.
  • Accounting Information โ€” How much CPU time the process has used, time limits, process ID, parent process ID, and priority.
  • I/O Status Information โ€” List of open files, allocated I/O devices, and pending I/O requests.
  • Scheduling Information โ€” Priority level, pointers to scheduling queues, and any other data the scheduler needs.

Context Switching

When the scheduler decides to run a different process, a context switch occurs:

  1. The current process's state is saved into its PCB (registers, program counter, etc.).
  2. The CPU switches to the new process.
  3. The new process's state is loaded from its PCB into the CPU registers.
  4. The new process resumes execution from its saved program counter.

This happens extremely fast โ€” typically in microseconds. But it's not free. During a context switch, the CPU isn't doing useful work. It's just moving data around. That's why having too many processes can actually slow things down โ€” the CPU spends more time switching between them than doing real work.

PCB in Memory

The PCB is usually stored in kernel memory, in a linked list or array indexed by process ID. Some systems maintain a hash table for fast lookup. When the OS needs to find information about a process, it goes straight to the PCB โ€” it's the single source of truth for everything about that process.

On modern systems, there might be hundreds or thousands of PCBs in memory at any time, each one a snapshot of a process's entire existence.

๐Ÿงช Quick Quiz

What is a context switch?