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โfdis 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 awrite(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
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms