关于Linux下的强制锁
来源:互联网 发布:广州龙族网络 编辑:程序博客网 时间:2024/05/17 08:21
问:
我在程序中,open一个文件,然后用fcntl()上锁,上锁(是写锁)也是成功的。
但我试图再次打开这个文件时,还是能open。我想要的结果是:再次open时,应该是错误的,请问怎么能办到?
答:
it's advisory lock
when using advisory lock
it is up to programmer to ensure that
whenever access a file or part of that file, acquire its lock first
maybe you need mandantory lock
see the following table
Type of Blocking Nonblockingexisting lock descriptor, descriptor,on region tries to tries toheld by otherprocess read write read writeread lock OK blocks OK EAGAINwrite lock blocks blocks EAGAIN EAGAIN
see /usr/src/linux/Documentation/mandatory.txt and relevant chapters in APUE
the steps you need to take are as follows:
chmod g+s file
chmod g-x file
then remount the filesystem which "file" resides with a "mand" option, e.g.
mount -o remount,mand /dev/hda2 /
there you go!
-------------------------------补充:
1. 强制锁并不会影响文件是否可以被打开
2. 强制锁并未包含在 posix 标准当中。linux (2.0.x)也支持强制性锁,但为了打开这个功能,你必须:
1、对要加锁的文件执行 chmod g+s; chmod g-x (Sys V 强制锁)
2、该文件所在的文件系统在 mount 的时候,加选项: mand
这样,当一个进程用 fcntl 或者 lockf 对一个文件加了写锁之后,其他进程读写这个文件会被 BLOCK,除非 open 加参数 O_NONBLOCK。用 fcntl + F_GETLK,可以得到 BLOCK 本进程的进程号。
- 关于Linux下的强制锁
- linux下强制覆盖的密令
- linux下强制umount
- Linux下强制杀Tomcat进程的Shell脚本
- Linux下强制T掉登录的用户
- Linux 下强制中断其他用户的登陆连接
- linux 下如何强制关闭正在运行的程…
- Linux下强制卸载已安装的php!
- Linux下强制卸载已安装的php!
- 关于强制索引的使用
- 关于C++的强制转换
- 关于强制换行的问题
- 关于强制换行的总结
- linux下强制变更root密码方法
- linux下强制踢掉登陆用户
- Linux 下如何强制取消 NFS 目录
- Linux系统下强制踢掉登录用户
- linux下强制umount+Fuser命令详解
- 获取webbrowser的useragent
- DeepLearning(深度学习)原理与实现(二)
- 每天一个linux命令(37):date命令
- debian安装mongodb
- 2013蓝桥杯预赛javaA 世纪末的星期
- 关于Linux下的强制锁
- HDU3294:Girls' research
- 字符串匹配的KMP算法
- IIS 6.0/7.0/7.5、Nginx、Apache 等 Web Service 解析漏洞总结
- x86 IO端口见解
- HDU2072:单词数
- 英语感受 5月份--英语思维
- 词法分析器源码
- 优先队列+prim(待完善)