douyun6399
2015-05-07 09:48
浏览 276
已采纳

flock()总是返回true

Been over this for the last hour, and cant really figure out whats wrong.

This is the code I was using to debug the issue.

Monitor.php

$fp = fopen("lock.txt", "r+");
$var=flock($fp, LOCK_EX | LOCK_NB);
var_dump($var);
$var=flock($fp, LOCK_SH | LOCK_NB);
var_dump($var);
exit;

locker.php

$fp = fopen("lock.txt", "r+");
sleep(60);

To see if flock is working properly, I first run the locker.php, so that file will be locked for 60 seconds, and I then try running the monitor.php to see if I can get a lock.

I get TRUE for both exclusive and shared lock.

What would be the problem?

Solution : Assumed opening the file would lock it. The locker should issue an flock to lock the file.

Missed that part in the hurry. Deadlines.Deadlines :)

图片转代码服务由CSDN问答提供 功能建议

过去一小时已经结束了这一点,并且无法弄清楚什么是错的。

这是我用来调试问题的代码。

Monitor.php

  $ fp = fopen(“lock  .txt“,”r +“); 
 $ var = flock($ fp,LOCK_EX | LOCK_NB); 
var_dump($ var); 
 $ var = flock($ fp,LOCK_SH | LOCK_NB); 
var_dump($  var); 
exit; 
   
 
 

locker.php

  $ fp = fopen(“lock.txt”  ,“r +”); 
sleep(60); 
   
 
 

要查看flock是否正常工作,我首先运行locker.php,以便该文件将是 锁定了60秒,然后我尝试运行monitor.php以查看是否可以锁定。

我获得了独占和共享锁的 TRUE

会出现什么问题?

解决方案:假设打开文件会将其锁定。 储物柜应该发出一个羊群来锁定文件。

匆忙错过了那个部分。 Deadlines.Deadlines:)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drdu53813 2015-05-07 09:54
    已采纳

    locker.php is not locking the file. fopening a file will not lock it. flock is a cooperative advisory locking system. All parties need to participate in it and use flock to lock and/or check for locks. Unless locker.php explicitly flocks the file it is not locked.

    打赏 评论
  • dongshadu2546 2015-05-07 09:55

    Under PHP (and many UNIX systems), flock is advisory, meaning that all processes that want to use the file must use locking. Those that don't can still do whatever they want to the file and will not prevent other processes from obtaining locks.

    Your locker program should both open and lock the file, such as:

    $fp = fopen("lock.txt", "r+");
    $var=flock($fp, LOCK_SH | LOCK_NB);
    sleep(60);
    
    打赏 评论

相关推荐 更多相似问题