Chinaunix

标题: 为什么检测文件锁会出错? [打印本页]

作者: fantasiazhang    时间: 2006-06-10 12:34
标题: 为什么检测文件锁会出错?
fd0=open("/var/run/svtest.pid",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if(fd0<0)
        {
                printf("can't open open file \"/var/run/svtest.pid\"\n");
                exit(1);
        }
        fcntl(fd0,F_GETLK,&lock);
        printf("errno:%s\n",strerror(errno));
代码如上,打算测试一下文件锁功能,但编译后运行总提示errno:Invalid argument.而且fcntl的返回值也是-1.到底哪个参数是非法的呢?
作者: fantasiazhang    时间: 2006-06-11 09:36
各位老大的文件锁都是怎么来做的呢?
作者: xingdongcat    时间: 2006-06-12 17:49
标题: 回复 2楼 fantasiazhang 的帖子
应该是没有问题的,不知道你在调用fcntl之前对lock的设置是什么样的,感觉是参数设置有问题或者没有设置,试试看下面的例子:
lock.l_type = F_WRLOCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
fd0=open("/var/run/svtest.pid",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd0<0)
{
       printf("can't open open file \"/var/run/svtest.pid\"\n");
       exit(1);
}
fcntl(fd0,F_GETLK,&lock);
printf("errno:%s\n",strerror(errno));

[ 本帖最后由 xingdongcat 于 2006-6-12 17:52 编辑 ]




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2