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: 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()ornew. 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:
- Save current hardware register values, program counter, and stack pointers of $P_0$ into its PCB.
- Update $P_0$'s state to Ready or Blocked.
- Load $P_1$'s saved register contents and memory map from its PCB.
- 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: 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 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: 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)andreceive(message). The kernel handles message buffering, queueing, and synchronization. Highly suitable for multi-computer distributed architectures (e.g. network sockets, RPC).