Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Suppose I read (cat) a file while another process is rewriting its contents. Is the output predictable? What would happen?

share|improve this question
3  
The behavior is undefined, you should never do this. –  warl0ck Jun 26 '12 at 13:40
add comment

2 Answers

up vote 9 down vote accepted

That depends on what the writer does.

If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.

If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.

If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.

Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.

share|improve this answer
add comment

It's a classic race condition, so the outcome is unpredictable by definition.

Among others, it depends on

  • fopen(3) or open(2) write modes,
  • how/if the writer is buffering its output,
  • how the reader is reading the file,
  • the speed difference between the reader and writer,
  • the time difference between the read and writer's start.
  • And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).

If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.

share|improve this answer
1  
This answer is far more comprehensive than mine. Deleting my answer. –  killermist Jun 26 '12 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.