Operating System Process Management

Process:

A process is an active instance of a program being executed by the operating system (OS). It is the fundamental unit of work in a computer system.

Key Components of a Process

  1. Program Code (Text Section)
    The process includes the executable instructions stored in memory as machine code. This text section is marked read-only to prevent accidental modification during execution while allowing multiple processes to safely share the same program code when running identical applications.
  2. Current Activity State
    At any moment, a process's execution status is captured through two key elements: the Program Counter (PC) that tracks the next instruction address, and CPU registers that temporarily hold computation data, memory addresses, and system flags. This combination precisely preserves the process's execution context.

  3. Process Stack
    The stack operates as a dynamic memory structure that systematically manages function calls and local data storage. It grows and shrinks automatically during program execution, storing return addresses for function calls, local variables specific to each function scope, and parameters passed between functions, while maintaining proper call/return sequencing.

  4. Data Section
    This memory region contains both initialized and uninitialized global/static variables that persist throughout the process lifecycle. Unlike temporary stack data, these variables maintain their values across function calls and different execution phases, providing stable storage for program-wide information.

  5. Heap Memory
    The heap serves as the process's dynamic memory pool, allowing runtime allocation and deallocation through explicit memory management calls. This flexible memory region enables data structures to grow as needed during execution, though proper manual management (allocation/deallocation) is required to prevent memory leaks or fragmentation issues.

Difference between Program and Process

Aspect Program Process
Definition Static set of instructions stored on disk (executable file) Dynamic instance of a program loaded into memory and executing
State No execution state (always the same) Has an execution state (New, Ready, Running, Waiting, Terminated)
Lifetime Exists permanently on storage Temporary: created when launched, destroyed when terminated
Memory Usage Not loaded into memory (stored on disk) Occupies memory space (text, data, stack, heap segments)
Resource Allocation No CPU, memory, or I/O resources assigned Actively uses CPU cycles, RAM, file handles, and other OS resources
Dependencies Self-contained file Requires OS support (scheduling, memory management)
Multi-Instance Capability Single file can be copied Multiple processes can run the same program independently
Control Structure No operating system control block Managed via Process Control Block (PCB) containing PID, state, registers, etc.
Execution Overhead No runtime overhead (just storage) Incurs CPU scheduling, context switching, and memory management overhead
Modification During Execution Cannot change while stored State changes continuously during execution (variables, PC, stack)
Example /usr/bin/firefox (disk file) When you launch Firefox, its process appears in Task Manager

Process States

Process States - Comprehensive Explanation

1. New State

When a process is first created through system calls like fork() or CreateProcess(), it enters the New state. During this initialization phase, the operating system performs several critical setup tasks: it generates a unique Process ID (PID) to identify the process, creates a Process Control Block (PCB) to store its execution context, allocates necessary memory space for the program's code and data segments, and establishes initial resource allocations such as file descriptors and security attributes. The process remains in this transient state until the OS completes all initialization procedures, after which it transitions to the Ready state. This state is essential for ensuring proper process isolation and resource provisioning before execution begins.

2. Ready State

A process in the Ready state has all necessary resources allocated and is prepared for immediate execution, awaiting only CPU time. The operating system maintains these processes in a ready queue, where they remain until the scheduler selects them for execution based on the system's scheduling algorithm. Processes may enter this state from either the New state (after successful initialization) or from the Running state (when preempted due to time quantum expiration or the arrival of a higher-priority process). The Ready state represents the process's potential to execute, with the OS periodically evaluating the ready queue to maintain optimal CPU utilization and system responsiveness.

3. Running State

The Running state represents active execution, where the process's instructions are being processed by the CPU. Only one process per processor core can be in this state at any given moment. While running, the process may transition to other states: it returns to Ready when its allocated time slice expires (in time-sharing systems), moves to Waiting when it requires I/O operations or synchronization with other processes, or progresses to Terminated upon completion. The OS continuously monitors running processes, maintaining their execution context in the PCB and enforcing memory protection boundaries to prevent unauthorized access to other processes' resources.

4. Waiting/Blocked State

Processes enter the Waiting state when they cannot continue execution until specific events occur, such as I/O completion, resource availability, or inter-process communication signals. The OS removes these processes from the ready queue and maintains them in separate wait queues organized by event type. This state is crucial for efficient resource management, as it allows the CPU to work on other processes while waiting for relatively slow operations like disk access or user input. When the awaited event occurs, the OS moves the process back to the Ready state, enabling it to resume competing for CPU time. The duration in this state varies significantly depending on the requested operation - from microseconds for memory access to seconds for user input or network communication.

5. Terminated State

Termination represents the final state in a process's lifecycle, entered either through normal completion or forced termination. In this state, the process has ceased execution but may retain its exit status information temporarily. The OS performs cleanup operations including releasing allocated memory, closing open files, and removing the PCB. However, in some systems (particularly Unix-like systems), a terminated process may remain in a Zombie state until its parent process acknowledges the termination through a wait() system call. This state ensures proper resource deallocation and allows parent processes to retrieve information about their child processes' execution results.

Process State Transitions

1. New → Ready Transition

When a program launches, the OS first creates the process in the New state, allocating memory and initializing its Process Control Block (PCB). Once setup completes, it transitions to Ready state, entering the scheduling queue where it awaits CPU allocation. This initialization ensures all necessary resources are prepared before execution begins.

2. Ready → Running Transition

The CPU scheduler selects ready processes based on priority and scheduling policy. When chosen, the OS:

  • Loads the process context into CPU registers

  • Sets the program counter

  • Updates the PCB status
    The process now actively executes instructions until it either completes, gets preempted, or requires I/O.

3. Running → Ready Transition

In time-sharing systems, processes automatically return to Ready state when their time quantum expires. The OS:

  • Saves all execution context (registers, PC)

  • Updates runtime statistics

  • Requeues the process
    This preemption enables fair CPU distribution across multiple processes.

4. Running → Waiting Transition

Processes enter Waiting state when they request unavailable resources. Common triggers include:

  • Disk/network I/O operations

  • Inter-process communication waits

  • Synchronization locks
    The OS maintains separate wait queues for different resource types, enabling efficient wake-up when conditions are met.

5. Waiting → Ready Transition

When the awaited event completes (I/O finishes, message arrives), the OS:

  • Removes the process from the wait queue

  • Marks it as executable

  • Places it in the ready queue
    The scheduler will then dispatch it normally based on priority.

6. Running → Terminated Transition

Processes terminate either:

  • Normally (via exit() call)

  • From errors (segmentation fault)

  • By external signals (kill command)
    The OS then:

  • Records exit status

  • Notifies the parent process

  • Releases all resources

  • Removes the PCB

Process Control Block (PCB) 

The Process Control Block (PCB) is a data structure maintained by the operating system for each process. It is sometimes referred to as the "identity card" of a process because it holds all the essential information the OS needs to manage and track that process. Every time a process is created, the OS generates a PCB for it, which stays in memory until the process is terminated.

The PCB contains several key attributes of a process. It includes the Process ID (PID), which uniquely identifies the process. It stores the process state, which can be new, ready, running, waiting, or terminated. It also holds the program counter, which points to the next instruction the process will execute. Additionally, the CPU registers and stack pointer are saved in the PCB so that the process can resume correctly after being interrupted or switched.

Apart from execution-related data, the PCB also contains memory management information such as base and limit registers, page tables, or segment tables that help the OS manage the process's memory. It stores I/O status information, including a list of I/O devices and files currently opened by the process. The scheduling information like priority, process queues, and CPU usage stats are used to decide how the CPU is allocated. Finally, accounting information tracks how long the process has been running and how many resources it has used.

During context switching, the OS uses the PCB to save the current process's state and load the state of the next process. Without the PCB, the OS would not be able to resume the correct execution of a process. Thus, the PCB plays a crucial role in process management, scheduling, memory handling, and resource tracking in a multiprogramming environment.