免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4351 | 回复: 13
打印 上一主题 下一主题

关于unlink. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-03 19:34 |只看该作者 |倒序浏览
刚才写了一段小程序,用来防止一个程序在同时被多次运行.代码如下:
#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:

论坛徽章:
0
2 [报告]
发表于 2007-03-03 19:38 |只看该作者
$ cat << EOF > /tmp/testv.c
#include <fcntl.h>
#include <unistd.h>

int main()
{
    unlink("/tmp/testv.12414");
    return 0;
}
EOF
$ gcc -o /tmp/testv /testv.c
$ dd if=/dev/zero of=/tmp/testv.12414 bs=1k count=10240
$ df -k /tmp
$ /tmp/testv
$ df -k /tmp

论坛徽章:
0
3 [报告]
发表于 2007-03-03 19:54 |只看该作者
[root@localhost tmp]# dd if=/dev/zero of=/tmp/testv.12414 bs=1k count=10240
读入了 10240+0 个块
输出了 10240+0 个块
[root@localhost tmp]# df -k /tmp
Filesystem             1K-块        已用     可用 已用% 挂载点
/dev/hda6             33722028   4932892  27048496  16% /
[root@localhost tmp]# /tmp/testv
[root@localhost tmp]# df -k /tmp
Filesystem             1K-块        已用     可用 已用% 挂载点
/dev/hda6             33722028   4922636  27058752  16% /

论坛徽章:
0
4 [报告]
发表于 2007-03-03 20:01 |只看该作者
我刚才又试了一下,发现man手册说的没有错.
只能说明,一个文件一旦被unlink之后,同名的文件就能在相通的文件路径下被创建.

论坛徽章:
0
5 [报告]
发表于 2007-03-03 20:02 |只看该作者
好。假如你在 /tmp/testv.c 里 return 之前加上一句,sleep(10);,然后编译、执行 /tmp/testv &,(此时 testv 在后台)你运行 df -k /tmp 看看,注意 testv 进程退出前和退出后 结果的差别。

论坛徽章:
0
6 [报告]
发表于 2007-03-03 20:03 |只看该作者
看来用这种方法来防止程序被多次运行是不可考的.
多谢版主.

论坛徽章:
0
7 [报告]
发表于 2007-03-03 20:06 |只看该作者
--

你可以用 PID 文件存储已运行进程实例的 pid,并上锁。这样新的实例会检测到错误,也就知道已经有一个实例在执行,不必再次运行。

我给你的例子是为了说明,unlink() 的确可以删除文件,但删除后文件占用的磁盘块并不立即被释放,而等到文件的描述符被销毁以后才会。

--

论坛徽章:
0
8 [报告]
发表于 2007-03-03 20:07 |只看该作者
我那段程序里有两个getchar做了中断,通过d f 能看到文件系统容量的变化.
关键问题在那个open函数上.

论坛徽章:
0
9 [报告]
发表于 2007-03-03 20:10 |只看该作者
我再试一下子文件锁

论坛徽章:
0
10 [报告]
发表于 2007-03-03 23:36 |只看该作者
APUE第二版中关于这个问题的例子:


  1. Figure 4.16. Open a file and then unlink it
  2. #include "apue.h"
  3. #include <fcntl.h>

  4. int
  5. main(void)
  6. {
  7.     if (open("tempfile", O_RDWR) < 0)
  8.         err_sys("open error");
  9.     if (unlink("tempfile") < 0)
  10.         err_sys("unlink error");
  11.     printf("file unlinked\n");
  12.     sleep(15);
  13.     printf("done\n");
  14.     exit(0);
  15. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP