search

Home  >  Q&A  >  body text

Confused about the exclusive lock of PHP file lock?

flock.php
<?php

$fp=fopen('lock.txt','a');

if (flock($fp,LOCK_EX)){

  fwrite($fp," HHHHHHHHHHHHHH".time()."\r\n");

}

?>
flock2.php
<?php

$fp=fopen('lock.txt','a');

if (flock($fp,LOCK_EX)){

  fwrite($fp," GGGGGGGGGGGGGG".time()."\r\n");

}

?>
Doubt:
Why is the flock.php file locked with an exclusive lock, but the lock is not released and the fclose file is not closed. But when executing flock2.php file, it can still be written.
Isn’t the exclusive lock of the file lock unable to be written by other processes after it is locked? Can I write again only when the lock is released?

曾经蜡笔没有小新曾经蜡笔没有小新2877 days ago755

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 13:17:13

    php is over after execution and the resources are released, and the lock will be released naturally

    If you want to test, add an infinite loop to flock.php to ensure it never exits, and then execute flock2.php to see the effect

    reply
    0
  • Cancelreply