About this simulator
A context switch saves the running process's registers and program counter in its process control block (PCB) and loads the next process's saved state. The CPU does no useful work during the switch, so switching too often wastes time. The simulation shows each save and load and measures the overhead for the quantum and switch cost you choose.
How it works
- A process runs for at most one quantum, then the timer interrupt fires.
- The OS saves the program counter and registers into that process's PCB, then loads the next process's PCB.
- Efficiency . Try different quanta to see the trade-off.
Formulas used
Worked example
Two processes need 5 time units each. With quantum 2 and a switch cost of 1 the order is P1, P2, P1, P2, P1, P2 with 5 context switches. Total time is , so CPU efficiency is . Raising the quantum to 5 leaves a single switch and about 91% efficiency.
Enter these numbers in the simulator above to see every step.
When to use it
- Answering questions on context-switch overhead and CPU efficiency.
- Understanding why a very small time quantum hurts throughput.
- Seeing what a PCB stores and how the program counter is restored.
Frequently asked questions
What happens during a context switch?
The OS saves the current process's CPU state (program counter, registers) into its PCB and loads the saved state of the next process so it can resume exactly where it stopped.
How do you calculate CPU efficiency with context switching?
.
Why is context switching considered overhead?
Because no user process makes progress while the OS saves and loads state; the time is lost to bookkeeping.