1

Consider this pseudo-code and an ext4 file system:

f = open("/tmp/new_file", "w")
write(f, "Test")
close(f)

In another process, I try to open /tmp_newfile immediately afterwards:

Questions

  1. Can the other process open the file?
  2. What content does the other process see? Is it Test?

Expectations

I expect (1) to be true (the metadata is probably synchronized between processes) but (2) to be false (data might be buffered)

More questions

  1. How can I ensure that my file changes are visible to other processes? flush seems to work but it is bad for performance because it forces a write-to-disk. Is there something like soft-flush that makes the changes visible to other processes without flushing it to disk?
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267

1 Answers1

-1

Is it guaranteed, that the other process can see the file?

No, it's not guaranteed.

A third process can delete the file, even while you have it open.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • is it a POSIX standard or a Linux feature? – ar2015 May 24 '18 at 13:49
  • @ar2015 I'm not sure what you mean. Perhaps this explains somewhat: https://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handle-on-linux-if-the-pointed-file-gets-moved-del?noredirect=1 – Andrew Henle May 24 '18 at 13:52
  • And what if it isn't deleted? I'm only concerned if the filesystem change is synced to the main file system or if the processes have their "own" view. – Georg Schölly May 24 '18 at 14:04
  • @GeorgSchölly *And what if it isn't deleted?* Well, then you need to specify that constraint in your question. "Guaranteed" means the file will be visible no matter what - deletion is just one act that would break the guarantee. – Andrew Henle May 24 '18 at 14:07
  • I mean that if unix, free bsd and macOS also follow the same principle? – ar2015 May 24 '18 at 14:16