Study guides · Intermediate

Waiting for I/O: synchronous calls, readiness, and completion

Compare read, epoll, and io_uring through progress boundaries, partial results, request identity, and buffer lifetime.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Identify the event being awaited
  2. Readiness does not perform the operation
  3. Associate completions with requests
  4. Trace a partial receive
  5. Bound outstanding work
  6. Compare equivalent workloads
  7. Ask Logeia about this articleCheck your understanding

Identify the event being awaited

Begin by distinguishing a function returning, an operation appearing ready to make progress, and a submitted operation completing. A blocking read can wait until data, end-of-file, or an error is available. A nonblocking read instead reports that progress is currently unavailable through a result such as EAGAIN. Neither boundary inherently means that an application message is complete. TCP supplies bytes, so the application must follow its framing rules. Describing an interface as synchronous or asynchronous does not remove the need to define the unit of progress and the unit of application work separately.

Readiness does not perform the operation

Epoll reports readiness for registered descriptors; the application still performs a read or write and checks its result. Another consumer may change the state before the operation, so a nonblocking read can return EAGAIN after notification. Do not assume a complete fixed-size buffer exists merely because an event arrived. Distinguish end-of-file from temporary lack of progress. Readiness is useful for coordinating many connections that spend time waiting, but per-connection state remains necessary. The program must retain partial message bytes, determine what work remains, and avoid confusing an event count with the number of completed application requests.

Associate completions with requests

Io_uring submits operation descriptions and returns completion records. Two reads can complete in a different order from submission, so attach identity and associate each completion with its original request. A result of thirty bytes does not satisfy an application requirement for one hundred; the remaining work still needs a policy. Asynchronous completion does not promise full-length results for every read. Buffers and related state must remain valid while an operation can reference them. Changing the notification mechanism does not eliminate partial progress or resource ownership. Inspect the particular operation’s contract, including cancellation and multishot behavior if those features are used.

Trace a partial receive

Suppose an application needs one hundred payload bytes and receives read results of forty, thirty, and thirty. Its accumulated length advances through forty, seventy, and one hundred, and the next destination begins after the accumulated prefix. EAGAIN preserves that state for a later attempt; EOF requires deciding whether the required length was reached. Restarting each read at the beginning of the buffer would overwrite earlier data. This is a progress-accounting example, not a claim that actual network delivery uses three packets or three system calls. The same state-machine reasoning remains useful across blocking, readiness-driven, and completion-driven implementations.

Bound outstanding work

Efficient waiting does not create unlimited consumption capacity. If work is submitted faster than it completes, queued requests and buffers can grow. Bound connection count, outstanding operations, and retained bytes, and define when to delay or reject admission. An edge-triggered handler that drains one busy connection without a fairness policy can delay others. Cancellation also needs lifecycle reasoning: a caller ceasing to wait does not necessarily mean that the kernel has stopped referencing the operation’s buffer. Track the contractually established completion or cancellation boundary before recycling resources. Throughput and correct cleanup must be designed together.

Compare equivalent workloads

Compare request size, connection count, concurrency, cache state, and persistence requirements consistently. Lower CPU usage is not necessarily an improvement if latency or backlog increases. Supported operations and restrictions depend on the kernel and deployment environment. Establish correct results and behavior under partial input, disconnects, and cancellation before increasing load. A newer interface name is not performance evidence. Select readiness or completion based on the operation mix and lifecycle model, then measure the resulting implementation. Report actual tested conditions rather than presenting one favorable configuration as a universal ranking of interfaces.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 14

Choose a learning path