文件锁 fcntl (F_SETLK) 的“陷阱”

来源:互联网 发布:第三方数据服务商 编辑:程序博客网 时间:2024/05/21 14:44
 

http://www.gnu.org/s/libc/manual/html_node/File-Locks.html
Locks are associated with processes. A process can only have one kind of lock set for each byte of a given file. When any file descriptor for that file is closed by the process, all of the locks that process holds on that file are released, even if the locks were made using other descriptors that remain open. Likewise, locks are released when a process exits, and are not inherited by child processes created using fork.

就是说,文件锁是跟进程关联的。如果给一个文件用fcntl加了锁,又用其它方式(打开然后)关闭了这个文件,那么进程就失去这个锁了,尽管加锁的那个fd还在open着!

原创粉丝点击