Lesson 2 of 15
Process State Machine
Process States
A Linux process moves through a well-defined set of states during its lifetime. There are exactly 5 states and 5 valid transitions:
SCHEDULE
NEW ──────────────► READY ◄──── WAKE ────┐
│ │
SCHEDULE │
│ │
▼ │
RUNNING ──── WAIT ──► WAITING
│
EXIT
│
▼
ZOMBIE
The 5 Valid Transitions
| # | From | Event | To | When |
|---|---|---|---|---|
| 1 | NEW | SCHEDULE | READY | Process created, admitted to ready queue |
| 2 | READY | SCHEDULE | RUNNING | Scheduler dispatches the process to CPU |
| 3 | RUNNING | WAIT | WAITING | Process blocks on I/O, lock, or sleep |
| 4 | WAITING | WAKE | READY | I/O completes or lock is released |
| 5 | RUNNING | EXIT | ZOMBIE | Process calls exit() or returns from main |
ZOMBIE is a terminal state — no event can move a process out of it. The process stays in ZOMBIE until its parent calls wait() to collect its exit status.
Any event that doesn't match one of the 5 transitions above is invalid and the state remains unchanged. For example, a READY process receiving WAIT is a no-op — only a RUNNING process can block.
Events
#define SCHEDULE 0 // Scheduler picks a process
#define WAIT 1 // Process blocks (I/O, lock)
#define WAKE 2 // Blocked process is unblocked
#define EXIT 3 // Process terminates
Your Task
Implement transition(state, event) that returns the new state, and trace(start, events[], n) that prints each state name after applying each event in sequence.
TCC compiler loading...
Loading...
Click "Run" to execute your code.