最佳锁文件方法 [英] Optimal lock file method

查看:277
本文介绍了最佳锁文件方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows可以选择打开具有独占访问权限的文件。 Unix不。

Windows has an option to open a file with exclusive access rights. Unix doesn't.

为了确保对某些文件或设备的独占访问,Unix中通常使用通常存储在/ var / lock目录中的锁文件。

In order to ensure exclusive access to some file or device, it is common practice in Unix to use a lock file usually stored in the /var/lock directory.

C指令 open(/var/lock/myLock.lock,O_RDWR | O_CREAT | O_EXCL,0666)如果锁定文件已经存在,则返回-1,否则创建它。该函数是原子的,并确保没有竞争条件。

The C instruction open( "/var/lock/myLock.lock", O_RDWR | O_CREAT | O_EXCL, 0666 ) returns -1 if the lock file already exist, otherwise it creates it. The function is atomic and ensures there is not race condition.

当资源释放时,锁定文件被以下指令删除
remove(/ var / lock / myLock。锁)

When the resource is released, the lock file is deleted by the following instruction remove( "/var/lock/myLock.lock" ).

此方法有两个问题。


  1. 程式可能会在不移除锁定的情况下终止。例如因为它被杀死,崩溃或什么。锁定文件保留在原位,并且将阻止对资源的任何访问,即使它不再使用。

  1. The program may terminate without removing the lock. For instance because it is killed, crashes or whatever. The lock file remains in place, and will prevent any access to the resource even though it is not used anymore.

锁定文件是使用组和世界写入权限创建的,但通常的做法是将帐户配置为使用将清除组和世界的权限掩码写权限。因此,如果我们有一个可靠的方法来确定锁是孤立的(不使用),则不允许文件所有者的用户将其删除。

The lock file is created with group and world write privilege but it is a common practice to configure accounts to use a permission mask that will clear the group and world write permission. Thus, if we had a reliable method to determine that the lock is orphan (not use), a user not owner of the file will not be allowed to remove it.

对于记录,我使用锁文件来确保对连接到串行端口dev / ttyUSBx实际上)。咨询方法,需要合作,是好的。但是应该确保不同用户之间的独占访问。

For the record, I use the lock file to ensure exclusive access to the device connected to the serial port (/dev/ttyUSBx in fact). Advisory method, requiring cooperation, is OK. But exclusive access should be ensured between different users.

有比锁文件更好的同步方法吗?如何确定创建锁文件的进程是否仍在运行?
如何使另一个用户可以删除锁文件,如果不使用?

Is there a better synchronization method than the lock file? How to determine if the process who created the lock file is still running? How to make it possible for another user to remove the lock file if not in use?

我想出的一个解决方案是使用该文件作为Unix套接字文件。如果文件存在,请尝试使用该文件进行连接。如果失败,我们可能认为文件的所有者进程已经死了。这需要在所有者进程中的一个线程循环在套接字 accept()上。不幸的是,系统不再是原子了。

One solution I came up with was to use the file as Unix socket file. If the file exist, try to connect using the file. If it fails, we may assume the owner process of the file is dead. This requires to have a thread looping on socket accept() in the owner process. Unfortunately, the system wouldn't be atomic anymore.

推荐答案

你应该使用 flock() ,如

You should probably be using flock(), as in

fd = open(filename, O_RDWR | O_CREAT, 0666); // open or create lockfile
//check open success...
rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain.
if (rc)
{
    // fail
}

这篇关于最佳锁文件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆