Will the data written with write () be flushed to disk if the process is killed?

I am working with a program that uses qdbm to store a repository of key values ​​(qdbm is associated with the program). In some cases, the process places the value in the qdbm database and then restarts by calling an external init script (via system ()). It seems that sometimes the value written to the qdbm database is not actually stored, and I wonder if this could be due to the fact that the data is not cleared to disk before the process is killed through SIGTERM.

Since qdbm writes using the write () system call (for example, the library function fwrite ()), I think the Linux kernel should know in order to ultimately flush everything to disk (the system does not restart, just a process). In addition, close () is called on FD before the process is killed.

So, is my understanding correct, or do I need to add some fdatasync () or similar calls somewhere there? References to authoritative references to semantics would also be appreciated.

0
source share
1 answer

Typically, data already written by the application to the kernel buffer with write() will not be affected by the application exiting or killed in any way. Closing or destroying implicitly closes all file descriptors, so there should be no difference; the kernel will process the flushing afterwards. Thus, there are no fdatasync() or similar calls.

There are two exceptions:

  • if the application uses user-land buffering (without calling the write() system call, but instead caching data in the user-space buffer with fwrite() ), these buffers may not be cleared if the user-space file is correctly closed - getting killed SIGKILL, certainly lead to the loss of the contents of these buffers,

  • if the kernel also dies (power loss, kernel crash, etc.), your data might skip getting written to disks from the kernel buffers and then be lost.

    / li>
0
source

Source: https://habr.com/ru/post/1263297/


All Articles