SwadhyAI Notes

All Operating Systems notes

Start here

The PCB is the kernel’s row in its “spreadsheet” of all processes. Debugging tools (/proc, debuggers) read fields that ultimately come from here (directly or via stable summaries).

Process Control Block (PCB)

The PCB aggregates identity (PID, parent pointers), scheduling (priority, runqueue linkage), memory (pointer to address-space metadata), files (descriptor table), and signals (mask, disposition, pending bitmap). On Linux, think of task_struct plus structures it points to (mm_struct, files_struct, …).

By the end of this page you should be able to

  1. Name three categories of fields stored in the PCB.
  2. Explain why not every PCB field is saved on every context switch.
  3. Relate `/proc/[pid]/status` to PCB concepts.

Operating systems · Processes

Process control block (PCB)

Process Control Block = kernel’s struct for a process

TL;DR

The PCB (Linux `task_struct` + related structs) stores everything the kernel must preserve across preemption: scheduling, memory map pointers, file table, signals, credentials, accounting, and cgroup links, context switch = serialize relevant subset of this state.

Summary

Think of the PCB as the save game for a process: incomplete or corrupted state leads to crashes or privilege bugs. Reading kernel sources for `task_struct` is advanced but clarifies why certain operations are expensive.

Deeper narrative

The PCB is the anchor for everything debuggable: `/proc/[pid]/status`, `sched`, `maps`, `fd`. When you “pause” a container, you are freezing tasks whose PCB linkage still exists in the global process table (subject to namespaces).

Mechanism (step by step)

Treat each step as a sentence in the story you tell on a whiteboard.
1

Identity & hierarchy

PID/TGID, parent pointers, cgroup membership, namespace links.

2

CPU scheduling fields

Priority, vruntime, runqueue node, migration constraints.

3

Memory context

Pointer to `mm_struct`, RSS stats, lazy FPU state.

4

Files & signals

File descriptor table, signal mask/disposition, pending list.

Key insights

Each card is a compression of the lesson, use them as spaced-repetition prompts.

Splitting hot and cold fields

Kernels pack frequently accessed scheduling fields separately to improve cache behavior on the fast path.

Pointer validity rules

Many PCB pointers reference objects with their own locks, lock ordering matters to avoid AB-BA deadlocks in kernel code.

Namespace pointers

PID namespace, mount namespace, and network namespace pointers determine what “PID 1” or “localhost” means inside a container.

Security fields

Credentials (`cred`) include uid/gid/capabilities; LSM hooks consult them on each sensitive operation.

Did you know?

Trivia with teeth: the kind of fact that changes how you reason about a design.
`mm_struct` can be shared by threads (`clone(CLONE_VM)`) while keeping separate `task_struct`s.
File descriptors live in `files_struct` with reference counting-`dup` shares the open file description unless separated.
Kernel stacks are small (often 8–16 KiB on x86_64): deep recursion in syscalls is dangerous.

Common pitfalls

Where smart people still trip, read slowly.

Reading PCB fields without locks

Race with concurrent exec or multithreaded mutation, use official APIs or `ptrace` stop states.

Huge PCB == slow context switch?

Hot subset is per-CPU or lazily switched (FPU); not every field saved each time.

Glossary

mm_struct

Describes a virtual address space; threads in a process share one.

files_struct

Holds fd table; threads may share or copy on `clone` flags.

Runqueue node

Embeds task in scheduler red-black tree / per-CPU lists.

Interview follow-ups

  1. Which PCB fields must change on `execve` vs `fork`?
  2. How does `clone` reuse PCB layout for threads?
  3. Where is pending signal bitmap stored relative to the thread?

Callouts

Interview tip

If asked “what is stored in PCB?”, group answer: identity, CPU state snapshot location, memory, files, signals, security, accounting.

Walkthrough & deep dive

Left: practical “how would I use this?” threads. Right: slower, code-leaning explanations, open one panel at a time so the page does not feel like a wall of sameness.

Walkthrough

Memory exercise

List five PCB fields you would need to duplicate to implement `fork` correctly.

Deep dive

Where are saved registers?

On syscall/interrupt entry, CPU state is pushed onto the per-thread kernel stack; `thread_struct` may hold additional persistent registers for context switch.

Kernel doc pointer

Terminal
Read: Documentation/sched/* and include/linux/sched.h comments (online mirrors) for field intent.

Self-check

Misses here are cheap; misses in an interview are not. Treat wrong answers as a patch list for tomorrow.

1The PCB primarily lives in…

2Sharing `mm_struct` implies threads share…

3File descriptors are tracked via…