The Runtime Theory
mediumKernelInternals#syscall-contract#partial-io

What Happens When read() Returns Fewer Bytes Than Requested?

A practice prompt about syscall contracts, file types, blocking behavior, buffers, and correct handling of partial reads.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read

A strong answer

read(fd, buffer, count) attempts to return up to count bytes. A successful call can return fewer bytes because fewer are currently available, a boundary such as end-of-file was reached, or the underlying object has different streaming semantics. For a regular file, a short read is possible even when more data may become available after another call.

A return of zero commonly means end-of-file for a regular file; errors are reported separately. A program that needs an exact number of bytes must loop, advance the destination pointer, reduce the remaining count, and handle interruption, error, and end-of-stream according to the protocol.

The descriptor is a process-local reference used by the kernel to find an open file description and its operations. It is not a physical block address. The actual path can involve cached pages, a device wait, a pipe producer, or a socket buffer.

Follow-up direction

Ask what object is being read and whether the caller is blocking. Explain that API contract and internal I/O path are distinct questions.

This answer walks

Practice follow-ups

  1. 01How does a zero return differ from an error?
  2. 02Why can reading a socket behave differently from reading a regular file?
  3. 03What is the difference between a file descriptor and a file description?

More interviews in this topic

One dispatch a week

The trace behind each question, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save