sync(2)

โœจ sync(2) โ€” the "okay bestie, FLUSH it" manual โœจ

okay so we're in the System Calls Manual today and the function we're stanning is sync (and its little sister syncfs) ๐Ÿ’– this is from the Linux man-pages, and honestly? it's giving "short but mighty."


๐ŸŽ€ NAME ๐ŸŽ€

sync, syncfs โ€” commit filesystem caches to disk ๐Ÿ’พโœจ

like, take all that stuff chilling in memory and actually write it down. no cap.


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) ๐Ÿซถ


๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

#include <unistd.h>

void sync(void);

int syncfs(int fd);

๐Ÿฆ‹ Feature Test Macro Requirements for glibc ๐Ÿฆ‹

(go check feature_test_macros(7) if you're new here, bestie)

sync():
    _XOPEN_SOURCE >= 500
        || /* Since glibc 2.19: */ _DEFAULT_SOURCE
        || /* glibc <= 2.19: */ _BSD_SOURCE

syncfs():
    _GNU_SOURCE

๐Ÿ’… DESCRIPTION ๐Ÿ’…

sync() causes all pending modifications to filesystem metadata and cached file data to be written to the underlying filesystems. that's it. that's the whole vibe. it's the "save your game before you close the laptop, i'm not asking" energy ๐Ÿ‘‘

syncfs() is like sync(), but synchronizes just the filesystem containing the file referred to by the open file descriptor fd. so not the whole world โ€” just that one filesystem. lowkey more polite ๐ŸŽ€


๐Ÿซถ RETURN VALUE ๐Ÿซถ

syncfs() returns 0 on success ๐ŸŽ‰ and if it's not vibing, it returns -1 and sets errno to tell you what went wrong. classic.


๐Ÿ’€ ERRORS ๐Ÿ’€

sync() is always successful. iconic. never misses. periodt โœจ

syncfs() can fail for at least the following reasons:

  • EBADF โ€” fd is not a valid file descriptor. bestie that's not a real door ๐ŸšชโŒ
  • EIO โ€” An error occurred during synchronization. This error may relate to data written to any file on the filesystem, or on metadata related to the filesystem itself. so not even necessarily your fault, which is so annoying ๐Ÿ˜ญ
  • ENOSPC โ€” Disk space was exhausted while synchronizing. the disk said "no more room, i'm full" ๐Ÿ“
  • ENOSPC, EDQUOT โ€” Data was written to a file on NFS or another filesystem which does not allocate space at the time of a write(2) system call, and some previous write failed due to insufficient storage space. so the space problem showed up later. sneak attack ๐Ÿ’€

๐ŸŒธ VERSIONS ๐ŸŒธ

According to the standard specification (i.e., POSIX.1), sync() schedules the writes, but may return before the actual writing is done. so technically it can dip out early ๐Ÿ˜ณ

However, Linux waits for I/O completions, and thus sync() or syncfs() provide the same guarantees as fsync() called on every file in the system or filesystem respectively. so on Linux we're actually getting the full send ๐Ÿ”ฅ


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

sync() โ€” POSIX.1-2024.

syncfs() Linux. (just Linux. built different fr fr ๐Ÿง)


๐Ÿงš HISTORY ๐Ÿงš

sync() โ€” POSIX.1-2001, SVr4, 4.3BSD. a true elder, we respect the lineage ๐Ÿ™‡โ€โ™€๏ธ

syncfs() Linux 2.6.39, glibc 2.14. a newer baby but she's thriving ๐Ÿ’–

Since glibc 2.2.2, the Linux prototype for sync() is as listed above, following the various standards. In glibc 2.2.1 and earlier, it was "int sync(void)", and sync() always returned 0. throwback lore!! ๐Ÿ“ผ

In mainline kernel versions prior to Linux 5.8, syncfs() will fail only when passed a bad file descriptor (EBADF). Since Linux 5.8, syncfs() will also report an error if one or more inodes failed to be written back since the last syncfs() call. glow-up behavior honestly ๐ŸŒˆ


๐Ÿ™ˆ BUGS ๐Ÿ™ˆ

Before Linux 1.3.20, Linux did not wait for I/O to complete before returning. ancient history but still messy ๐Ÿ’€

โญ SEE ALSO โญ


and that's the whole page, bestie!! ๐Ÿซถโœจ it's Linux man-pages 6.20, dated 2026-02-08, and yes i read the entire thing so you don't have to ๐Ÿ’… go flush your buffers, you absolute main character ๐Ÿ‘‘๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms