1

I've asked a similar question to this previously, but the comments I got make me think I didn't express myself well or something, so I deleted it and will try again. I have C code that is using libaio asynchronous I/O threads to write to a file. Later in the code, the memory locations that are being written are re-populated. Needless to say, I have to make sure the writing is complete before the re-populating starts. If I call fsync() before the re-population starts, will this cause the main thread to block until the writing from all threads are complete? The fsync man page seems to imply this, but I can't find a clear statement of it. There is also the aio_fsync function, but its man page says that "Note that this is a request only; it does not wait for I/O completion." But waiting for I/O completion is exactly what I need.

I know that I can check on all threads writing to said file, one by one, and wait until they are done. But I was hoping for a one-liner like just calling fsync(). Is there such a thing?

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • The [POSIX `fsync()` documentation](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/fsync.html) "The `fsync()` function shall request that all data for the open file descriptor named by `fildes` is to be transferred to the storage device associated with the file described by `fildes`. **The nature of the transfer is implementation-defined.** ..." You need to provide implementation details. – Andrew Henle Oct 11 '21 at 23:03
  • (And FWIW, if you're using asynchronous IO for performance reasons, I do hope you're using file system(s) and disk system(s) that are designed to handle the parallel IO operations you're trying to do, otherwise you've complicated your code to an immense degree for no good reason. And if you're not using asynchronous IO for performance reasons...) – Andrew Henle Oct 11 '21 at 23:07
  • @AndrewHenle It's performance reasons, yes, and if we tune the number of threads and several other aspects of a run we do get better performance. And thanks for your answer. – bob.sacamento Oct 12 '21 at 02:10
  • in the past, when writing embedded systems, I used "sync;sync" to assure all writing, etc was complete before exiting the program – user3629249 Oct 12 '21 at 23:37

0 Answers0