Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 20 / 21 · INPUT, OUTPUT, AND SAFETY

C at the operating-system boundary.

Distinguish portable C streams from POSIX descriptors, partial reads, and system calls.

BY NAVAN SAMILONG READPOSIX INTERFACETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

ISO C defines FILE streams and their library operations, but it does not define Unix file descriptors or the functions open, read, write, and lseek. POSIX defines those system interfaces for a family of operating systems. A descriptor is a small integer handle in a process, while a FILE pointer refers to a buffered C library stream. They can ultimately reach the same underlying file, but their buffering, positioning, and error rules are different layers.

A system call may transfer fewer bytes than requested. That is not automatically a failure: a read may return the available bytes, and a write may make partial progress. Correct code treats the return value as the number of bytes actually transferred and advances its buffer position accordingly. A negative result signals an error, with errno providing more detail under the interface's contract. An interrupted operation may be retryable, while other errors need a different response.

02 / THE RULES

What C guarantees.

read returns zero at end-of-file for an ordinary file stream. write returns the number of bytes accepted or a negative error result. lseek changes a descriptor's file offset when the underlying object supports seeking; a pipe or terminal may not. open creates a descriptor that the caller later closes, and the numeric descriptor can be reused after close. A stale descriptor number therefore cannot be treated as a stable identity for a file.

Do not casually mix descriptor operations with stdio operations on a related FILE stream. The library's buffering can make the two views of position and bytes disagree unless the relevant synchronization rules are followed. Choose one layer for a particular flow, or use an explicit adapter with a documented ownership and synchronization plan. This example stays entirely in the POSIX descriptor layer and writes a short fixed message to standard output.

03 / UNDER THE SURFACE

Why the detail matters.

A descriptor is an operating-system resource owned by a process, not a C object that carries a buffer size or a file format. read and write operate on counted byte sequences. That makes them useful for pipes, sockets on some systems, terminals, and ordinary files, but it also means the caller has to manage offsets, partial transfers, and resource lifetime explicitly. The count passed to write is a maximum request, not a guarantee that the entire buffer will be consumed in one call.

The example's loop is a miniature state machine: sent marks how much of the message has already been accepted, and the invariant is that bytes before sent need no further write. After a positive result, sent grows; after EINTR, the operation is retried without moving the pointer; after another error or a zero-progress result, the program stops. This reasoning is specific to POSIX. The portable C stream layer has its own return-value and buffering contracts, which should be read separately rather than assumed to work identically.

04 / CODE, EXPLAINED

See the rule in context.

system-interfaces.c
#include <unistd.h>
#include <errno.h>
#include <stddef.h>

int main(void) {
    const char message[] = "Descriptor output\n";
    size_t length = sizeof message - 1;
    size_t sent = 0;
    while (sent < length) {
        ssize_t n = write(STDOUT_FILENO, message + sent, length - sent);
        if (n < 0) { if (errno == EINTR) continue; return 1; }
        if (n == 0) return 1;
        sent += (size_t)n;
    }
    return 0;
}
FOLLOW THE EXECUTION

message contains the text plus a terminating zero; length excludes that zero because the descriptor writes counted bytes, not a C string. Each successful write reports how many bytes it accepted, and sent advances by exactly that amount. The loop continues until all bytes are accepted, then exits successfully. On a normal terminal the line Descriptor output appears. This is POSIX code, not an ISO C17-only example.

05 / THE BOUNDARY

Where the rule stops.

A completed write call does not prove the data is durable on storage. It only reports what the interface accepted at that point. Durability and nonblocking behavior require additional system-specific contracts.

06 / THE CONNECTION

Keep this model.

The system boundary illustrates why portability must be stated rather than assumed. A C program can intentionally depend on POSIX and be well designed, but its operating-system contract should remain separate from the ISO C rules used by the rest of the course.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.