Operating Systems · Module 2 — Processes & Threads
The PCB and what a context switch really costs
For every process, the kernel keeps one record: the Process Control Block, or PCB. It is the process's whole identity apart from its memory.
Sign in to track your score
The editor (PID 2210) is halfway through a calculation. Three values sit in CPU registers. The program counter points at instruction 0x4A18.
The 4 ms timer fires. The OS takes the core and gives it to the compiler.
Ninety milliseconds later the editor gets a core again — possibly a different core.
It carries on from 0x4A18 with those same three values, as if nothing had happened. Nothing was lost.
Where were they kept?
Why & what
The PCB. For every process, the kernel keeps one record: the Process Control Block, or PCB. It is the process's whole identity apart from its memory.
Inside it:
- PID — 2210. Which process this is.
- Program counter — 0x4A18. The exact next instruction.
- CPU registers — the half-finished arithmetic that was in flight.
- State — Ready, Running or Waiting, from Topic 2.2.
- Memory map — where its code, data, heap and stack live in real RAM.
- Open files — matrix.c, build.log, and anything else it had open.
- Scheduling info — priority and how much CPU it has already had.
The OS keeps all the PCBs in one big table. When people say "the OS keeps track of processes", the PCB table is literally what they mean.
The context switch. A context switch is taking the CPU from one process and giving it to another.
- Save the current process's registers and program counter into its PCB.
- Load the next process's registers and program counter out of its PCB.
That is it. The state lives in the PCB while the process is off the CPU.
The cost. On the Nova-14 a context switch takes about 5 microseconds. During those 5 microseconds no useful work happens at all. It is pure overhead.
Compare that with the 4 ms time slice: 5 µs of overhead per 4,000 µs of work is about 0.1%.
Acceptable. But shrink the time slice to 50 µs and the overhead becomes 10%. This trade-off is the heart of Module 3.
Work it through once, because interviewers ask for exactly this arithmetic. Useful work per turn is the slice length. Total time per turn is the slice plus the 5 µs switch. So with a 4 ms slice the CPU spends 4000 / 4005 of its time doing real work. With a 50 µs slice it spends 50 / 55 — about 91%. The same 5 µs, a very different answer.
How it works
The editor is swapped out for the compiler:
- Something forces the kernel in. Either the 4 ms timer interrupt fires, or the editor makes a blocking system call. There is no third way.
- The kernel saves PID 2210's context. Registers and program counter go into its PCB. Its state changes from Running to Ready.
- The scheduler picks the next process. It looks at the ready queue and chooses PID 2317. Module 3 is this step in detail.
- The kernel loads PID 2317's context. Registers and program counter come out of its PCB. It also switches the memory map so the compiler's addresses now point at the compiler's RAM.
- The CPU returns to user mode and the compiler resumes on its own next instruction.

Common confusion
"Context switch and mode switch are the same." They are not, and Module 1 flagged this deliberately.
- Mode switch: same process, different privilege level. The editor makes a system call and comes straight back. Cost: about 2 µs.
- Context switch: different process entirely. The editor stops, the compiler starts. Cost: about 5 µs.
A system call is a mode switch. It only becomes a context switch if the call blocks and the OS decides to run somebody else.
"The 5 µs is just copying registers." Copying about 16 registers is fast. The expensive part is switching the memory map, which throws away hardware caches that were full of the editor's data. The new process starts with cold caches and runs slower for a while. This hidden cost is real, and Module 6 explains exactly what gets thrown away.
Interview angle
"What is a PCB and what does it contain?" List six fields and say the one-line summary: the PCB is everything the OS needs to restart a process exactly where it stopped.
"What happens during a context switch?" Save into the old PCB, choose the next process, load from the new PCB. Then add the sentence that separates a good answer from an average one: it is pure overhead, so the time slice has to be long enough that the switching cost stays small.
- 1.
The editor makes a read system call that returns immediately from cache. What happened?
- 2.
Why is a very short time slice a bad idea?