---
title: "System Calls & Blocking I/O"
description: "Follow a socket read through kernel entry, blocking, wakeup, and return."
source: https://vizlearn.app/cs/os/system-calls-blocking-io
access: premium (interactive player requires Lifetime; this doc is free)
---

# System Calls & Blocking I/O

A system call lets a running thread ask the kernel to perform a protected operation. Blocking
I/O can suspend that thread when the operation needs data that has not arrived. Input makes the
thread ready again; the scheduler must run it before the call can finish and the program can
continue.

Follow the caller, T1, through one socket read. Its identity stays the same when it enters the
kernel. If it must wait, another thread can use the CPU. The useful distinction is between
**where execution happens**, **whether T1 is runnable**, and **whether its read has returned**.
Those are three separate facts.

## How System Calls and Blocking I/O Work

User mode restricts what application instructions may do. A system call crosses a controlled
entry into kernel mode, where the operating system handles the request. The CPU is still
executing on behalf of the calling thread. Entering the kernel does not, by itself, select a
different thread. [Linux entry/exit handling](https://www.kernel.org/doc/html/latest/core-api/entry.html)
describes these transitions.

This lesson uses an established blocking byte-stream socket, one reader, and a valid buffer.
The caller requests up to a positive number of bytes. If the receive buffer is empty, the
kernel can put the caller to sleep until input is available. If bytes are already buffered,
the call can copy them and return without that wait. An ordinary read can also return fewer
bytes than requested. The requested count is a limit, not a promise to fill the buffer.
See [read(2)](https://man7.org/linux/man-pages/man2/read.2.html) and
[recv(2)](https://man7.org/linux/man-pages/man2/recv.2.html).

| State | What T1 can do |
| --- | --- |
| Running | Execute on the CPU, in user or kernel mode |
| Blocked | Wait for a condition; it is not eligible for ordinary scheduling |
| Ready | Compete for the CPU; it is not executing yet |

Publishing input and waking T1 are separate from running T1. Once selected, T1 resumes its
suspended kernel work, checks the receive condition again, copies bytes into the user buffer,
and returns a count to user mode. Only then can it execute the statement after `read`.

## Blocking I/O Step by Step

The default requests 4 bytes. The receive buffer starts empty; A, B, C, and D arrive at tick 3.
T2 is also runnable. This model lets T2 finish a 4-tick quantum before rescheduling T1.

1. **Enter the kernel.** T1 moves from user mode to kernel mode on the same CPU. The call is
   pending, and no other thread has been dispatched.
2. **Check and block.** The receive buffer has 0 bytes. T1 becomes Blocked, retaining the
   continuation of its unfinished read. The kernel's wait registration is treated as protected.
3. **Run T2.** The scheduler selects T2. From tick 0 to tick 3, T2 executes while T1 waits for
   input. T1 consumes no modeled CPU ticks during this interval.
4. **Publish input.** Grouped kernel input handling pauses T2's user work and puts A–D into
   the receive buffer. This work uses the same core; T1 has not resumed.
5. **Wake T1.** T1 moves to Ready at tick 3. Its user buffer is still empty and its read is
   still pending. T2 resumes user work and runs until tick 4.
6. **Schedule T1 again.** T2 returns to Ready. T1 takes the CPU in kernel mode and checks the
   buffer again. The four bytes remain available in this single-reader example.
7. **Copy, return, continue.** T1 copies A–D into its user buffer, returns 4 while crossing
   back to user mode, and then reaches the next statement.

The two aligned timing rows expose the distinction: T1 spends 3 ticks Blocked and 1 tick Ready,
while T2 owns the modeled CPU intervals. The moving T1 token keeps its identity color across
both privilege changes and queue changes. A mode change is visible inside the CPU; a scheduling
change moves the token between the CPU and a queue.

## Why Wakeup Does Not Finish a Blocking Read

A wakeup restores eligibility to run. It does not hand the caller a return value or reserve the
CPU for it. Scheduling policy, other runnable work, and priority affect when the caller gets
another turn. The fixed tick-4 choice is specific to this example, not a universal Linux rule.
[sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) describes the scheduling layer.

This distinction matters when you investigate latency. A thread can wait for input and then
wait for CPU time after input is available. Improving the input source addresses the first
interval; it does not automatically remove the second.

## Edge Cases

- **Already buffered:** set arrival to 0. T1 still enters the kernel, copies data, and returns,
  but it never enters Blocked. There is no modeled waiting, not a claim of zero syscall cost.
- **No other runnable thread:** remove T2. The CPU idles during T1's data wait; T1 resumes at
  tick 3 after wakeup without the additional ready interval.
- **No input:** observation stops at tick 4 with T1 still Blocked and the call still pending.
  Ending playback does not produce a timeout or EOF.
- **Short read:** request 4 bytes with only 2 incoming. This read returns 2. An application
  that needs exactly 4 must account for that partial result.
- **Excess input:** request 2 with 4 incoming. A/B go to the user buffer; C/D remain buffered
  for a later read.

## Common Mistakes

- Treating every syscall entry as a thread context switch. The already-buffered case has a
  kernel entry and return with T1 running throughout.
- Drawing the blocked caller as consuming CPU time. A blocked thread waits off the CPU;
  other threads can run, or the core can idle.
- Moving a woken thread directly to the next application statement. It must be scheduled,
  resume kernel work, and return first.
- Assuming a successful `read` filled the requested buffer. Check the returned byte count
  before consuming the destination bytes.

## A Note on Simplification

The animation has one core, at most two threads, and one input batch. T2's fixed quantum is an
illustrative policy, not Linux's default scheduler. Ticks measure only the displayed wait/run
intervals; syscall entry, copying, input handling, and scheduling have unmodeled duration.

The model groups driver, interrupt, softirq, network-protocol, and DMA work into one input
event. It omits ABI registers, wait-queue locks, competing readers, signals, EOF, socket errors,
timeouts, multicore execution, and filesystem caching. The socket uses its ordinary receive
threshold and has no read-until-full flag. In a real program, nonblocking I/O and asynchronous
I/O have different contracts; neither is a variant shown here.
