Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Context of a Process

Lesson 22 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

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:

RegisterPurpose
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 RegistersData 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:

  1. Save the register context of the current process.
  2. Save any other volatile system-level context.
  3. Select the next process to run (scheduler).
  4. Restore the register and system-level context of the selected process.
  5. 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.