The Process Lifecycle

[!NOTE] This module explores the core principles of The Process Lifecycle, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. What is a Process?

[!TIP] Program vs Process: A Program is a passive entity (like a recipe book on a shelf). A Process is an active entity (like a chef cooking the recipe).

A process is the fundamental unit of execution in an operating system. It includes:

  • Text Section: The compiled code (instructions).
  • Program Counter (PC): A pointer to the next instruction to execute.
  • Stack: Temporary data (function parameters, return addresses, local variables).
  • Data Section: Global variables.
  • Heap: Dynamically allocated memory.

2. The Process Control Block (PCB)

The OS manages thousands of processes. How does it keep track? It uses a massive table of structures called Process Control Blocks (PCBs).

Think of the PCB as the “Passport” or “Medical Record” of a process. It resides in Kernel Memory.

Field Description
PID Process ID (Unique Integer).
State New, Ready, Running, Waiting, Terminated.
PC Program Counter (Where did we stop?).
Registers CPU registers (Accumulator, Index, Stack Pointer) saved during a context switch.
Scheduling Info Priority, pointers to scheduling queues.
Memory Info Page tables, Segment tables (Base/Limit).
I/O Info List of open files (File Descriptors), I/O devices allocated.

[!NOTE] Context Switching: When the CPU switches from Process A to Process B, it must save the PCB of A and load the PCB of B. This is pure overhead (CPU does no useful work during the switch).


3. The 5-State Process Model

A process moves through distinct states during its life.

  1. New: The process is being created. The PCB is allocated, but code isn’t loaded into RAM yet.
  2. Ready: The process is in RAM, waiting for the CPU. It sits in the Ready Queue.
  3. Running: The process has the CPU and is executing instructions.
  4. Waiting (Blocked): The process is waiting for some event (I/O completion, signal). It cannot run even if the CPU is free!
  5. Terminated: The process has finished execution. PCB is deallocated.

Interactive: Process State Manager

Control the lifecycle of a process. Watch how the PCB state updates.

New
Ready
Running
Terminated
Waiting
Process Control Block (PID: 1042)
State: NEW
PC: 0x0000
CPU: Not Assigned

4. Special Processes: Zombies & Orphans

Unix systems have interesting parent-child relationships.

Zombie Process

A process that has finished execution (exit()) but has still has an entry in the process table.

  • Why? The parent hasn’t read its exit status yet using wait().
  • Analogy: A ghost waiting for its master to acknowledge its death.
  • Fix: Parent calls wait().

Orphan Process

A process whose parent has died.

  • Outcome: Adopted by init (PID 1) or systemd. init automatically waits for its children, cleaning up the mess.

5. Code Example: Creating Processes

In modern development, we rarely use raw fork()/exec(). We use higher-level abstractions.

```java import java.io.IOException; public class ProcessDemo { public static void main(String[] args) { // ProcessBuilder is the modern way to spawn processes ProcessBuilder pb = new ProcessBuilder("ls", "-l"); try { // Start the process Process process = pb.start(); // Wait for it to finish (like waitpid) int exitCode = process.waitFor(); System.out.println("Process exited with code: " + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ```