Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing and reading the same fd without fsync in Linux

Tags:

c

linux

fsync

Suppose I write a block to a file descriptor without doing fsync and then read the same block from the same descriptor some time later. Is it guaranteed that I will receive the same information?

The program is single-threaded and no other process will access the file at any time.

like image 769
a sad dude Avatar asked Dec 28 '22 09:12

a sad dude


1 Answers

Yes, it is guaranteed by the operating system.

Even if the modifications have not made it to disk yet, the OS uses its buffer cache to reflect file modifications and guarantees atomicity level for reads and writes, to ALL processes. So not only your process, but any other process, would be able to see the changes.

As to fsync(), it only instructs the operating system to do its best to flush the contents to disk. See also fdatasync().

Also, I suggest you use two file descriptors: one for reading, another for writing.

like image 154
fge Avatar answered Jan 14 '23 15:01

fge