For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > June 2007 > #41769 [Opn]: flock() ignores third argument $wouldblock









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #41769 [Opn]: flock() ignores third argument $wouldblock
tony2001@php.net

2007-06-25, 7:05 pm

ID: 41769
Updated by: tony2001@php.net
Reported By: commander at graphicore dot de
Status: Open
-Bug Type: Filesystem function related
+Bug Type: Documentation problem
Operating System: WinXP
PHP Version: 4.4.7
New Comment:

Seems to be docu problem, reclassified as such.


Previous Comments:
------------------------------------------------------------------------

[2007-06-21 18:52:55] commander at graphicore dot de

Little typo:
with "LOCK_EX + LOCK_NB" OR "LOCK_SH + LOCK_NB"

------------------------------------------------------------------------

[2007-06-21 18:23:02] 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


------------------------------------------------------------------------


--
Edit this bug report at http://bugs.php.net/?id=41769&edit=1
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com