Linux LOCK_NB 在羊群中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5507185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:29:14  来源:igfitidea点击:

What does LOCK_NB mean in flock?

phplinux

提问by Rami Dabain

What does LOCK_NBmean in the PHP flockcommand?

LOCK_NBPHPflock命令中的意思是什么?

采纳答案by mario

LOCK_NBmeans non-blocking.

LOCK_NB表示非阻塞。

Usually when you try to lock a file, your PHP script execution will stop. The call to flock()then blocksit from resuming. It does so until a concurrent lock on the accessed file is removed.

通常,当您尝试锁定文件时,您的 PHP 脚本执行将停止。要将呼叫flock()然后从恢复它。它会一直这样做,直到被访问文件的并发锁被移除。

Mostly your process is the only one trying to lock the file, so the blocking call to flockactually returns instantly. It's only if two processes lock the very same file, that one of them will be paused.

大多数情况下,您的进程是唯一一个试图锁定文件的进程,因此对阻塞的调用flock实际上会立即返回。只有当两个进程锁定同一个文件时,其中一个进程才会被暂停。

The LOCK_NBflag however will make flock()return immediately in any case. In that setting you have to check the returned status to see if you actually aquired the lock. As example:

LOCK_NB旗但是会让flock()立即返回在任何情况下。在该设置中,您必须检查返回的状态以查看您是否确实获得了锁。例如:

while ( ! flock($f, LOCK_NB) ) {
    sleep(1);
}

Would more or less emulate the behaviour of the normal blocking call. The purpose of course is to do something else / meaningful (not just wait) while the file is still locked by another process.

或多或少会模拟正常阻塞调用的行为。当然,目的是在文件仍被另一个进程锁定时做其他/有意义的事情(不仅仅是等待)。