System Design · Concept Library · Free article
When Your Program Asks the OS for Help
Follow a read across the kernel boundary and see why a blocked worker is not a busy CPU.
A system call is a controlled request for the operating system kernel to perform an operation on behalf of a program. If a read cannot complete yet, the calling work can become blocked and stop competing for CPU time until the device reports completion. A blocking worker may still occupy a limited worker slot. Completion-driven designs can use that slot for other ready work, while every success, error and cancellation path must release its owned handles.
Ordinary code cannot operate every resource directly
Application code normally runs with limited privilege in user mode. The operating system kernel manages protected operations such as device access, process scheduling and address-space mappings. A system call is a controlled entry into that kernel interface.
Calling read(handle, buffer, count) asks the kernel to transfer available bytes from the resource named by the handle. The call can return data, report an error or, under a nonblocking interface, report that completing now would block. Exact APIs and return rules depend on the operating system and resource type.
Crossing the boundary does not mean the CPU waits inside the same instruction until a disk platter finishes. The kernel can record the pending operation, block the calling task and schedule other ready work.
Compare two lines in an image loader. pixel = buffer[i] reads bytes already addressable by the application; an ordinary successful array access does not need a system call. n = read(fd, buffer, 16) requests bytes through an OS-managed resource. A library wrapper arranges the system-call number and arguments, enters the kernel, and returns its result to your code. Some higher-level library calls buffer data and can finish without another kernel entry.
For a positive request size, a successful Linux read returns the number of bytes actually copied, which can be less than 16. A return of zero means end of file; minus one reports an error through errno. If it returns 6, only the first six newly read bytes are available. Process those six or continue filling the remaining buffer according to the format; do not treat the other ten as newly read image data. Handle interruption and nonblocking retry conditions according to the API.
A system call is not automatically blocking. A regular-file read may be satisfied from cached data, and many system calls return without waiting for a device. A transition into kernel mode also need not switch to a different thread. The lab deliberately chooses a pending device read so the blocking transition is visible.
The device can be busy while the worker uses no CPU
After issuing the read, the request moves from running to blocked in our model. It is waiting for device completion and is not eligible for CPU scheduling. The device has work in flight; the CPU may run a different ready task.
This distinction explains a common service symptom: low CPU utilization does not prove the service has spare request capacity. Every worker can be blocked on a slow dependency while ready requests wait for worker admission. A faster CPU does not shorten the fixed four-tick device wait in this experiment. If the modeled device is paused, requests and their handles remain blocked until a new run resumes it.
When completion arrives, the blocked request becomes ready again. It still needs a core for its final processing tick. Completion makes work eligible; it does not guarantee immediate execution when all cores are occupied.
Enable JavaScript to compare the two choices in the compact concept diagram. The worked examples explain the same process boundary, scratch ownership, CPU schedule or pending-I/O consequence without requiring the interactive.
Blocking a call and retaining a worker are separate choices
In a thread-per-request design, a blocking read can retain that request's worker thread until completion. The thread uses no CPU while blocked, but the worker slot is unavailable to admit another request. More threads can overlap more waits until memory, handles, scheduler overhead or the device becomes the next limit.
A completion-driven design records the pending operation and lets the worker run other ready work. An event loop or completion queue later dispatches the result. This can support many waiting operations with fewer threads, but application state must survive across the split and code must handle cancellation and errors carefully.
The lab models the admission difference. It does not claim that one programming API, operating system or runtime implements every completion path in the same way.
| Design | Trace | Useful CPU | Elapsed |
|---|---|---|---|
| Blocking | A: prepare, wait, finish; then B; then C | 6 ticks | 18 ticks |
| Completion-driven | Prepare A/B/C; wait for the wave; handle A/B/C | 6 ticks | 10 ticks |
The lab batches admitted reads into a wave. Preparation uses ticks 0 to 3, all three reads finish their four-tick device phase at tick 7, and handling results uses ticks 7 to 10. Device time is measured from the last issue in the wave. Earlier reads therefore wait longer than four elapsed ticks. This batching assumption makes the arithmetic explicit; real devices can finish requests independently.
A pending request is not a blocked thread in the completion-driven case. Its continuation state, such as the buffer and where to resume processing, stays in application-managed storage. The one worker remains available. Simply wrapping a blocking file read in a callback does not provide this behavior: the runtime needs an asynchronous facility or other workers. Nonblocking readiness and asynchronous completion are also different interfaces; regular-file I/O does not become asynchronous just by setting a socket-style nonblocking flag.
A finite handle stays owned until every terminal path releases it
Opening a file or device resource returns a handle. On Unix-like systems this is commonly a file descriptor, a small integer used in later calls. The process has a finite descriptor table, even if its configured limit is large.
A happy path that always closes the handle can still leak on error. Cancellation creates the same ownership question: if the request stops caring about the result, who cancels or drains the operation and releases the handle? A leaked handle remains allocated after the user-visible request ends. Repetition can exhaust the table and make later opens fail.
The workbench shows one acquired handle per admitted read. Its error and cancellation fixtures finish correctly only when cleanup returns ownership on those paths. A successful trace says nothing about data durability; close and fsync have different contracts.
Suppose the handle limit is two. Releasing a worker does not release a read's file descriptor, so admitting three pending reads at once still asks for three handles. The last lab keeps that limit fixed. Its available repair uses two blocking workers to admit at most two reads, finishes that wave, then admits the third. A real completion-driven service can instead bound in-flight operations with a queue or permit counter; that independent admission control is outside this lab.
Cleanup needs a clear owner. A finally block or an ownership wrapper can close the descriptor after all users of the operation have settled, including error paths. Cancellation is more subtle: another thread may still be using the descriptor, and closing or reusing its integer can race with that operation. Our cancellation fixture settles the operation before releasing its handle. Follow the concrete API's cancellation and close rules rather than treating a close call as universal cancellation.
Name the resource that limits useful progress
A CPU-bound workload spends most of its limiting time executing computation. An I/O-bound workload waits mainly for input or output. These are workload descriptions, not permanent labels on a program. Image decoding may be CPU-bound for one format while reading many tiny remote files is latency-bound.
Measure running, ready and blocked time together. High ready-queue wait with saturated cores suggests CPU contention. Many blocked workers with an idle CPU points toward device or dependency waiting. A full handle table is a resource leak or concurrency-bound problem even if both CPU and device have capacity.
Ungraded transfer checks
- Does a blocked read occupy a CPU core in this model?
- No. It waits for device completion and is not ready to run. It may still retain a worker slot in blocking mode.
- Why might CPU utilization be low while requests queue?
- The admitted workers can all be blocked on I/O, leaving ready requests unable to obtain a worker slot.
- What happens after device completion?
- The request becomes ready and needs CPU scheduling for its remaining work.
- Why test error and cancellation separately from success?
- They are distinct terminal paths. A handle released on success can still leak when an error or cancellation exits through different code.
Your turn: repair four I/O paths
Release a worker while reads wait, close a handle after error, resume a paused device and settle cancellation, then respect a two-handle table. The device time stays explicit, so a completed repair cannot pretend those ticks became CPU work.
A system call asks the kernel to perform a privileged operation. A read that cannot finish yet can block its caller, making that work ineligible for CPU time until completion. The device can remain busy while the CPU runs another ready task.
Open The system call and I/O workbench. Editable controls require JavaScript; the article and model limits remain readable here.
Sources and model limits
- Linux read system call documents read results, errors and nonblocking behavior.
- Linux open system call describes file descriptors and open file descriptions.
- Linux close system call describes descriptor release and important multithreaded qualifications.
- OSTEP I/O devices explains polling, interrupts and direct memory access at a teaching level.
- The workbench has one named device, three requests and fixed CPU phases. One admitted wave is a declared batch: its reads overlap and complete together after the selected device time, measured from the last issue step. A paused run does not advance that device clock. The model omits caches, real files, kernel queues, DMA, interrupts and measured latency.
Published by GOAT Engineer. Revisit CPU scheduling. Privacy policy · [Contact](mailto:[email protected]).
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.