fflush
fflush is a C standard-I/O operation that passes pending output from a stream's user-space buffer to its underlying write function.
For an output stream, zero reports a successful flush, while EOF reports failure with errno set to EBADF or to any error that write(2) can return. Capture the failure before cleanup. The stream remains open. A NULL argument flushes all open output streams. [1]
Input-stream flushing has a separate contract. For a seekable input file, buffered data fetched but not yet consumed is discarded, and POSIX sets the underlying file offset to the stream position. Pipes and terminals are outside that rule. [1] [4]
For a descriptor-backed output stream, check the intended writes and fflush before fsync(fileno(stream)), because fsync cannot submit bytes still held in the library buffer. Serialize concurrent output around that boundary. Neither operation alone supplies multi-file atomicity or directory-entry persistence. [1] [2]
Regular files are block buffered by default, terminals line buffered and stderr unbuffered. [5] In glibc 2.42, BUFSIZ, the buffer size that setbuf expects, is 8192 bytes. [6]
A file lock does not invalidate library input. In 36 ordered Linux/glibc cases, retained input (including an in-buffer fseek) stayed stale; input fflush or reopening returned fresh bytes. Output flushed after handoff lost an increment; flushing before unlock preserved both. [7]
GNU documents stale input across independent channels; glibc 2.39 can seek within buffered bytes. Coordinate input cleaning and positioning separately from output submission and crash durability. [8] [9]
Relations
Advantages
- Makes buffered output visible through the file descriptor at a point the program chooses, without closing the stream. [1]
Limitations
- Flushing a user-space output buffer is not a durability acknowledgement.
- A successful flush marks modification and status-change times for update but says nothing about later writeback errors. [4, 2]
Citations
- Linux man-pages 6.19: fflush(3)
- Linux man-pages 6.19: fsync(2)
- POSIX.1-2024: fflush()
- Linux man-pages 6.19: setvbuf(3), buffering modes
- glibc 2.42: libio/stdio.h, BUFSIZ and FOPEN_MAX
- Hesela: stdio and lock-handoff evidence with source hashes and replay code
- GNU C Library 2.39: Independent Channels
- glibc 2.39: _IO_new_file_seekoff in-buffer seek path
Further reading
Sources & caveats
- linux-fflush is the Linux man-pages 6.19 fflush(3) page, re-read for return values, errors, NULL handling and input streams.
- linux-fsync is the Linux man-pages 6.19 fsync(2) page, re-read for the boundary between library buffers and kernel buffers.
- posix-fflush is the POSIX.1-2024 fflush() page, read for input-stream offsets and timestamp marking.
- setvbuf is the Linux man-pages 6.19 setvbuf(3) page, read for default buffering modes.
- glibc-stdio is libio/stdio.h at the glibc 2.42 tag, read for the BUFSIZ value.
- The directory-fsync entry links here because flushing stream buffers does not persist directory entries.
- Lock-boundary evidence: controlled native executions on Linux 6.8.0-151-generic, not a population failure rate, network-locking test or crash experiment. Original frozen captures retained; integration replay does not increase their sample sizes.