From:             commander at graphicore dot de
Operating system: WinXP
PHP version:      4.4.7
PHP Bug Type:     Filesystem function related
Bug description:  flock() ignores third argument $wouldblock

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

-- 
Edit bug report at http://bugs.php.net/?id=41769&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=41769&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=41769&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=41769&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=41769&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=41769&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=41769&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=41769&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=41769&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=41769&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=41769&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=41769&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=41769&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=41769&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=41769&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=41769&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=41769&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=41769&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=41769&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=41769&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=41769&r=mysqlcfg

Reply via email to