| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2005-05-13 16:03 UTC] sniper@php.net
  [2005-05-14 01:09 UTC] jaa at interflow dot dk
  | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 05:00:01 2025 UTC | 
Description: ------------ flock($fp, LOCK_EX | LOCK_NB) returns true even when $fp is already locked. This happens both on FreeBSD 5.4 and Gentoo Linux and both PHP 5.0.2, 5.0.3 and 5.0.4 exhibits this behaviour but PHP 4.3.11 works as expected. The blocking version: flock($fp, LOCK_EX) works as expected. The workaround is to check the value of the "wouldblock" argument to flock() but this shouldn't be necessary and is not documented in the manual. Reproduce code: --------------- <?php $fp = fopen("/tmp/lock.txt", "w+"); if (flock($fp, LOCK_EX | LOCK_NB )) { print "Got lock @ ".date('H:i:s'); while(1) {} } else { echo "Couldn't lock the file."; } fclose($fp); ?> Expected result: ---------------- When a file is already locked flock($fp, LOCK_EX | LOCK_NB) should return false. First terminal: % ~/php-5.0.4/sapi/cli/php flock.php 15:18:34 Got lock @ 15:18:34 Second terminal: % ~/php-5.0.4/sapi/cli/php flock.php 15:18:33 Couldn't lock the file. Actual result: -------------- First terminal: % ~/php-5.0.4/sapi/cli/php flock.php 15:18:34 Got lock @ 15:18:34 Second terminal: % ~/php-5.0.4/sapi/cli/php flock.php 15:18:33 Got lock @ 15:18:37