The process needs to communicate its status to other processes on the same machine. To do this, he writes the relevant information in the file, each time rewriting it completely.

To make the state complete, a checksum is added to the end of the file, and readers, in the event of a checksum error, reread the file (this may mean that the writer has started updating the file, and part of the file has already been rewritten).

The question is whether there is a more elegant solution, without reference to the platform, for sharing file access.

An additional question is whether there is a guarantee that readers will see the changes in the same order in which the writer makes them (i.e., is it worth adding some ordinal number of the record in addition to the checksum).

    2 answers 2

    If you already want on files, then do it somewhere like this - the process writes to a temporary file, and when it realizes that it has already recorded everything and closed the file, it simply deletes the original one and changes the temporary one. In this case, the chances that the file will be read "incompletely" are few. In Linux, while the file is open by the process, it is not deleted (more precisely, the file name in the table is deleted, but not the file itself). And when the last descriptor is closed, the file will be physically deleted (but in fact hardly anyone will overwrite it with zeros. Just change the blocks as free). This method gives another plus - if there are several readers and they read “long”, then they can read the “old version”.

    But this method should not be used for synchronization - the file system will not give you guarantees that if one program has recorded data, then the other has already seen it.

    UDP

    An additional question is whether there is a guarantee that readers will see the changes in the same order in which the writer introduces them (i.e., is it worth adding another ordinal number of the record in addition to the checksum)

    I would not hope for it. Yes, it is quite possible that for a certain file system this will be fulfilled, but I have never seen such guarantees anywhere.

    • Venda, as it were, is also there in their requirements ..) Although why not, in Windows readers will try again simply. - Vladimir Gamalyan
    • "Venda" is not listed in the question :). But, given that they are trying to be a posix underneath, then everything should work. But it needs to be checked. - KoVadim
    • Perhaps satisfied with your answer. And about the abuse of the FS (the next answer), what do you think? Is the devil like his baby so scary? - Vladimir Gamalyan
    • there is such a thing that violence alone, then very rarely to another. It is possible that the torrent clients look at your code and giggle softly, "this is it" rapes "." I would personally take the base (or something like "radish") and use it. Here and the guarantees are stricter, and the buns are different. And violence endure more seriously. - KoVadim

    Use pipes or sockets. There is absolutely no point in raping the FS.

    Of course, the socket API is different on different platforms, but ideologically they are equivalent.

    For Qt there is a wrapper - QLocalSocket .

    The fastest way to IPC is file mapping to memory.

    And your way is the wildest bike.

    • No, well, it's not interesting, everyone can on sockets) - Vladimir Gamalyan
    • @VladimirGamalian on FS you can have a lot of guarantees (if the file system is local) and very little (if it is a network clustered file system like ceph or OrangeFS, in which there is not even a lock on the file). Writing adapters for all occasions will take you a couple of months and still will not work normally. - gbg
    • Those. leave the option with optimistic read and checksum, and no steam? - Vladimir Gamalyan