Introduction
A UNIX process does not run continuously. It goes through several states during its lifetime. The kernel manages these transitions based on events like I/O completion, scheduling decisions, and signals.
Process States in UNIX
1. User Running:
- The process is executing in user mode (running user code).
2. Kernel Running:
- The process is executing in kernel mode (executing a system call or handling an interrupt).
3. Ready to Run (In Memory):
- The process is ready to execute and is waiting in memory for the scheduler to assign the CPU.
4. Asleep in Memory (Sleeping):
- The process is waiting for an event (e.g., I/O completion, resource availability).
- It is in memory but not eligible for CPU time.
5. Ready to Run (Swapped):
- The process is ready to run but has been swapped out to disk due to insufficient memory.
6. Sleeping (Swapped):
- The process is sleeping AND has been swapped to disk.
7. Preempted:
- The kernel has decided to run a higher-priority process, so this process is temporarily set aside.
- It is ready to run and will resume when scheduled.
8. Created:
- The process has been created (
fork()has been called) but is not yet ready to run. - The kernel is setting up data structures.
9. Zombie:
- The process has terminated (
exit()called) but its parent has not yet calledwait(). - The process table entry still exists to provide the exit status to the parent.
- No resources are held except the process table entry.
State Transition Diagram
fork()
│
▼
┌─────────┐
│ Created │
└────┬────┘
enough memory │ not enough memory
┌────────────┴──────────────┐
▼ ▼
┌──────────────┐ ┌────────────────┐
│ Ready to Run │ │ Ready to Run │
│ (In Memory) │◄────────►│ (Swapped Out) │
└──────┬───────┘ swap └────────────────┘
│ scheduled ▲
▼ │ swap out
┌──────────────┐ │
│Kernel Running│─────────────────┤
└──┬───────┬───┘ │
│ │ sleep │
│ ▼ │
│ ┌──────────────┐ ┌────────────────┐
│ │ Asleep in │───►│ Sleeping │
│ │ Memory │ │ (Swapped) │
│ └──────┬───────┘ └────────┬───────┘
│ │ wakeup │ wakeup
│ ▼ │
│ ┌──────────────┐ │
│ │ Ready to Run │◄────────────┘
│ │ (In Memory) │
│ └──────────────┘
│
│ return to user
▼
┌──────────────┐
│ User Running │
└──────┬───────┘
│ exit()
▼
┌──────────────┐
│ Zombie │
└──────────────┘
Key Transitions
| From | To | Trigger |
|---|---|---|
| Created → Ready (Memory) | Process setup complete, memory available | |
| Created → Ready (Swapped) | Not enough memory | |
| Ready (Memory) → Kernel Running | Scheduler selects this process | |
| Kernel Running → User Running | Return from system call / interrupt | |
| User Running → Kernel Running | System call or interrupt occurs | |
| Kernel Running → Asleep (Memory) | Waiting for I/O or resource | |
| Asleep (Memory) → Ready (Memory) | Event/resource becomes available (wakeup) | |
| Kernel Running → Ready (Memory) | Preempted by higher-priority process | |
| Kernel Running → Zombie | Process calls exit() | |
| Ready/Asleep (Memory) → (Swapped) | Swapper swaps process to disk | |
| (Swapped) → (In Memory) | Swapper swaps process back in |
Summary
- UNIX processes go through 9 states during their lifetime.
- Transitions are triggered by system calls, scheduling, I/O events, and memory management.
- A zombie process has exited but hasn't been collected by its parent.
- Swapping moves processes between memory and disk to manage limited RAM.