linux – 实现可移植文件锁定机制

我已经实现了一个文件锁定机制,沿着 linux手册页中“open”的建议,其中指出:

Portable programs that want to perform atomic file locking using a
lockfile,and need to avoid reliance on NFS support for O_EXCL,can
create a unique file on the same file system (e.g.,incorporating
hostname and PID),and use link(2) to make a link to the lockfile. If
link(2) returns 0,the lock is successful. Otherwise,use stat(2) on
the unique file to check if its link count has increased to 2,in
which case the lock is also successful.

这似乎工作得很好,但是为了在我的测试中获得100%的代码覆盖率,我需要覆盖链接数增加到2的情况.

我已经尝试过谷歌搜索了,但我似乎能找到的所有内容都是上面反复出现的“它完成的方式”.

任何人都可以向我解释一下哪种情况会导致链接失败(返回-1),但链接数增加到2?

解决方法

您可以在 Linux程序员手册的链接(2)页面底部找到您的问题的答案:
On NFS file systems,the return code may  be  wrong  in  case  the  NFS
   server  performs  the link creation and dies before it can say so.  Use
   stat(2) to find out if the link got created.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


查找全部容器的日志文件 $ sudo find /var/lib/docker/containers -name *.log 查看日志位置 $ docker inspect --format='{{.LogPath}}' <container_name> 实时查询内容 $
Linux日志文件中列属性的详细解析
在Linux系统中没有duf命令,如何有效地管理磁盘空间?
深入探讨EncryptPad在Linux操作系统中的功能和优势
原理和应用场景:Linux中ttyload工具的工作原理和实际用途
深度解析SELinux的三种策略类型
评估Linux系统性能的ttyload工具使用效果
分享在Linux系统中检测SSH版本的方法
介绍Linux平台上的数据加密工具EncryptPad
在Linux系统中,如何查看和诊断块设备信息?
在Linux环境下如何查看块设备信息?
探索Linux操作系统下的数据加密工具EncryptPad
学会在Linux系统中查看硬盘信息
分析SELinux:原理与实践
掌握SELinux策略类别
技巧:有效解读和管理Linux日志文件
查看Linux系统中的所有用户
了解Linux系统中各种不同类型的日志文件
深入理解Linux PS命令
方法:在Linux操作系统中查看用户