ENع
←Process states
Lesson 07 · Processes and threads

Process states: ready, running and blocked

12 min10 steps5 questionsBeginner
About this lesson

A computer with one CPU can only run one process at a time, yet it juggles many. The kernel does it by keeping every process in one of three states: running on the CPU, ready and waiting for a turn, or blocked waiting for something slow such as the disk. Follow two processes, A and B, through ten ticks of time as A waits for a disk read, B takes the CPU, the disk wakes A up and the timer takes the CPU back from B. The lesson on the process (address space, registers and the PCB) introduces the PCB that records each state, and the Linux lesson on processes and signals shows the same states in ps.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 7 · Interactive
01 / 10

Three states

A process is a running program. At any moment it is in exactly one of three states: running (on the CPU now), ready (could run, but waits for the CPU) or blocked (waits for something else, such as the disk).

state     meaning                          can it use the CPU?
running   its instructions run on the CPU  it is using it
ready     it could run right now           yes, when picked
blocked   it waits for an event (disk ...) no, not until the event

one CPU runs one process at a time,
so all the others are ready or blocked.
Loading figure…
or ← →
Read the lesson step by step
  1. Three states

    A process is a running program. At any moment it is in exactly one of three states: running (on the CPU now), ready (could run, but waits for the CPU) or blocked (waits for something else, such as the disk).

    state     meaning                          can it use the CPU?
    running   its instructions run on the CPU  it is using it
    ready     it could run right now           yes, when picked
    blocked   it waits for an event (disk ...) no, not until the event
    
    one CPU runs one process at a time,
    so all the others are ready or blocked.
  2. Two processes start

    We start two processes, A and B, on a computer with one CPU. The kernel writes ready as the state in each one's process control block (PCB) and puts both in the ready queue, a line waiting for the CPU.

    the plan (time counted in ticks):
      A   2 ticks of work, read() from the disk, 2 more ticks
      B   6 ticks of work
      time slice 3 ticks · a disk read takes 2 ticks
    
    PCB states   A ready   B ready
    ready queue  [A, B]   (front first)
  3. Ready to running

    The scheduler, the part of the kernel that picks who runs next, takes A from the front of the queue. The kernel loads A's saved registers and A runs on the CPU in ticks 1 and 2. B stays ready.

    dispatch: ready -> running
    
    t1   A running  B ready
    t2   A running  B ready
    
    ready queue  [B]
  4. Running to blocked

    At the end of tick 2, A calls read() to get data from the disk. The disk is slow, so the kernel starts it working, marks A blocked and takes A off the CPU. The scheduler never picks a blocked process.

    wait: running -> blocked
    A's system call, in C99 + POSIX:
    
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
        char buf[512];
        int fd = open("data.bin", O_RDONLY);
        if (fd < 0) return 1;
        ssize_t n = read(fd, buf, sizeof buf); /* may block: A waits for the disk */
        printf("read %zd bytes\n", n);
        close(fd);
        return 0;
    }
    
    kernel  sends the request to the disk
    PCB     A blocked   (waits for the disk)
  5. The CPU keeps busy

    The CPU is free, so the scheduler dispatches B. B runs in ticks 3 and 4 while the disk fetches A's data. Blocking lets the CPU and the disk work at the same time.

    dispatch: ready -> running
    
    t3   A blocked  B running
    t4   A blocked  B running
    
    ready queue  []
    disk         reading for A
  6. Blocked to ready

    At the end of tick 4 the disk finishes and raises an interrupt. The kernel marks A ready, not running: B still holds the CPU and keeps running in tick 5.

    wake: blocked -> ready
    disk interrupt: A's data is here
    
    t5   A ready    B running
    
    ready queue  [A]
  7. Running to ready

    B has now run for 3 ticks, its whole time slice. A timer interrupt hands the CPU back to the kernel, which moves B to the back of the ready queue and dispatches A. Taking the CPU away like this is preemption.

    end of t5   timer: B used its 3-tick slice
    preempt     B: running -> ready   (back of the queue)
    dispatch    A: ready -> running
    
    t6   A running  B ready
  8. A finishes

    A runs ticks 6 and 7, carrying on right after its read(), because its registers were saved in its PCB. Then A calls exit(). The kernel marks it done and the CPU is free again.

    exit: running -> done
    read() returned 512 (bytes read)
    
    t6   A running  B ready
    t7   A running  B ready
    
    PCB   A done
  9. B finishes

    The scheduler dispatches B again. It runs its last 3 ticks, 8 to 10, and exits. In all 10 ticks the CPU was never idle, even though A spent 2 ticks blocked.

    the whole trace:
    
    t1   A running  B ready
    t2   A running  B ready
    t3   A blocked  B running
    t4   A blocked  B running
    t5   A ready    B running
    t6   A running  B ready
    t7   A running  B ready
    t8   A done     B running
    t9   A done     B running
    t10  A done     B running
    
    CPU busy 10 of 10 ticks
  10. Four arrows

    The kernel moves processes on four arrows: dispatch (ready to running), preempt (running to ready), wait (running to blocked) and wake (blocked to ready). A blocked process always wakes to ready, never straight to running.

    the scheduler as a Python simulation:
    
    from collections import deque
    
    SLICE, IO_TICKS = 3, 2
    left = {"A": [2, 2], "B": [6]}       # CPU bursts; A reads the disk between
    state = {"A": "ready", "B": "ready"}
    ready, io, running, used = deque("AB"), {}, None, 0
    t = 0
    while any(s != "done" for s in state.values()):
        t += 1
        if running is None and ready:     # dispatch
            running, used = ready.popleft(), 0
            state[running] = "running"
        print(t, state["A"], state["B"])
        for p in list(io):                # the disk works
            io[p] -= 1
            if io[p] == 0:                # wake
                del io[p]; state[p] = "ready"; ready.append(p)
        if running is None:
            continue
        p = running
        left[p][0] -= 1; used += 1
        if left[p][0] == 0:
            left[p].pop(0)
            if left[p]:                   # wait for the disk
                state[p] = "blocked"; io[p] = IO_TICKS
            else:                         # exit
                state[p] = "done"
            running = None
        elif used == SLICE:               # preempt
            state[p] = "ready"; ready.append(p); running = None
    
    prints:
    1 running ready
    2 running ready
    3 blocked running
    4 blocked running
    5 ready running
    6 running ready
    7 running ready
    8 done running
    9 done running
    10 done running

Quiz Unlocks after the last step.