本文介绍了请问这个典型的例子羊群工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个人必须通过同步文件系统程序(shell脚本),我发现了一个羊群基于解决方案是的(也可以工作在NFS )是:

When one must synchronize programs (shell scripts) via file system, I have found an flock-based solution to be recommended (should also work on NFS). The canonical example for usage from within a script (from http://linux.die.net/man/1/flock) is:

(
flock -s 200

# ... commands executed under lock ...

) 200>/var/lock/mylockfile

我不太明白为什么这个整体结构可以确保原子性。我特别想知道以何种顺序羊群-s 200 200 GT;的/ var /锁/ mylockfile 执行当如庆典执行code的这些行。这是为了保证/确定性?我的理解是,它的必须的是确定性的,如果这个成语应该工作。但是,由于一个子外壳是一个子进程产生了,我不明白这两个过程是如何同步自己。我只看到已经这两个命令之间的竞争条件。

I don't quite get why this whole construct ensures atomicity. In particular, I am wondering in which order flock -s 200 and 200>/var/lock/mylockfile are executed when e.g. bash executes these lines of code. Is this order guaranteed/deterministic? The way I understand it, it must be deterministic if this idiom should work. But since a sub shell is spawned in a child process, I do not understand how these two processes synchronize themselves. I only see a race condition between these two commands already.

我想AP preciate如果有人可以做出我的困惑这个消失,说明为什么这个结构可以用来安全地同步过程。

I would appreciate if someone could make my confusion about this disappear and explain why this construct can be used to safely synchronize processes.

同时,如果有人知道,我会很感兴趣的是如何安全的选择只是一些任意的文件描述符(如 200 中的例子),尤其是在一个大的NFS文件系统与许多客户的上下文

At the same time, if someone knows, I would be interested in how safe it is to chose just some arbitrary file descriptor (such as 200 in the example), especially in the context of a large NFS file system with many clients.

推荐答案

子shell的整个I / O方面(...)200 GT;的/ var /锁/ mylockfile 必须评估 - 和I / O重定向进行 - 之前的任何命令可以在子shell执行,因此,重定向总是precedes的羊群-s 200 。想想如果子shell都有其标准输出管道到另一个命令;该管具有在创建子壳之前被创建。这同样适用于文件描述符200重定向。

The whole I/O context of the sub-shell (...) 200>/var/lock/mylockfile has to be evaluated — and the I/O redirection done — before any commands can be executed in the sub-shell, so the redirection always precedes the flock -s 200. Think about if the sub-shell had its standard output piped to another command; that pipe has to be created before the sub-shell is created. The same applies to the file descriptor 200 redirection.

的文件描述符数的选择确实没有丝毫关系 - 超越最好不要使用文件描述符0-2(标准输入,输出,错误)。文件名的事项;不同的进程可以使用不同的文件描述符;只要名字商定,应该没事。

The choice of file descriptor number really doesn't matter in the slightest — beyond it is advisable not to use file descriptors 0-2 (standard input, output, error). The file name matters; different processes could use different file descriptors; as long the name is agreed upon, it should be fine.

这篇关于请问这个典型的例子羊群工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:17