- 论坛徽章:
- 0
|
刚才写了一段小程序,用来防止一个程序在同时被多次运行.代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char** argv)
{
int fd;
if(-1 == (fd = open("unique.file", O_RDONLY | O_CREAT | O_EXCL, 0600)))
{
printf("该进程已经有一个副本再运行!\n");
exit(1);
}
getchar();
unlink("unique.file");
getchar();
close(fd);
exit(0);
}
问题是.文件unique.file在被unlink之后,就消失了.我用ls也找不到那个文件了.
但我查看手册,手册的描述是:
unlink deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
也就是说,只要程序不close掉fd,那么这个文件就应当存在.
哪位大侠能告诉我呢?:em14::em14::em14: |
|