bash群:为什么200? [英] bash flock: Why 200?

查看:49
本文介绍了bash群:为什么200?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于该线程: bash群:如果无法获取锁则退出

如果有人能向我解释"200"代表什么,我将不胜感激.

I'll appreciate if someone can explain to me what does the '200' stand for.

我已经读过有关flock的信息,似乎要指定200作为文件描述符,但是这个数字有什么用呢?

I've read about flock and it seems that 200 if to specify a File Descriptor, but what is so good about this number?

推荐答案

数字200没什么特别的.它恰好是 flock 命令的手册页中使用的示例;而且它的数量很大,因此它不太可能与您在脚本执行期间打开的任何其他文件的文件描述符冲突.

Theres nothing special about the number 200. It just happens to be the example used in the man page of the flock command; and it happens to be a large number, so it's unlikely to conflict with the the file descriptor of any other file you open during your script.

在您的评论中,您询问以下问题:

In your comment, you ask about:

( 
  flock -e 200
  echo "In critical section"
  sleep 5 
) 200>/tmp/blah.lockfile 
echo "After critical section"

括号()创建一个子外壳;与父流程分开的新流程. 200>/tmp/blah.lockfile 导致该进程在文件描述符200上打开/tmp/blah.lockfile 进行写入.括号内的命令被执行.在该外壳内.

The parentheses () create a subshell; a new process, separate from the parent process. The 200>/tmp/blah.lockfile causes that process to open up /tmp/blah.lockfile for writing, on file descriptor 200. The commands inside the parentheses are executed within that shell.

flock -e 200 在文件描述符200指向的文件上获得排他锁.排他锁是指试图获得对该文件的排他锁或共享锁的任何其他人,将阻塞(等待),直到该锁被释放为止;否则,如果它们超时或被要求不阻塞,则将失败.因此,在子外壳程序主体的其余部分( echo sleep 命令)中,该子外壳程序将持有该锁,而没有其他人可以获得该锁.子shell完成后,文件将关闭并放弃锁定.

flock -e 200 obtains an exclusive lock on the file pointed to by file descriptor 200. An exclusive lock means that anyone else who tries to obtain a lock on that file, either exclusive or shared, will block (wait) until this lock has been relinquished, or fail if they hit a timeout or asked not to block. So during the remainder of the body of the subshell (the echo and sleep commands), the lock will be held by that subshell, and no one else can obtain that lock. Once the subshell finishes, the file will be closed and lock relinquished.

这篇关于bash群:为什么200?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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