Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does fsync system call work in linux?

Tags:

linux

When write call returns, the data is copied onto some page managed by kernel. That page can contain writes from multiple process. So, when one of the applications issues fsync call, will this result in flushing the whole page, which means flushing other applications data also, but the cost would be incurred by the process which calls fsync. Is this correct?

like image 696
Jimm Avatar asked Dec 21 '12 18:12

Jimm


1 Answers

fsync operates on a single file. It will flush all changes made to that file. If multiple processes are writing into a single file the process that made the fsync call will be paused until all the changes are written to disk.

This is more complicated when some journaling filesystems come into play. For example, ext3 and ext4 (to a lesser extent) with the "ordered" mode are required to flush all the changes to all of the files ahead of the fsync file in the journal.

That means that if programs have been writing to a large database or a large log file or a video file and you then fsync a two-line configuration file, your fsync must wait for all those megabytes of data to be written before it can return.

This is why I run my ext4 in "writeback" mode, which can have some unpleasant consequences after a crash such as files of the correct size but filled with zeroes. But in normal operation "writeback" is so much faster that I feel the tradeoff is worth it.

like image 197
Zan Lynx Avatar answered Oct 02 '22 22:10

Zan Lynx