Process Management

Definition

A process can be thought of as a task or job in the system. Process Management is the set of OS functions that handle creation, scheduling, execution, context switching, synchronization, and termination of processes. The OS manages thousands of processes efficiently while maintaining system stability and fairness.

Process Control Block (PCB)

Also called Task Control Block (TCB), this is a data structure maintained by OS for every process.

What Information is Stored in PCB?

  1. Process ID (PID): Unique identifier for each process
  2. Process State: Current state (new, ready, running, waiting, terminated)
  3. Program Counter (PC): Memory address of next instruction to execute
  4. CPU Registers: Current values of all CPU registers (for context switching)
  5. Memory Information: Base address, limit, page tables, segment tables
  6. Priority: Scheduling priority level
  7. I/O Status: List of open files, I/O devices allocated
  8. Accounting Information: CPU time used, real time elapsed, memory used
  9. CPU Scheduling Information: Scheduling queue pointers, CPU burst time prediction
  10. Signals: Pending signals for the process
  11. Parent/Child Process: Process relationships
  12. Resource Pointers: Links to allocated resources

Why PCB?

When a process is interrupted, the OS saves its PCB. When resumed, OS restores PCB to restore exact state before interruption.

Process Creation

When is a Process Created?

  1. System Boot: System processes (init, shell, services)
  2. User Request: User clicks on application icon
  3. System Call: Running process creates child process using fork() or exec()
  4. Batch Job: Scheduled job starts automatically

Process Creation Steps

  1. Get Process ID: Assign unique PID
  2. Allocate Memory: Allocate code, data, heap, stack segments
  3. Initialize PCB: Create and fill Process Control Block with initial values
  4. Set Scheduling Parameters: Assign priority, scheduling queue
  5. Open Files: Open standard input, output, error streams
  6. Link to Parent: If child process, establish parent-child relationship
  7. Place in Queue: Place in ready queue for scheduling
  8. Return to Caller: Parent process gets child’s PID

Parent and Child Processes

When Parent Creates Child

  1. Fork() System Call (Unix/Linux):

    • Parent process calls fork()
    • Child process created as exact copy of parent
    • Both continue execution after fork() call
    • Child gets own PID, but same program counter position
    • Returns child’s PID to parent, 0 to child
  2. Exec() System Call:

    • Child calls exec() to load new program
    • Child’s memory replaced with new program
    • Child now runs different code than parent

Process Relationships

  • Parent Process: Process that creates another process
  • Child Process: Process created by another process
  • Orphan Process: Child whose parent terminates before child
  • Zombie Process: Child that finished but parent hasn’t retrieved status

Process Termination

When Process Terminates?

  1. Normal Exit: Process completes successfully, calls exit()
  2. Error Exit: Error condition, process terminates
  3. Fatal Error: Division by zero, memory access violation
  4. Killed by Another Process: Parent or system kills process

Termination Steps

  1. Close Files: All open files closed
  2. Release Memory: All allocated memory freed
  3. Deallocate PCB: Process Control Block deleted
  4. Adjust Relationships: If parent waits, return exit code; if not, become zombie briefly
  5. Update Parent: Parent notified of termination
  6. Cascade Termination: If process has children, may terminate them too

Context Switching

What is Context?

All information about a process stored in its PCB (CPU registers, program counter, memory info, etc.).

Context Switch Steps

  1. Save Context: OS saves CPU registers, PC, memory info to PCB of running process
  2. Update PCB: Mark process as not running, update its state
  3. Select Next: Scheduler selects next process from ready queue
  4. Load Context: Load PCB of selected process into CPU registers
  5. Start Execution: Jump to program counter address and resume

Context Switch Time

Pure Switching Time: 1-1000 microseconds (CPU-dependent)

Total Context Switch Time:

  • Time to save/restore context
  • Time for scheduler to select next process
  • Time for memory management (cache misses, TLB flushes)
  • Can be 10-100 microseconds on modern CPUs

Context Switch Overhead

During context switch, no user process executes. High context switch overhead can reduce efficiency:

  • Too many processes → Too many switches → Overhead increases
  • OS must balance number of processes vs. switching overhead

Key Operations

1. Process Creation

  • Uses fork() in Unix/Linux, CreateProcess() in Windows
  • Creates PCB and allocates resources

2. Process Scheduling

  • Scheduler selects next process from ready queue
  • Uses scheduling algorithm (FCFS, SJF, etc.)

3. Process Context Switch

  • Save state of current process
  • Load state of next process
  • Resume execution

4. Process Communication

  • Processes communicate via pipes, sockets, shared memory
  • OS provides inter-process communication mechanisms

5. Process Synchronization

  • Coordinate processes accessing shared resources
  • Use locks, semaphores, condition variables

Advantages of Process Isolation

  1. Protection: One process crash doesn’t affect others
  2. Security: One process can’t access another’s memory
  3. Stability: Faulty process can be terminated independently
  4. Concurrency: Multiple processes run truly independently
  5. Resource Management: Each process has own resources
  6. Privilege Separation: Different privilege levels possible

Disadvantages

  1. Overhead: Context switching between processes is expensive
  2. Communication Complexity: Inter-process communication harder than thread communication
  3. Memory Usage: Each process needs separate memory space
  4. Slow Creation: Creating new process slower than creating thread
  5. Slow Context Switch: Process context switching slower than thread switching

Summary

Process management is core OS responsibility. Through PCB, processes can be created, scheduled, switched, and terminated efficiently. Understanding process management is essential for understanding OS scheduling, synchronization, and resource allocation mechanisms.