dtslobe4694 2012-05-08 07:47
浏览 7
已采纳

测试锁定文件

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 2012-05-08 08:16
    关注
    $file = fopen('somefile.php', 'r+');
    
    while(!flock($file, LOCK_SH | LOCK_NB)) {
        sleep(1);
    }
    
    require 'somefile.php';
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?