Trace a Process Lifecycle
This trace follows a process through the five classic states the kernel manages: New, Ready, Running, Waiting, and Terminated.
Step 1: Process created via fork(), enters New state
A parent process calls fork(). The kernel allocates a new PCB, creates a new PID, copies the parent's address space (or sets up copy-on-write), and copies file descriptors. The new process is in the New state.
Step 2: Process initialized, placed on ready queue
The kernel initializes the process's registers, sets the program counter to the entry point, and adds the PCB to the ready queue — a list of all processes eligible to run. The process is now in the Ready state.
Step 3: Scheduler selects process, context switch to Running
The scheduler — the OS component that decides which ready process runs next — picks this process from the ready queue. The kernel performs a context switch: it saves the previously running process's register state and loads this process's state. The process transitions to Running.
Step 4: Process executes and requests I/O (read from file)
The process runs, executing instructions. It opens a file and calls read(). The data is not immediately available — the disk must spin up and the read head must seek to the right track. The process blocks waiting for the disk.
Step 5: Process blocks, transitions to Waiting
Because the process called a blocking I/O operation, the kernel moves it off the CPU and onto the waiting queue. The process is now in the Waiting (blocked) state — it has yielded the CPU voluntarily.
Step 6: I/O completes, process returns to Ready queue
The disk controller signals that the data is ready. An interrupt fires, and the kernel's interrupt handler wakes the process. The PCB is moved from the waiting queue back to the ready queue. The process is now in the Ready state again.
Step 7: Scheduler selects process again, context switch back to Running
The scheduler eventually picks this process again. Another context switch occurs: the kernel saves the currently running process's state and restores this process's state. The process resumes in the Running state, right where it left off.
Step 8: Process calls exit(), transitions to Terminated
The process finishes its work and calls exit(). The kernel closes all file descriptors, releases memory, updates the parent process's exit status (if the parent is waiting), and frees the PCB. The process enters the Terminated state. If the parent has called waitpid(), it can now reap the zombie; otherwise, the process remains as a zombie until the parent exits or signals SIGCHLD.