← 🧵 Processes, Threads & System Calls
Subtopic 2 of 4

1.2Context switch, modes and system calls

The idea

A context switch saves one process's CPU state and loads another's. A system call is how a user program asks the kernel for a privileged service, by trapping into kernel mode.

Think of it like this: A hotel guest asking the front desk: guests may not open the safe themselves, they ask staff who may.

Why it exists

Protection: user code must not touch devices or other processes' memory directly, so the hardware splits execution into user and kernel mode.

How it works

  1. The program loads the call number and arguments and executes a trap.
  2. Hardware sets the mode bit to kernel and jumps to the trap handler.
  3. The kernel validates the arguments, performs the service, stores the result.
  4. Return from trap: mode bit back to user, execution continues after the call.

Worked example: Does a system call always cause a context switch?

No. Entering the kernel is a mode switch. A context switch happens only if the kernel then runs a different process (for example because the call blocks on I/O).

Now play with it

  • →Step through the read() example and watch the mode register flip.
  • →Read the table: which operations are privileged?
CPU modeuser
Running codeapplication
Syscall table·
Return value·
  • The program calls read(): the library wrapper puts the system-call number and arguments in registers.
  • The wrapper executes a trap instruction (software interrupt). Hardware switches to kernel mode and jumps to a fixed kernel entry point.
  • The kernel uses the number to index the system-call table and finds the handler.
  • The kernel checks the arguments, does the work (maybe blocking the process for the disk), and stores the result.
  • Return from trap: mode bit back to user, execution resumes after the call with the result.

The kernel never trusts user pointers: it validates them. A context switch may happen while the call blocks, but entering the kernel itself is not a context switch.

OperationNeeds kernel mode?
Executing an ordinary instruction (add, load)no
Reading the system clockusually no
I/O instructions (in, out), haltyes (privileged)
Setting the timer or the mode bityes
Changing page tables, disabling interruptsyes
A system callenters kernel mode itself via a trap

Check yourself

Which of the following must run in kernel mode?

Remember: Mode switch ≠ context switch. Privileged instructions fault in user mode; a trap enters the kernel on purpose.
Exam notes (the dense version)

A context switch saves the state of one process in its PCB and loads another: pure overhead, no useful work. The CPU runs in user mode or kernel mode. A system call is a controlled entry into kernel mode (via a trap / software interrupt); the mode bit is set, the kernel runs, then returns. Privileged instructions (I/O, halt, changing the mode bit or page tables) fault in user mode. Interrupts are asynchronous (from devices); traps and exceptions are caused by the running instruction.

Previous year questions: Processes, Threads & System Calls (15)

PYQs are grouped by topic, so this is the full set for Processes, Threads & System Calls; try the ones that match this subtopic. Open all 15 on the topic page.