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

linux – 实现可移植文件锁定机制,第1张

概述我已经实现了一个文件锁定机制,沿着 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 o 我已经实现了一个文件锁定机制,沿着 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.
总结

以上是内存溢出为你收集整理的linux – 实现可移植文件锁定机制全部内容,希望文章能够帮你解决linux – 实现可移植文件锁定机制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1033055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存