Process Management - Fundamentals
Quick Reference (TL;DR)
Process: A program in execution. Consists of address space (memory) + execution state (registers, stack). Each process is isolated from others.
Process Lifecycle: New β Ready β Running β Waiting β Terminated. Transitions controlled by OS scheduler.
PCB (Process Control Block): Kernel data structure storing all process information (PID, state, registers, memory info, etc.).
1. Clear Definition
A process is an instance of a program in execution. It consists of:
- Address Space: The memory allocated to the process (code, data, stack, heap)
- Execution State: CPU registers, program counter, stack pointer, and other runtime information
π‘ Key Insight: A process is more than just codeβit's code + data + execution context. Multiple processes can run the same program (different instances).
2. Core Concepts
Process Definition (Address Space + Execution State)
Address Space:
βββββββββββββββββββββββββββ
β Stack (grows down) β β Local variables, function calls
βββββββββββββββββββββββββββ€
β (free) β
βββββββββββββββββββββββββββ€
β Heap (grows up) β β Dynamic memory (malloc)
βββββββββββββββββββββββββββ€
β Data (BSS, init) β β Global variables
βββββββββββββββββββββββββββ€
β Code (Text) β β Program instructions
βββββββββββββββββββββββββββ
Execution State:
- CPU Registers: General purpose, floating point, control
- Program Counter: Current instruction
- Stack Pointer: Top of stack
- Status Flags: CPU condition codes
- I/O State: Open files, network connections
Together: Address space defines "what" (memory), execution state defines "where" (current execution point).
Process Lifecycle (Realistic, Not Textbook)
States:
-
NEW: Process is being created
- Allocate PCB
- Allocate address space
- Load program into memory
- Initialize data structures
-
READY: Process is ready to run, waiting for CPU
- In ready queue
- All resources allocated except CPU
- Waiting for scheduler
-
RUNNING: Process is executing on CPU
- Currently using CPU
- Only one process per CPU core
- Can transition to:
- READY: Time quantum expires, preempted
- WAITING: Blocks (I/O, synchronization)
-
WAITING/BLOCKED: Process is waiting for event
- I/O operation
- Synchronization (mutex, semaphore)
- Child process termination
- Can only transition to READY when event occurs
-
TERMINATED: Process has finished
- Resources being deallocated
- PCB may remain (zombie) until parent reads exit status
- Eventually removed from system
State Transitions:
NEW
β
βΌ
READY ββββββββ
β β
β (scheduled) β (preempted/timeout)
βΌ β
RUNNING ββββββ
β
β (blocks)
βΌ
WAITING
β
β (event occurs)
βΌ
READY
β
β (exits)
βΌ
TERMINATED
PCB Contents (Exact)
Process Control Block (PCB) - Complete structure:
Identification:
- Process ID (PID)
- Parent Process ID (PPID)
- User ID (UID)
- Group ID (GID)
State Information:
- Process state (NEW, READY, RUNNING, WAITING, TERMINATED)
- Priority
- Scheduling information
CPU State (saved on context switch):
- Program counter (instruction pointer)
- CPU registers (general purpose, floating point)
- Stack pointer
- Status flags
Memory Management:
- Page table base address (CR3 on x86)
- Memory limits
- Memory segments (code, data, stack, heap)
- Open files memory mappings
I/O Information:
- List of open file descriptors
- I/O devices in use
- Pending I/O operations
Accounting:
- CPU time used
- Memory usage
- I/O statistics
- Start time
Scheduling:
- Priority
- Time quantum remaining
- Last run time
- Scheduling queue pointer
Example Size: ~1-4 KB per process (depends on OS)
Context Switch (Exact Steps)
When: Process A running β Process B running
Steps:
-
Save Process A's Context:
- Save CPU registers to A's PCB - Save stack pointer - Save program counter - Save floating point registers - Update A's state to READY (or WAITING) -
Update Scheduler:
- Remove A from running queue - Add A to appropriate queue (ready/blocked) - Update A's PCB state -
Select Next Process:
- Run scheduler algorithm - Choose process B from ready queue - Update B's state to RUNNING -
Restore Process B's Context:
- Load CPU registers from B's PCB - Load stack pointer - Load program counter - Load floating point registers - Load page table (CR3) - switch address space -
Flush Caches:
- Flush TLB (Translation Lookaside Buffer) - Invalidate instruction cache (if needed) - Memory barriers -
Resume Execution:
- Jump to saved program counter - Process B resumes from where it left off
Cost: ~1-10 microseconds
CPU Burst vs I/O Burst
CPU Burst: Period when process is using CPU
- Computation
- Data processing
- Algorithm execution
I/O Burst: Period when process is waiting for I/O
- Reading from disk
- Writing to network
- Waiting for user input
- Waiting for synchronization
Process Behavior:
CPU Burst β I/O Burst β CPU Burst β I/O Burst β ...
Types of Processes:
- CPU-bound: Long CPU bursts, short I/O bursts (scientific computing)
- I/O-bound: Short CPU bursts, long I/O bursts (web server, database)
Scheduling Implications:
- CPU-bound processes: Preemptive scheduling important
- I/O-bound processes: Responsiveness important
- Mixed: Balance both
3. Use Cases
Real-World Scenarios
- Web Browser: Each tab is a process (or thread)
- Operating System: Multiple processes (file manager, network, etc.)
- Server: Each client connection may be a process
- Development: Compiler, linker, debugger are separate processes
4. Advantages & Disadvantages
Process Model
Advantages:
β
Isolation: Processes can't access each other's memory
β
Security: OS enforces boundaries
β
Fault Tolerance: One process crash doesn't affect others
β
Parallelism: Multiple processes on multiple CPUs
Disadvantages:
β Overhead: Creating/switching processes is expensive
β Communication: IPC (Inter-Process Communication) is complex
β Resource Usage: Each process has own address space
5. Best Practices
- Use processes for isolation: When security/fault tolerance needed
- Minimize process creation: Expensive operation
- Use threads for parallelism: Within same process
- Understand process lifecycle: Know when processes block
6. Common Pitfalls
β οΈ Mistake: Confusing process with program (process = running program)
β οΈ Mistake: Thinking processes share memory (they don't, unless explicitly shared)
β οΈ Mistake: Not understanding process states
β οΈ Mistake: Creating too many processes (overhead)
7. Interview Tips
Common Questions:
- "What is a process?"
- "Explain process states and transitions."
- "What's in a PCB?"
- "What happens during a context switch?"
Key Points:
- Process = Address space + Execution state
- Five states: NEW, READY, RUNNING, WAITING, TERMINATED
- PCB stores all process information
- Context switch saves/restores state
8. Related Topics
- Threads (Topic 6): Lightweight processes
- CPU Scheduling (Topic 7): When processes run
- Memory Management (Topic 11): Process address space
- System Calls (Topic 4): How processes interact with OS
9. Visual Aids
Process Address Space
High Address
β
βΌ
ββββββββββββββββ
β Stack β β Local variables, grows down
β β β
β βΌ β
β (free) β
β β² β
β β β
β Heap β β Dynamic memory, grows up
ββββββββββββββββ€
β Data β β Global variables
ββββββββββββββββ€
β Code β β Program instructions
ββββββββββββββββ
Low Address
Process State Diagram
βββββββ
β NEW β
ββββ¬βββ
β
β (admitted)
βΌ
ββββββββββββ
β READY βββββββββββ
ββββββ¬ββββββ β
β β
β (scheduled) β (preempted)
βΌ β
ββββββββββββ β
β RUNNING ββββββββββ
ββββββ¬ββββββ
β
β (blocks)
βΌ
ββββββββββββ
β WAITING β
ββββββ¬ββββββ
β
β (event)
βΌ
ββββββββββββ
βTERMINATEDβ
ββββββββββββ
10. Quick Reference Summary
Process: Program in execution = Address space + Execution state
States: NEW β READY β RUNNING β WAITING β TERMINATED
PCB: Contains all process information (PID, state, registers, memory, I/O)
Context Switch: Save current, restore next (~1-10 ΞΌs)
CPU vs I/O Burst: Process alternates between computation and I/O