I wrote a small program in C
FILE *fp = fopen("/tmp/file", "w+");
char *buf = "hello";
fwrite(buf, 1, strlen(buf), fp);
for(;;);
On a shell I use cat to read the contents, the file is empty as fflush is not being called. My question is will it remain like this forever or will at sometime the data will be pushed to disk?
In other words, does the fwrite call crosses the File systems boundary to the VM subsystem, if yes then page daemon should at sometime be invoked which will flush the contents to the file. If not then the data will remain there forever till the file is closed or flushed.
Each stdio
FILEhas an internal buffer (see setvbuf for more). Thefwriteorfprintforfputscalls are filling that buffer. It will be written to the file system (using the write(2) syscall, executed inside the kernel) only when full or when you callfflush.You may have line-buffering (e.g. for stdout), then the
writeis triggered when a newline appears in the bufferP.S. try to
straceyour program to understand what it is doing.P.P.S Linux is free software: you could study the source code of the GNU libc or of musl-libc -whose source I find easier to read.