我可以使用open(2)和O_CREAT和flock(2)阻止脚本两次启动吗? [英] Can I prevent a script from launching twice using open(2) with O_CREAT and flock(2)?

查看:57
本文介绍了我可以使用open(2)和O_CREAT和flock(2)阻止脚本两次启动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用PID文件来阻止脚本两次启动。有许多方法可以实现排他性,但是由于我的脚本将始终在Linux机器上运行,并且我希望能够自动检测过时的PID文件,因此我想使用 flock(2)来实现此目的。

I would like to prevent a script from launching twice by using a PID file. There are many ways to implement exclusivity, but since my script will always run on a Linux machine and I would like to be able to detect stale PID files automatically, I would like to use flock(2) to implement this.

很久以前,一位同事告诉我,以下伪代码是实现此目的的正确方法( open(...,'w')的意思是使用 O_CREAT 以写模式打开):

I was told long ago by a colleague that the following pseudocode is the right way to do this (open(..., 'w') means "open in write mode with O_CREAT"):

fd = open(lockfile, 'w');
write(fd, pid);
close(fd);
fd = open(lockfile);
flock(fd)
file_pid = read(fd)
if file_pid != pid:
    exit(1)
// do things

我很好奇为什么他建议使用上述内容,而不是:

I am curious why he suggested the above instead of:

fd = open(lockfile, 'w')
flock(fd)
// do things

大概是因为他认为这是因为 open(2)的如果不存在就创建文件功能 O_CREAT 并不是原子的,也就是说,两个完全同时调用 open(2)的进程可能会

Presumably he suggested this because he thought the "create file if it doesn't exist" functionality of open(2) with O_CREAT is not atomic, that is, two processes who call open(2) at exactly the same time might get handles to two different files because the file creation is not exclusive.

我的问题是,后者的代码在Linux系统上是否总是正确的?

My question is, is the latter code always correct on a Linux system, or if not, when is it not correct?

推荐答案

群不是100%可靠: http://en.wikipedia.org/wiki/File_locking#Problems

flock is not 100% reliable: http://en.wikipedia.org/wiki/File_locking#Problems

第一个方法是宁可从某种意义上讲,此方法具有侵入性,因为该过程的后续调用可能会盲目覆盖前一次调用写入的pid数据,从而有效地阻止了第一个进程的运行。

The 1st recipe is rather intrusive in the sense that a subsequent invocation of the process could blindly overwrite the pid data written by the previous invocation effectively preventing the 1st process from running. At high repeated invocation rates it's thus possible for none of the processes to run.

要确保文件创建的排他性,请使用O_CREAT | O_EXCL。您需要处理不合时宜的进程死亡,将文件丢在后面。

To ensure file creation exclusivity use O_CREAT | O_EXCL. You'd need to handle untimely process death leaving the file behind, tho.

我建议使用2个文件:


  • 使用O_CREAT打开的锁定文件| O_EXCL仅用于保护实际的PID文件操作,应该只存在很短的时间,可以根据创建时间轻松确定是否过时。

  • 实际的PID文件

每个进程等待锁文件消失(变旧后将其清除),然后尝试创建锁文件(只有一个实例成功,其他人等待),检查PID文件的存在/内容(如果过时,则清理并删除),如果决定运行则创建一个新的PID文件,然后删除锁定文件并按照决定运行/退出。

Each process waits for the lock file to disappear (cleans it when it becomes stale), then attempts to create the lock file (only one instance succeeds, the others wait), checks the PID file existence/content (cleans up and deletes it if stale), creates a new PID file if it decides to run, then deletes the lock file and runs/exits as decided.

这篇关于我可以使用open(2)和O_CREAT和flock(2)阻止脚本两次启动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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