Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fsync does not write data to file

Tags:

c

fsync

I have two (POSIX) threads that write to a log file like this:

pthread_mutex_lock(&log_mutex);
fprintf(LOG, "something...\n");
fsync(fileno(LOG));
pthread_mutex_unlock(&log_mutex);

The file is opened in main() with fopen() with mode "a". While the process is running I can't see anything appearing in the file with cat or tail although after the process is terminated and the file is fclose()-ed, the lines are all there.

What am I doing wrong?

like image 626
Michael Seiwald Avatar asked Jan 13 '23 22:01

Michael Seiwald


2 Answers

I guess you need to call fflush() to flush the changed to the file system.

See also difference between fsync() and fflush().

like image 102
Manuel Faux Avatar answered Jan 25 '23 05:01

Manuel Faux


Since you are using FILE handle in C, you need to first flush the data from C/C++ buffers to kernel buffers by calling fflush(). fsync is not really required unless you also want to make sure the data reaches the underlying physical storage especially for durability concerns

like image 20
jayadev Avatar answered Jan 25 '23 04:01

jayadev