关于文件锁(flock)

许威威 2012-06-24 11:22:08
我在给文件加写锁之后,然后一直占用着文件,但是用另外一个测试程序测试的时候,为什么会显示文件已经加锁,但是测试程序还是可以往文件里面写入东西,求解。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
if(argc < 2)
{
printf("Argument error!\n");
exit(1);
}
int fd = open(argv[1],O_RDWR|O_NONBLOCK);
printf("fd %d\n",fd);
if(fd < 0)
{
fprintf(stderr,"open %s error:%s\n",argv[1],strerror(errno));
exit(1);
}
struct flock lock;
//给整个文件加锁
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;

if(fcntl(fd,F_SETLK,&lock) < 0)
{
fprintf(stderr,"f_setlk error:%s\n",strerror(errno));
exit(1);
}
printf("pid:%d\n",getpid());
//等待输入,
getchar();

return 0;
}

测试程序

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main(int argc,char *argv[])
{
if(argc < 2)
{
printf("Argument error!\n");
exit(1);
}
int fd = open(argv[1],O_RDWR|O_APPEND);
printf("fd:%d\n",fd);
if(fd < 0)
{
fprintf(stderr,"open error:%s\n",strerror(errno));
exit(1);
}

struct flock lock;
lock.l_type = F_RDLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
int flag;
if((flag = fcntl(fd,F_SETLK,&lock)) < 0)
{
//printf("flag:%d\n",flag);
fprintf(stderr,"set lock failure:%s\n",strerror(errno));
//exit(1);
}
fcntl(fd,F_GETLK,&lock);
if(lock.l_type == F_RDLCK)
printf("%d read lock is set by pid:%d\n",lock.l_type,lock.l_pid);
else if(lock.l_type == F_WRLCK)
printf("%d WRLOCK is set by pid:%d\n",lock.l_type,lock.l_pid);
else if(lock.l_type == F_UNLCK)
printf("none lock\n");
printf("l_start:%ld\n",lock.l_start);
printf("l_pid:%d\n",lock.l_pid);

char buffer[] = "hello world!";
if(write(fd,buffer,strlen(buffer)) < 0)
{
fprintf(stderr,"write error:%s\n",strerror(errno));
exit(1);
}

return 0;
}
...全文
224 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2012-06-24
  • 打赏
  • 举报
回复
fcntl提供的记录锁是建议锁,需要进程之间的配合,也就是说进程都要遵守获得锁->访问->释放锁的规则。
关于建议锁和强制锁可以这里说的比较清楚。
http://blog.csdn.net/hnmsky/article/details/4316421

fcntl
.....
The following values for cmd are available for advisory record locking. Record locking is supported for regular files, and may be supported for other files.

F_GETLK
....
qq120848369 2012-06-24
  • 打赏
  • 举报
回复
普通的文件锁是建议锁, 和互斥锁都是一样的道理, 你遵守就会被锁住, 你不遵守它拦不住你.

文件锁支持强制锁, APUE可以看到相关说明, 楼上的链接也很详细.


Mandatory locking
(Non-POSIX.) The above record locks may be either advisory or mandatory, and are advisory by default.

Advisory locks are not enforced and are useful only between cooperating processes.

Mandatory locks are enforced for all processes. If a process tries to perform an incompatible access (e.g., read(2) or write(2)) on a file region that
has an incompatible mandatory lock, then the result depends upon whether the O_NONBLOCK flag is enabled for its open file description. If the O_NON-
BLOCK flag is not enabled, then system call is blocked until the lock is removed or converted to a mode that is compatible with the access. If the
O_NONBLOCK flag is enabled, then the system call fails with the error EAGAIN.

To make use of mandatory locks, mandatory locking must be enabled both on the file system that contains the file to be locked, and on the file itself.
Mandatory locking is enabled on a file system using the "-o mand" option to mount(8), or the MS_MANDLOCK flag for mount(2). Mandatory locking is
enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permission bit (see chmod(1) and chmod(2)).

The Linux implementation of mandatory locking is unreliable. See BUGS below.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧