Sub topic 4
Process States and the Process Control Block
This chapter assumes you know what a process is and how it sits in memory (code, data, heap, stack). It also assumes you know what an interrupt is. It covers the states a process moves through and the data structure the kernel uses to track it.
Why a process needs a state
A computer usually has more processes than CPUs. At any instant, one process per CPU is executing. The others are either able to run but waiting for a CPU, or unable to run until something happens, such as a disk read finishing. The kernel records which case applies to each process. That record is the process state.
The state tells the scheduler which processes it may pick. A process waiting for keyboard input must not be given the CPU, because it would have nothing to do.
The five-state model
Most textbooks, and most GATE questions, use these five states.
| State | Meaning |
|---|---|
| New | The kernel is creating the process and setting up its structures. |
| Ready | The process can run and is waiting for a CPU. |
| Running | The process's instructions are executing on a CPU. |
| Waiting (blocked) | The process is waiting for an event, such as I/O completion or a signal. |
| Terminated | The process has finished. The kernel is releasing its resources. |
Each transition has a specific cause:
- New to Ready: the kernel admits the process once it is set up.
- Ready to Running: the scheduler selects the process and the dispatcher gives it the CPU.
- Running to Ready: a timer interrupt ends the time slice, or a higher-priority process becomes ready. This is preemption.
- Running to Waiting: the process makes a blocking system call, such as
readon a slow device. - Waiting to Ready: the event arrives, typically signalled by an interrupt handler.
- Running to Terminated: the process calls
exitor is killed.
Some transitions never happen. A waiting process never goes directly to running, because it must pass through ready so the scheduler can choose it. A ready process never goes directly to waiting, because only a running process can issue the call that blocks it.
Counting processes in each state
GATE questions often ask for a maximum or minimum count. Suppose there are n processes and k CPUs.
- At most
kprocesses are running, since each CPU runs one at a time. - Up to
nprocesses can be ready, if all of them are waiting for the CPU. - Up to
ncan be waiting, if all are blocked on I/O. A system can sit in this situation with no process running. - With one CPU and
nprocesses, at mostn - 1can be ready. This holds if one is running, and also if a process is in the new or terminated state.
Read the question carefully. "Maximum number in the ready state" and "maximum number that can be ready while one is running" have different answers.
Queues behind the states
States map to queues the kernel maintains:
- The ready queue holds PCBs of ready processes. Its ordering depends on the scheduling algorithm. It may be a FIFO list, a priority heap, or several lists.
- Device queues hold PCBs of processes waiting for a particular device. Each device has its own.
A state change is a PCB moving from one queue to another. When a disk read completes, the interrupt handler moves the waiting PCB from the disk queue to the ready queue.
The Process Control Block
The Process Control Block (PCB), also called the task control block, is the kernel data structure that represents one process. Linux calls it task_struct. The kernel creates it when the process is created and frees it after the process is fully reaped. Everything the kernel needs to stop a process and resume it later is stored there.
| PCB field group | Contents |
|---|---|
| Identification | Process ID (PID), parent PID, user and group IDs |
| Process state | New, ready, running, waiting, or terminated |
| CPU context | Program counter, general-purpose registers, stack pointer, status flags |
| Scheduling information | Priority, queue pointers, CPU time used, time slice remaining |
| Memory management | Page table pointer or base and limit registers, segment information |
| I/O and file information | Table of open files, devices allocated to the process |
| Accounting | CPU time consumed, time limits, job numbers |
The PCB is kernel data. A user process cannot read or modify it directly, and it reaches this information only through system calls such as getpid.
What the PCB does during a switch
When the kernel takes the CPU from process A and gives it to process B, it saves A's program counter and registers into A's PCB. It then loads B's saved values from B's PCB. The PCB is what makes it possible to resume a process exactly where it stopped. The next chapters on context switching examine the cost of this step. Here, note that the PCB holds the saved state while a process is not running.
A small simulation
This Python sketch models a PCB and rejects transitions the five-state diagram does not allow.
from enum import Enum
class State(Enum):
NEW = "new"
READY = "ready"
RUNNING = "running"
WAITING = "waiting"
TERMINATED = "terminated"
ALLOWED = {
State.NEW: {State.READY},
State.READY: {State.RUNNING},
State.RUNNING: {State.READY, State.WAITING, State.TERMINATED},
State.WAITING: {State.READY},
State.TERMINATED: set(),
}
class PCB:
def __init__(self, pid, priority=0):
self.pid = pid
self.state = State.NEW
self.pc = 0
self.registers = [0] * 4
self.priority = priority
def move(self, new_state):
if new_state not in ALLOWED[self.state]:
raise ValueError(
f"pid {self.pid}: {self.state.value} -> {new_state.value} is not allowed"
)
self.state = new_state
p = PCB(1)
for s in [State.READY, State.RUNNING, State.WAITING,
State.READY, State.RUNNING, State.TERMINATED]:
p.move(s)
print(f"pid {p.pid} is now {p.state.value}")
q = PCB(2)
q.move(State.READY)
q.move(State.RUNNING)
q.move(State.WAITING)
try:
q.move(State.RUNNING)
except ValueError as err:
print(err)The last line prints pid 2: waiting -> running is not allowed. The blocked process must go back through ready first.
Beyond five states
Real systems add states.
- Suspended ready and suspended waiting. When memory is short, the OS swaps a process out to disk. A swapped-out process is suspended. A suspended waiting process becomes suspended ready when its event occurs, and it returns to ready only after it is swapped back in. This gives the seven-state model.
- Zombie. After a process exits, its PCB remains until the parent calls
waitand reads the exit status. Until then the process is a zombie. It uses no CPU or user memory, but it holds a PID and a process-table slot. Zombies accumulate if a parent never callswait. - Orphan. If the parent exits first, the child is adopted by
init(PID 1) or a similar reaper process, which callswaitfor it.
Common errors
- Drawing an arrow from waiting to running. It must go to ready.
- Assuming the new state is the same as ready. A new process is not yet admitted to the ready queue.
- Thinking a zombie is still running. It has terminated, and only its PCB entry remains.
- Forgetting that preemption sends a process to ready, not to waiting.
- Believing the PCB is stored in the process's own address space. It lives in kernel memory.
Exercise
A system has one CPU and 6 processes, all past the new state and not yet terminated. What is the maximum number of processes that can be in the ready state? What is the maximum if at least one process must be running?
Show answer
With no running process required, all 6 can be ready, for example just after a timer interrupt and before the dispatcher picks one. If one process must be running, the other 5 can be ready, so the maximum is 5.
Exercise
A process is running and issues a blocking read from disk. List the state transitions until the process runs again, and name what triggers each one.
Show answer
Running to waiting, triggered by the blocking system call. Waiting to ready, triggered by the disk's completion interrupt, whose handler moves the PCB to the ready queue. Ready to running, triggered by the scheduler dispatching the process.
Exercise
A child process calls exit, but its parent never calls wait. In what state is the child, and what resource does it still hold?
Show answer
The child is a zombie, which is a terminated process whose PCB entry is still present. It holds its PID and process-table entry so the parent can later collect its exit status.