Search code examples
concurrencyfile-ioposixsystem-callsfile-descriptor

According to POSIX, when will my writes be visible to other processes?


If I open a file with O_CREAT | O_WRONLY and write to it. Does POSIX say 1) other apps can see the file in the folder (without fsync) and 2) be able to see what I wrote?

Same questions after I do close without an fsync.

Now finally will it once my program ends? I understand fsync will confirm my write is on disk but I don't need my file to be on disk, I need it to be visible to other processes


Solution

  • Yes, other processes see your writes immediately. You do not need to close or fsync.

    https://pubs.opengroup.org/onlinepubs/009695399/functions/write.html

    Writes can be serialized with respect to other reads and writes. If a read() of file data can be proven (by any means) to occur after a write() of the data, it must reflect that write(), even if the calls are made by different processes. A similar requirement applies to multiple write operations to the same file position. This is needed to guarantee the propagation of data from write() calls to subsequent read() calls.

    This means, for instance, that if the OS caches your write instead of actually writing it to disk, it needs to ensure that any other reads of that file are fulfilled from that same cache.