Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does sync() work?

I checked man 2 sync

It shows sync and syncfs

   void sync(void);

   void syncfs(int fd);

syncfs is easy to understand. An fd is given and the data of that fd is written completely to underlying file systems.

What is it with sync?

sync() causes all buffered modifications to file metadata and data to be written to the underlying file systems.

Is it that all the buffers in the system are written to fs? or is it that all the files that are opened by this process are written to fs? I didnot quite understand "buffered modifications to file metadata"

like image 800
Sandeep Avatar asked Jun 15 '26 03:06

Sandeep


1 Answers

Whenever you issue a write, send, write to file-backed mappings or similar things the kernel is not forced to flush that data straight to persistent storage, the underlying network stack, etc... This buffering is done for performance reasons.

sync instructs the kernel to do exactly this. Empty all buffers.

like image 110
Sergey L. Avatar answered Jun 16 '26 18:06

Sergey L.