bash 群:为什么是 200?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13551840/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash flock: Why 200?
提问by Subway
Regarding that thread: bash flock: exit if can't acquire lock
关于该线程: bash flock:如果无法获得锁则退出
I'll appreciate if someone can explain to me what does the '200' stand for.
如果有人能向我解释“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?
我读过关于 flock 的文章,如果要指定文件描述符,似乎是 200,但是这个数字有什么好处呢?
回答by Brian Campbell
Theres nothing special about the number 200. It just happens to be the example used in the man page of the flockcommand; 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.
数字 200 没有什么特别之处。它恰好是flock命令手册页中使用的示例;它恰好是一个很大的数字,因此它不太可能与您在脚本期间打开的任何其他文件的文件描述符发生冲突。
In your comment, you ask about:
在您的评论中,您询问:
( 
  flock -e 200
  echo "In critical section"
  sleep 5 
) 200>/tmp/blah.lockfile 
echo "After critical section"
The parentheses ()create a subshell; a new process, separate from the parent process. The 200>/tmp/blah.lockfilecauses that process to open up /tmp/blah.lockfilefor writing, on file descriptor 200. The commands inside the parentheses are executed within that shell.
括号()创建一个子shell;一个新的进程,与父进程分开。在200>/tmp/blah.lockfile这一进程中开拓事业/tmp/blah.lockfile的写作,文件描述符200括号内的命令是外壳内执行。
flock -e 200obtains 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 echoand sleepcommands), 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.
flock -e 200获得文件描述符 200 指向的文件的排他锁。如果他们超时或要求不要阻止。因此,在子shell 主体的其余部分(echo和sleep命令)期间,该锁将由该子shell 持有,其他人无法获得该锁。一旦子shell完成,文件将被关闭并释放锁定。

