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:
- The current process's state is saved into its PCB (registers, program counter, etc.).
- The CPU switches to the new process.
- The new process's state is loaded from its PCB into the CPU registers.
- 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.