Process Management
Short Notes
A process is a program in execution.
Process States
- New: Created.
- Ready: Waiting in main memory for CPU.
- Running: Executing on CPU.
- Waiting: Blocked (waiting for I/O).
- Terminated: Finished.
- Suspended Ready/Wait: Moved to secondary memory (swap).
Process Control Block (PCB)
Stores PID, State, PC, Registers, Memory limits, Open files list.
Key Theories & Formulas
1. Operations
- fork(): Creates a child process. Returns 0 to child, PID of child to parent.
- exec(): Replaces current process image with a new program.
Example Problems
Problem: How many processes are created?
fork();
fork();
fork();
- Formula: \(2^n\) processes (including parent).
- Result: \(2^3 = 8\).
Hardest GATE Questions
Topic: fork() loops and variable values Tricky Question (GATE 2011/2014/2021):
int x = 10;
if(fork() == 0) {
x = x + 5;
printf("%d", x);
} else {
wait(NULL);
x = x - 5;
printf("%d", x);
}
- Analysis:
fork()creates a copy of the address space.- Child sets local copy of
xto 15. Prints 15. - Parent waits, then sets its local
xto 5. Prints 5. - The "Trap": Thinking
xis shared. It is NOT shared between parent and child (copy-on-write). - Complexity: Calculating the total number of "hello" prints in a loop of
fork(). - Hard Aspect: Orphan vs Zombie processes.
- Orphan: Parent dies, init inherits.
- Zombie: Child dies, parent hasn't read exit status