John Doe
8/24/2011 5:06:00 AM
On Tue, 23 Aug 2011 19:26:51 -0700, Wang WolfLouis wrote:
> I have a question about file descriptor. consider this
> situation, one process wirte a log file using vfprintf function,
> sometimes I deleted this log file on the disk.
Misconception. On Unix, you don't "delete" files, you "unlink" them. This
removes a specific filename from a specific directory. The files
themselves are reference-counted. A file is deleted by the kernel once its
link count reaches zero and no process has it open.
If the link count reaches zero but some process has it open, the file
still exists on disk, but it can no longer be open()ed.
[On Linux, if you list the /proc/<pid>/fd directory for the process, the
file will be listed with " (deleted)" after its name.]
Windows is different; it won't allow you to delete, move or rename a file
(or directory) while any process has it open. This is one reason why
installers often tell you to close any applications before continuing;
they can't replace files (e.g. to update system DLLs) if those files are
in use. Updating particularly important DLLs requires a reboot, as that's
the only practical way to terminate "system" processes which may be using
those DLLs.
> The process seams hold the same log file file descriptor as before,
> notheing error message print.
Correct. There is no error here.
> As I know process treat the log file as COPY-ON-WRITE
> technique,
Copy-on-write isn't related to this.
> however, if the file descriptor does not exist,
> vfprintf function should print some error message. So, should access
> file every time if want to write contents ?
There is no error. The file still exists, it's still open. If you have
opened the file for read-write, you will be able to rewind the file
pointer with lseek() and read what you had written. Similarly, any
child processes which are created will inherit the descriptor.
For temporary files, it's quite common to unlink() them as soon as they
are created. This ensures that the file will automatically be deleted if
the process terminates unexpectedly (e.g. crashes).