2012-05-08 07:47 阅读 6


I locked a file and I am trying to test how the system reacts to a locked file. I would like other processes to wait for the process to be released until they can require_once that file again and not to come back with an error.

The problem is no matter what I do, I don't seem to be able to lock the file and do some waiting while another process asks for the file, and see the reaction of the system to that locked file.

I tried
1. sleep() which doesn't work, it halts the whole php engine.
2. I created a batch file that simply pauses until you hit enter. and used exec("cmd.exe /c START c:\\1.bat");

So the hunch is that all processes are actually being one serial queue of tasks, so that if one is paused for any reason, so are all others ?

so How can I test if a file is locked ?
But of course if I am right, what's the point in locking the file whatsoever if other processes can't access it anyway?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanfu7004 duanfu7004 2012-05-08 08:16
    $file = fopen('somefile.php', 'r+');
    while(!flock($file, LOCK_SH | LOCK_NB)) {
    require 'somefile.php';

    Since flock doesn't seem to work within php scripts, you need to check if a lock is present.

    点赞 评论 复制链接分享
  • dsqe46004 dsqe46004 2012-05-08 08:51

    the require_once is handled by php. Because of that, you cannot simply acquire a lock for it AFAIK. If you look at file locking, refer to this
    It states that you should use locks whenever you try to access the locked file.

    点赞 评论 复制链接分享