user1190650
user1190650

Reputation: 3425

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

Upvotes: 5

Views: 5356

Answers (1)

nneonneo
nneonneo

Reputation: 179717

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.

Upvotes: 9

Related Questions