In the context of the current discussion the real questions are: Has
There are OSes where if you open a file, then delete it, the opened
stream continues to be valid and you can continue reading from (or even
writing to) it until you close it, at which point it's actually removed
from disk. Other OSes will refuse to delete a file that is open. Still
others will delete the file immediately and return an error on the next
read.
In the first case, if you open a file, then rename a file over it, then
open the same file name again, you will have two streams open to the
same file name but with different contents. This is actually useful in
some scenarios (e.g. reading rotating log files), but can be confusing
the first time you encounter it.
The problem with rename potentially not being atomic isn't the above,
though. The issue is that some OSes will delete the destination name
before renaming the source file to the destination name. If one
attempts to open that file between those steps, one will get a "file not
found" error.
Worse, some network file systems won't recognize that the file contents
have changed. It's entirely possible that you can open a file, read
part of contents A, wait a while (during which someone else renames over
your file), then read part of contents B.
Of course, all of this is off-topic here since ISO C doesn't acknowledge
that multiple programs can run at the same time
Please bare with me... at the end is an interesting speculation that
NO renaming is necessary and an interesting UNIX question.
Amazing how a thread grows and the implications!! Logic is so
challenging at times! Once I begin to think I understand what I am
doing ... a conversation exposes things I have never considered fully
enough. Along with that comes additional growth. A very good thing.
Now to add my 2 cents of "learning observer" comment.
I very often write text files (LOG files) during a long (24+ hrs)
topological tuning run. The tuning program flushes the output to these
files after certain blocks of code are completed. During the tuning
run I often open these text files with Notepad and observe their
contents. I am careful to rename them within Notepad if I do any
editing (annotations) and want to save a particularly interesting
section of text. If I save an opened Notepad session without renaming
(once the tuning run is complete) I loose any tuning run output that
was added after the time I opened in Notepad. This is obvious! The
data from the tuning run is on the disk; however, the Notepad session
maintains the original begin file marker and the Notepad session's end
file marker is the first to be read in subsequent openings of the
file.
If I were to open in Notepad during the tuning run, delete the
contents, and then save without renaming ... I am not sure what would
happen? Yikes! The tuning program stream remains open (flushed but not
closed) and thus the file pointer within the tuning program would be
at the place of last write? But the removal by another program of the
earlier output now creates havoc?? Lets say file "NEW" is initiated
during the run and resides on the hard drive next to the open log
file. The tuning program pointer is now out of sync and could write
over the "NEW" file? At the conclusion of the tuning run and the
closing of the log file ... that log file would essentially contain
two "End-of-file" markers? One from the Notepad session and one from
the tuning program? If you then delete the file with two end-of-file
markers (speculative - not proven!) ... the begin-file-marker is
cleared and the file contents sit on the disk ready to be overwritten.
And the NEW file may be corrupted ... or may not have been overwritten
and by luck remains intact.
Thus to share data between simultaneously running programs ... all
that need be done is have Program #2 open the file in read only mode!!
No renaming necessary?
Program #2 reads the data (with internal time stamp). If the time
stamp within the data is fresh ... it acts upon it. If time stamp is
unchanged ... does nothing.
Now what a great operating system would do is NOT allow two programs
too simultaneously open a file with write privileges. I have confirmed
by observation that Win2k DOES allow both Notepad and a C program to
simultaneously open the very same file with write privilege. I am
curious ... does Unix prevent this from happening? My guess is not.
This is similar to the problem of large program development when teams
of programmers each have access too and make changes to the same code.
The latest MS studio makes a big deal about having control over this
situation and highlighting the changes such that only one set of
changes are kept. Challenging task indeed!! I wonder how MS keeps the
NEW files intact? Amazing technology!