Are Posix I / O by filename sequential?

I would like to know if there is a Posix standard guaranteeing that changes to a file are guaranteed to be reflected through repeated calls to open

/ close

with the same file name. For exposure, consider this Bash script:

#!/bin/bash

FILE=$(mktemp)

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

      

Is it guaranteed that echo

all data is available in the file by the time it ends ?

In terms of Posix functions, an example might be:

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 the record on line # 1 guaranteed to be visible for read # 2, or can the OS cache the record so it doesn't propagate over time? We can assume that no other process knows the file name or otherwise undermines the file search.

+3


source to share


1 answer


Yes. For example. from the write () spec ( 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" to read (), meaning you can open () a new fd and read () through this, and the same guarantees are provided.

By the way, while such strong consistency makes it easy to reason about semantics, it also makes providing a POSIX-compliant distributed FS with decent performance very difficult, if not impossible.

+2


source







All Articles