Atomic write to file on linux

Is there a way to flush a buffer into a file atomically?

By โ€œatomicโ€ I mean: if, for example, someone terminates my application during recording, I would like to have the file in the state before or after the recording, but not in the damaged intermediate state.

If the answer is no, maybe this can be done with really small buffers? For example, can I unload 2 consecutive int32_t variables with one 8 bytes of fwrite (on the x64 platform) and make sure that both of these int32s are reset or not one of them, but not only one of them?

+5
source share
1 answer

It might be worth checking out signal.h (the standard c library) to handle such situations. Note that additional thread guards may be required for threading.

0
source

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


All Articles