php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41769 flock() ignores third argument $wouldblock
Submitted: 2007-06-21 18:23 UTC Modified: 2007-08-17 12:54 UTC
From: commander at graphicore dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.4.7 OS: WinXP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: commander at graphicore dot de
New email:
PHP Version: OS:

 

 [2007-06-21 18:23 UTC] commander at graphicore dot de
Description:
------------
Different behaviors of flock between WinXp and Linux(and FreeBSD) if you use the optional third Argument ($wouldblock) with "LOCK_EX + LOCK_NB" OR "LOCK_EX + LOCK_NB"

If this is no bug please correct the documentation.

Versions of PHP tested
Win:
PHP 4.4.4
PHP 4.4.7
PHP 5.1.6
PHP 5.2.2
(all Windows versions do the same)

Linux
PHP 4.4.4
FreeBSD
PHP 4.4.6
(both Linux and FreeBSD do the same)


Reproduce code:
---------------
<?php
/*
Here's my Code, run it from two different browsers or from two different files because if you request the same file twice at the same time modern browsers wait for the first request to finish until sending the seccond.(IE7 does not wait therefore its not modern):
*/
$starttime = time();
$date = date('h:i:s',$starttime);
echo 'start:'.$date.'<br />';

$handle = fopen('test.txt','ab');
if($handle){
    $wouldblock = NULL;
    $flock = flock($handle,LOCK_EX + LOCK_NB,$wouldblock);
    echo '$flock '; var_dump($flock);echo '<br />';
    echo '$woulblock '; var_dump($wouldblock);echo '<br />';
    if(!$flock){
        echo 'no lock for me<br />';
    }else{
        echo 'got a lock<br />';
        fwrite( $handle, ' start '.$date.' ' );
        sleep(5); //assume the whole writing process takes 5 seconds
        fwrite( $handle, ' end '.$date."\n" );
    }
    fclose($handle);
}
echo 'end: this took '.(time()-$starttime).' secconds';
?>

Expected result:
----------------
Linux will if the file is locked set $wouldblock to int(1) and always return TRUE.

Output Linux (and FreeBSD):
###first process:
start:06:05:09
$flock bool(true)
$woulblock int(0)
got a lock
end: this took 5 secconds

###seccond Process:
start:06:05:11
$flock bool(true) 
$woulblock int(1) 
got a lock
end: this took 5 secconds

###test.txt
 start 06:05:09  start 06:05:11  end 06:05:09
 end 06:05:11

Actual result:
--------------
Windows will set $wouldblock always to int(0) and if the file is locked return FALSE.

Output WinXp:
###first process:
start:06:09:22
$flock bool(true) 
$woulblock int(0) 
got a lock
end: this took 5 secconds

###seccond Process:
start:06:09:24
$flock bool(false)
$woulblock int(0)
no lock for me
end: this took 0 secconds

###test.txt
 start 06:09:22  end 06:09:22

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-21 18:52 UTC] commander at graphicore dot de
Little typo:
with "LOCK_EX + LOCK_NB" OR "LOCK_SH + LOCK_NB"
 [2007-06-25 17:56 UTC] tony2001@php.net
Seems to be docu problem, reclassified as such.
 [2007-08-16 13:46 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"LOCK_NB not supported on Windows"
 [2007-08-17 12:54 UTC] commander at graphicore dot de
LOCK_NB works fine I think, the third argument &$wouldblock is not supported
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 29 03:01:29 2024 UTC