Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C file synchronization

I would like to open a file in C where the reads and writes are both synchronized. Is the proper way

    fopen("file.txt", O_DSYNCH | O_RSYNCH)

or

    fopen("file.txt", O_SYNCH)

This is for use on Linux

like image 831
user1190650 Avatar asked Feb 19 '23 17:02

user1190650


1 Answers

From man 3 open:

If both O_DSYNC and O_RSYNC are set in oflag, all I/O operations on the file descriptor shall complete as defined by synchronized I/O data integrity completion.

Therefore, the correct call is

open("file.txt", O_DSYNC | O_RSYNC);

Note that fopen does not take O_ flags (it uses mode strings like "r+"), and you therefore cannot use any of the O_*SYNC options with it directly.

like image 154
nneonneo Avatar answered Feb 21 '23 05:02

nneonneo