Core Concepts of OS Process Control and CPU Scheduling

Process Management is one of the most critical responsibilities of an Operating System. While a program is just a passive collection of instructions stored on a disk, a process is an active, executing instance of that program.

1. Process Concepts & The Process Control Block

When a program is loaded into memory to execute, it becomes a process. A process is structurally divided into four distinct memory sections:

  • Text Section: Contains the compiled, executable machine code.
  • Data Section: Stores global and static variables.
  • Heap Section: Memory dynamically allocated during runtime (e.g., using malloc() in C or new in C++).
  • Stack Section: Temporary data storage for function parameters, return addresses, and local variables.

The Process Control Block (PCB)

To manage these processes, the OS creates a data structure for each one called a Process Control Block (PCB) (also known as a Task Control Block). The PCB acts as the identity card for a process, containing all the information needed to manage it.

Key attributes stored in a PCB include:

  • Process ID (PID): A unique identifier assigned by the OS.
  • Process State: The current condition of the process (e.g., New, Ready, Running).
  • Program Counter (PC): Contains the address of the next instruction to be executed.
  • CPU Registers: Information saved when the process switches out, including accumulators and index registers.
  • Memory-Management Information: Includes page tables or segment tables.
  • I/O Status Information: A list of open files and allocated I/O devices.

2. Process States

As a process executes, it changes status. The lifecycle of a process typically transitions through five classic states:

  1. New: The process is being created or loaded from disk.
  2. Ready: The process is in main memory, waiting to be assigned to a processor.
  3. Running: Instructions are currently being executed by the CPU.
  4. Waiting (Blocked): The process is waiting for an event to occur (e.g., I/O completion).
  5. Terminated: The process has finished execution, and the OS is cleaning up resources.

Context Switching

When the CPU switches from one process to another, the OS saves the current state of the old process in its PCB and loads the state of the new process. This is called a Context Switch, which represents pure overhead.

3. Operations on Processes

Operating systems provide mechanisms for process creation and termination.

Process Creation

A process can create several new processes, forming a tree structure. The creator is the parent process, and the new ones are child processes.

  • In UNIX-like systems, a child is created using the fork() system call, which duplicates the parent’s address space. The exec() system call is then used to load a new program.
  • The parent can wait for the child to finish (wait()) or run concurrently.

Process Termination

A process terminates when it finishes its final statement and calls exit().

  • Zombie Process: A process that has finished but remains in the process table because the parent hasn’t read its exit status.
  • Orphan Process: A process whose parent has terminated. The OS adopts it, usually assigning it to the root init or systemd process.

4. Inter-Process Communication (IPC)

Processes may be independent or cooperating. Cooperating processes require Inter-Process Communication (IPC) mechanisms to share data.

Levels of Scheduling

Scheduling levels control how processes move between memory, the CPU, and storage.

  • Long-Term Scheduler (Job Scheduler): Selects processes from the job pool on disk and loads them into memory.
  • Medium-Term Scheduler: Handles swapping processes between main memory and secondary storage.
  • Short-Term Scheduler (CPU Scheduler): Selects a process from the ready queue and allocates the CPU.

Scheduling Criteria

Performance metrics include:

  • CPU Utilization: Percentage of time the CPU is busy.
  • Throughput: Number of completed processes per unit of time.
  • Turnaround Time: Total time from submission to completion.
  • Waiting Time: Total time spent waiting in the ready queue.
  • Response Time: Time from submission until the first response.

Scheduling Algorithms

Algorithms can be non-preemptive or preemptive.

1. First-Come, First-Served (FCFS)

Processes are executed in arrival order. It is simple but prone to the Convoy Effect.

2. Shortest Job First (SJF)

Executes the process with the smallest next CPU burst. Preemptive SJF is known as Shortest Remaining Time First (SRTF).

3. Priority Scheduling

Allocates the CPU to the highest-priority process. Aging is used to prevent starvation of low-priority jobs.

4. Round Robin (RR)

Designed for time-sharing, each process gets a fixed time quantum.

5. Multilevel Queue Scheduling

Ready queues are partitioned based on process properties (e.g., foreground vs. background).

6. Multilevel Feedback Queue

Allows processes to move between queues based on CPU usage, providing high flexibility.

Multiple-Processor Scheduling

  • Asymmetric Multiprocessing: One master processor handles scheduling.
  • Symmetric Multiprocessing (SMP): Each processor schedules itself.
  • Processor Affinity: Processes prefer the processor they are currently running on to optimize cache usage.
  • Load Balancing: Uses Push and Pull migration to distribute workloads evenly.

Algorithm Evaluation

Deterministic Modeling

Uses a static, predetermined workload to calculate exact performance metrics for different algorithms.