Overview
Process management is the core function of an operating system’s kernel, responsible for creating, scheduling, and terminating processes. It ensures that the CPU is utilized efficiently and that multiple programs can run concurrently.
Core Concepts
- Process vs. Thread:
- Process: An executing instance of a program. It has its own isolated memory space (stack, heap, code).
- Thread: The smallest unit of execution within a process. Multiple threads share the same memory space of their parent process.
- Process Control Block (PCB): A data structure that stores all information about a process (PID, state, program counter, register values, open files).
- Process States:
- New: Process is being created.
- Ready: Process is in the queue, waiting for CPU time.
- Ready/Running: CPU is currently executing the process.
- Blocked/Waiting: Process is waiting for an event (e.g., I/O completion).
- Terminated: Process has finished execution.
- CPU Scheduling:
- First-Come, First-Served (FCFS): Simple queue, can lead to “convoy effect”.
- Shortest Job First (SJF): Prioritizes shorter tasks; can lead to starvation.
- Round Robin (RR): Each process gets a fixed time slice (quantum). Fair and used in time-sharing systems.
- Priority Scheduling: Processes with higher priority run first.
Code Examples
# Unix-like process management commands
ps aux | grep "my_app" # List processes
top # Real-time process monitor
kill -9 <PID> # Forcefully terminate a process
Use Cases
- Multitasking: Running a web browser, music player, and IDE simultaneously.
- Background Services: Running database engines or log collectors in the background.
- Resource Allocation: Ensuring a critical system process gets CPU priority over a background update.
Gotchas
- Context Switching: The overhead of saving and saving the state of one process and loading another. Frequent switching can degrade performance.
- Starvation: When a low-priority process never gets CPU time because higher-priority processes keep arriving.
