› Module 2: Process Management › 2.3 Threads & Multithreading
M O D U L E

Module 2: Process Management

Processes, threads, states, context switching, and inter-process communication

2.1 Process Concept & Process Control Block (PCB)

A process is an active program in execution — the fundamental unit of computation in an operating system. While a program is a passive entity (an executable file sitting on secondary storage), a process is a dynamic entity possessing an allocated address space, a program counter, CPU register contents, and allocated system resources.

Figure 2.1: Structure of a Process Control Block (PCB) in Kernel Memory PROCESS (Active) Process ID (PID: 1042) Process State (Ready) Program Counter (0x7FFF) CPU Registers (EAX, ESP...) CPU Scheduling (Priority: 5) Memory Info (Page Tables) Accounting (CPU Time: 45ms) I/O Status (Open Files Table)

Figure 2.1: The Process Control Block (PCB) maintains all per-process state variables, allowing the OS scheduler to preempt and resume execution transparently.

Address Space of a Process

When an executable file is loaded into RAM, the OS partitions its virtual memory space into four distinct segments:

  • Text Segment: Compiled machine instructions executed by the CPU. Read-only to prevent self-modifying code.
  • Data Segment: Divided into initialized global/static variables and the BSS segment (uninitialized globals zeroed out at startup).
  • Heap: Dynamically allocated memory requested at runtime via malloc() or new. Grows upwards toward higher memory addresses.
  • Stack: Stores activation records for function calls, local parameters, and return addresses. Grows downwards toward lower memory addresses.

Context Switching Overhead

A Context Switch occurs when the CPU scheduler stops executing process $P_0$ and begins executing $P_1$. The kernel must:

  1. Save current hardware register values, program counter, and stack pointers of $P_0$ into its PCB.
  2. Update $P_0$'s state to Ready or Blocked.
  3. Load $P_1$'s saved register contents and memory map from its PCB.
  4. Flush or invalidate CPU caches, branch target buffers, and TLB entries.

Because the CPU performs zero useful user computation during this transition, context switching is pure operating system overhead.

2.2 Process States & State Transitions

As a process executes, its operational status changes dynamically based on scheduling events, resource availability, and hardware interrupts. The operating system tracks each process within a standardized five-state lifecycle model.

Figure 2.2: Complete Process State Transition Diagram New admitted Ready dispatch interrupt (quantum) Running exit Terminated Waiting / Blocked I/O or event wait I/O completion

Figure 2.2: Transitions are driven by CPU scheduling, timer quantum expirations, and I/O hardware interrupts.

The Three Levels of Schedulers

  • Long-Term Scheduler (Job Scheduler): Selects batch processes from disk pools and admits them into main memory (Ready state). Controls the degree of multiprogramming (number of processes currently in memory). Runs infrequently.
  • Short-Term Scheduler (CPU Scheduler): Selects from the in-memory Ready queue and allocates the CPU core to a process. Runs extremely fast (every 10–100ms); must be highly optimized.
  • Medium-Term Scheduler (Swapper): Temporarily removes (swaps out) partially executed processes from main memory to secondary storage during high memory pressure, and swaps them back in later when memory clears.

2.3 Threads & Multithreading

A thread is the basic unit of CPU execution within a process, consisting of a thread ID, program counter, CPU register set, and a private call stack. While traditional heavyweight processes possess only a single thread of control, modern operating systems support multithreaded processes where multiple concurrent threads share the underlying process resources.

Figure 2.3: Single-Threaded Processes vs. Multithreaded Process Memory Model Process 1 Separate Address Space Thread 1 PC, Registers Own Stack Process 2 Separate Address Space Thread 1 PC, Registers Own Stack Single Multithreaded Process Shared Code, Data, Heap, Files, PID across all threads Thread 1 PC, Regs Own Stack Thread 2 PC, Regs Own Stack Thread 3 PC, Regs Own Stack

Figure 2.3: Single-threaded processes require distinct address spaces; threads within a single process share the heap, globals, and open files while keeping independent execution stacks.

Benefits of Multithreading

  • High Responsiveness: A user interface thread remains interactive even if another worker thread is stalled awaiting network or disk I/O.
  • Economical Context Switching: Thread creation and thread switching consume an order of magnitude fewer CPU cycles than heavyweight process forks because memory page tables and address spaces do not need reloading.
  • Scalability on Multicore CPUs: Multiple threads can execute in parallel on physical CPU cores simultaneously.

User Threads vs. Kernel Threads

User Threads are managed above the kernel by a user-space thread runtime (e.g. green threads, fibers). They create and switch with near-zero overhead, but if one user thread issues a blocking system call, the entire process blocks. Kernel Threads are scheduled directly by the OS kernel; if one thread blocks, the kernel can immediately schedule another thread of the same process.

2.4 Inter-Process Communication (IPC)

Processes executing concurrently in an operating system may be either independent (cannot affect or be affected by other running processes) or cooperating. Cooperating processes require an Inter-Process Communication (IPC) mechanism for data exchange, computation speedup, and modular microservice execution.

Figure 2.4: Fundamental IPC Models — Shared Memory vs. Message Passing Shared Memory Model Fast direct read/write • user-managed sync Process A Process B Shared Memory (Kernel mapped region) Message Passing Model Kernel mediated • network & cluster friendly Process A Process B send(msg) recv(msg) Kernel Message Queue Buffer

Figure 2.4: Shared Memory provides maximum transfer throughput with zero kernel copies; Message Passing offers simpler semantics that scale across distributed networked nodes.

Comparison of IPC Paradigms

  • Shared Memory: The OS kernel establishes a mapping between the virtual address spaces of both processes to the same physical memory frames. Once initialized, communication occurs at hardware memory bus speeds without invoking the kernel. However, applications must explicitly synchronize access using semaphores to avoid race conditions.
  • Message Passing: Communicating processes use explicit system calls — send(message) and receive(message). The kernel handles message buffering, queueing, and synchronization. Highly suitable for multi-computer distributed architectures (e.g. network sockets, RPC).
MODULE COMPREHENSIVE QUIZ

Module 2 Final Quiz

Test your understanding across all 4 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 2 Final Quiz →
Yukti Mascot