views:

47

answers:

2

I have a file under creation in a long process. To my understanding copying that file to another file would kill the creation process. Is there away to overcome this problem?

+6  A: 

No, reading from a file that is currently being written to will not disrupt that writing process. However, the copy you take will be incomplete, as obviously you can't copy parts that haven't been written yet. Only the first file will get subsequent writes, not the copy, so if you want the entire file, wait until the writing is complete.

Ether
Yes. You won't interrupt the writing process by reading a file. If you could do that it would be a Denial Of Service vulnerability in every program which ever writes a world-readable file (think of /bin/passwd for example).
MarkR
+1  A: 

Unless the second process uses a file descriptor associated with the same open file description as the process creating the file, there will be no problem. If the file descriptor does share the same open file description, then the read process will move the file position (as will the write process), leading to confusion. However, that requires the processes to have a common ancestor that opened the file (for example, the copier is forked by the creator). This is unlikely to be the case, I think, from your description.

Your copying process can do as tail -f does and read up to the first EOF, then pause and read again when there is more data available, repeating until told to stop, or it can determine that the file is complete. If it has no way of telling when the file is complete, then you'll have to kill it (interrupt, or worse).

Jonathan Leffler