Frage

I would like to know whether there is a Posix standard guarantee that modifications to a file are guaranteed to be visible through repeated open/close calls on the same file name. For exposition, consider this Bash script:

#!/bin/bash

FILE=$(mktemp)

echo "Some data" >> $FILE
cat $FILE

Is it guaranteed that by the time echo finishes, all data is available in the file?

In terms of Posix functions, an example could be like this:

const char fn[] = "/tmp/somefile";
const char data[] = "hello world";

// Stage 1
{
   int fd = open(fn, O_CREAT);
   write(fd, data, sizeof data); // #1
   close(fd);
}

// Stage 2
{
   int fd = open(fn);
   read(fd, ...);                // #2
   close(fd);
}

Is it guaranteed that the write at line #1 is visible to the read #2, or could the OS cache the write so that it does not propagate in time? We can assume that no other process knows the file name or otherwise subverts the file lookup.

War es hilfreich?

Lösung

Yes. E.g. from the write() specification ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html ):

After a write() to a regular file has successfully returned:

    Any successful read() from each byte position in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.

    Any subsequent successful write() to the same byte position in the file shall overwrite that file data.

Note that it says "Any" read(), i.e. you can open() a new fd and read() via that, and those same guarantees are provided.

Incidentally, while this kind of strong consistency makes it easy to reason about the semantics, it also makes providing a POSIX compliant distributed FS with decent performance very difficult, if not impossible.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top