Home/Learn/Operating Systems/Process Control Block (PCB)

Process Control Block (PCB)

Beginner
Processes & Threads

The PCB is a data structure maintained by the OS kernel that stores all information about a process — its state, registers, memory maps, open files, and scheduling metadata.

Overview

When the OS creates a process, it allocates a Process Control Block (PCB) — also called a task descriptor in Linux (task_struct). The PCB is the kernel's complete record of a process. It holds the process ID (PID), current state (running/ready/blocked), saved CPU register values for context switching, memory management info (page tables, base/limit registers), open file descriptors, accounting data (CPU time used), and I/O status. When the scheduler switches the CPU from one process to another, it saves the current process state into its PCB and loads the next process's PCB, restoring its registers and memory context.

PCB Fields and Their Purpose

The PCB is the "identity card" of a process. The OS uses it during context switches, scheduling decisions, resource reclamation at exit, and inter-process communication. In Linux, every process is represented by a task_struct which is several kilobytes of data.

Java — ProcessHandle API mirrors PCB fields
// PCB fields (conceptual mapping to Java/OS concepts):
// ┌──────────────────────────────────────────────┐
// │ Process ID (PID)      — unique identifier     │
// │ Process State         — NEW/READY/RUNNING/... │
// │ Program Counter (PC)  — next instruction addr │
// │ CPU Registers         — AX, BX, SP, etc.      │
// │ Memory Limits         — base + limit registers │
// │ Open File Table       — file descriptors       │
// │ I/O Status            — pending I/O requests   │
// │ Scheduling Info       — priority, burst time   │
// │ Accounting Info       — CPU time, wall time    │
// │ Parent PID (PPID)     — who created this       │
// └──────────────────────────────────────────────┘

// Reading process info from Java
long pid = ProcessHandle.current().pid();
ProcessHandle.Info info = ProcessHandle.current().info();
System.out.println("PID: " + pid);
System.out.println("Command: " + info.command().orElse("unknown"));
System.out.println("CPU time: " + info.totalCpuDuration().orElse(Duration.ZERO));

PCB and Context Switching

During a context switch, the OS saves the running process's CPU state (registers, program counter, stack pointer) into its PCB, then loads another process's PCB to restore its state. The PCB lives in kernel memory, inaccessible to user processes — this protects it from tampering.

Pseudocode — OS context switch using PCB
// Simulating what the OS does during context switch (pseudocode)
void contextSwitch(PCB current, PCB next) {
    // Step 1: save current process state
    current.programCounter = CPU.getPC();
    current.registers      = CPU.getAllRegisters();
    current.stackPointer   = CPU.getSP();
    current.state          = READY;

    // Step 2: load next process state
    CPU.setPC(next.programCounter);
    CPU.setAllRegisters(next.registers);
    CPU.setSP(next.stackPointer);
    next.state = RUNNING;

    // Step 3: switch memory map (expensive — TLB flush)
    MMU.loadPageTable(next.pageTableBase);
}
// In Linux: __switch_to() in arch/x86/kernel/process_64.c

Key Points to Remember

  • 1PCB (task_struct in Linux) is the kernel data structure representing a process.
  • 2It stores PID, state, saved registers, memory info, open files, and scheduling data.
  • 3During context switch, the OS saves the current PCB and loads the next one.
  • 4PCB resides in kernel memory — user processes cannot directly access it.
  • 5Java's ProcessHandle API provides read-only access to some PCB-equivalent fields.

Interview Questions

Sign in to ask Aria
1

What information does a Process Control Block contain?

EasyMicrosoft
2

What happens to the PCB during a context switch?

MediumAmazon
3

Why is the PCB stored in kernel space rather than user space?

MediumGoogle
4

How does the OS use the PCB when a process is terminated?

MediumUber

Ask Aria about Process Control Block (PCB)

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…