Introduction
The context of a process is the complete set of information the kernel needs to describe the process and resume its execution after it has been suspended. When the kernel switches from one process to another (context switch), it saves the context of the old process and restores the context of the new one.
Three Levels of Context
The UNIX process context consists of three layers:
---
1. User-Level Context
This is the context visible to the user program:
- Text Segment (Code):
- The machine instructions of the program.
- Read-only and shareable (multiple processes running the same program can share the text segment).
- Data Segment:
- Initialized Data: Global and static variables with explicit initial values.
- Uninitialized Data (BSS): Global and static variables without initial values (initialized to 0 by default).
- Heap: Dynamically allocated memory (
malloc()).
- Stack Segment:
- Stores local variables, function parameters, and return addresses.
- Grows downward in memory (toward lower addresses).
- Shared Memory: (if attached)
- Memory segments shared between processes for IPC.
---
2. Register-Level Context
The hardware register values at the time the process is running:
| Register | Purpose |
|---|---|
| Program Counter (PC) | Address of the next instruction to execute |
| Processor Status Register (PSR) | Condition codes, mode (user/kernel), interrupt level |
| Stack Pointer (SP) | Points to the top of the current stack |
| General Purpose Registers | Data used by the executing instructions |
- During a context switch, all register values are saved in the process's context and restored when it resumes.
---
3. System-Level Context
Kernel-maintained information about the process:
a) Static Part (does not change during execution):
- Process Table Entry — PID, state, priority, signals.
- U-Area — Open files, signal handlers, current directory.
- Per-Process Region Table — Memory region mappings.
b) Dynamic Part (changes during execution):
- Kernel Stack — Each process has a kernel stack for executing system calls.
- Saved Register Context — Register values saved during context switch.
- Context Layers — The kernel can stack multiple context layers when handling interrupts during system calls.
Context Switch
A context switch occurs when the kernel suspends one process and resumes another.
When does a context switch occur?
- A process makes a system call and sleeps (e.g., waiting for I/O).
- A process is preempted (a higher-priority process becomes ready).
- A process exits.
- An interrupt causes the kernel to reschedule.
Steps in a Context Switch:
- Save the register context of the current process.
- Save any other volatile system-level context.
- Select the next process to run (scheduler).
- Restore the register and system-level context of the selected process.
- Resume execution of the new process.
Cost of Context Switching:
- Context switches are expensive — they involve saving/restoring registers, flushing caches, and switching memory maps.
- The kernel tries to minimize unnecessary context switches.
Context Layers
- A process can accumulate multiple context layers when it is interrupted while in kernel mode.
- Example: A process is executing a system call → an interrupt occurs → another interrupt occurs → each creates a new context layer.
- When each interrupt is handled, the corresponding context layer is removed (LIFO — Last In, First Out).
Context Layer Stack:
┌──────────────────────┐
│ Interrupt Handler 2 │ ← Most recent (top)
├──────────────────────┤
│ Interrupt Handler 1 │
├──────────────────────┤
│ System Call Context │
├──────────────────────┤
│ User-Level Context │ ← Base (bottom)
└──────────────────────┘
Summary
- Process context = User-Level + Register-Level + System-Level context.
- A context switch saves one process's context and restores another's.
- Context switches are triggered by sleep, preemption, exit, or interrupts.
- Context layers allow the kernel to handle nested interrupts during system calls.