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

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

问题描述

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.lock" ).

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.

推荐答案

看看有启发性的演示 文件锁定技巧和陷阱:

Take a look at the enlightening presentation File Locking Tricks and Traps:

这个简短的演讲介绍了文件锁定的几个常见陷阱以及一些更有效地使用文件锁定的有用技巧.

This short talk presents several common pitfalls of file locking and a few useful tricks for using file locking more effectively.

要更准确地解决您的问题:

To address your questions more precisely:

有没有比锁文件更好的同步方法?

Is there a better synchronization method than the lock file?

正如@Hasturkun 已经提到的,正如上面的演示所说,您需要使用的系统调用是 flock(2).如果您想在许多用户之间共享的资源已经是基于文件的(在您的情况下是 /dev/ttyUSBx),那么您可以 flock设备文件本身.

As @Hasturkun already mentioned and as the presentation above told, the system call you need to use is flock(2). If the resource you'd like to share across many users is already file-based (in your case it is /dev/ttyUSBx), then you can flock the device file itself.

如何判断创建锁文件的进程是否还在运行?

How to determine if the process who created the lock file is still running?

您不必确定这一点,因为在关闭与您的文件关联的文件描述符时,flock-ed 锁将自动释放,即使进程已终止.

You don't have to determine this, as the flock-ed lock will be automatically released upon closing the file descriptor associated with your file, even if the process was terminated.

如何让其他用户在不使用的情况下删除锁定文件?

How making it possible for another user to remove the lock file if not in use?

如果您要锁定设备文件本身,则无需删除该文件.即使您决定锁定 /var/lock 中的普通文件,使用 flock 您也不需要删除文件来释放锁定.

If you would lock the device file itself, then there will be no need to remove the file. Even if you would decide to lock an ordinary file in /var/lock, with flock you will not need to remove the file in order to release the lock.

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

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