In my program (on Mac OS X), I opened the file using following code.
int fd;
fd = open(filename, O_RDWR);
Program to delete the file is as follows:
unlink(filename);
In my case, I have same file which is opened and deleted. I observed the following:
- After opening the file, I can delete it using this program and even by using
rm
command. - After deleting the file, read and write operations are working on the file without any problem.
I would like to know the reason behind this. How to prevent rm
command or unlink(2)
system call from deleting the file which is being opened?
unlink(2)
? Did you check the documentation? "If one or more process have the file open when the last link is removed, the link is removed, but the removal of the file is delayed until all references to it have been closed."read
andwrite
should continue working, right?