fflush · shipping · also fflush(), fflush(3)

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.

Related Observatory article: Read on hesela.com

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

related
fwrite, fclose, fsync, File descriptor, ferror, clearerr, flock

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

  1. Linux man-pages 6.19: fflush(3) (accessed 2026-10-23)
  2. Linux man-pages 6.19: fsync(2) (accessed 2026-10-23)
  3. POSIX.1-2024: fflush() (accessed 2026-10-23)
  4. Linux man-pages 6.19: setvbuf(3), buffering modes (accessed 2026-10-23)
  5. glibc 2.42: libio/stdio.h, BUFSIZ and FOPEN_MAX (accessed 2026-10-23)
  6. Hesela: stdio and lock-handoff evidence with source hashes and replay code (accessed 2026-11-30)
  7. GNU C Library 2.39: Independent Channels (accessed 2026-11-30)
  8. glibc 2.39: _IO_new_file_seekoff in-buffer seek path (accessed 2026-11-30)

Further reading

  1. Hesela: The user-space buffer comes before fsync (accessed 2026-10-21)
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.

JSON: /data/terms/fflush.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. fflush. Hesela. 2026-11-30. https://hesela.dev/terms/fflush/