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

#FromEventToWhen
1NEWSCHEDULEREADYProcess created, admitted to ready queue
2READYSCHEDULERUNNINGScheduler dispatches the process to CPU
3RUNNINGWAITWAITINGProcess blocks on I/O, lock, or sleep
4WAITINGWAKEREADYI/O completes or lock is released
5RUNNINGEXITZOMBIEProcess 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.