How can unlinking be postponed?

Thomas at uwai.UUCP Thomas at uwai.UUCP
Mon Sep 9 01:57:53 AEST 1985


First of all, one unlink()s *directory entries*, not inodes.  If the
inode pointed to by the directory entry has its link count decremented
to zero, then that inode is deallocated *as soon as* the file is no
longer open by anyone.  As long as you have the file open, you can
perform any operation on it you want that does not take a filename but
rather just a descriptor, such as lseek, read, write, close, fchmod,
etc., but not rename, access, or chmod.  Once it is closed, it is gone.  
Thus the proper way to create a file which will be removed upon the 
process's exit is:

	invent a filename
	unlink the file (in case it already exists)
	create the file (saving the file descriptor)
	unlink the file

Now only the creating process and its descendents (though inhereited
file descriptors) may access the file, and the space will be reclaimed
upon the process's exit.  Note that if someone tried to create the file
again while it was open but not in any directory, then a different file
would be created!

-- tom
-- 

Tom Christiansen
University of Wisconsin
Computer Science Systems Lab 
...!{allegra,heurikon,ihnp4,seismo,uwm-evax}!uwvax!tom
tom at wisc-ai.arpa



More information about the Comp.unix.wizards mailing list