note 45464 added to function.flock
| From: | rudy dot metzger at pareto dot nl | Date: | Wed, 08 Sep 2004 10:31:56 +0000 |
| Subject: | note 45464 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76218@lists.php.net to get a copy of this message | ||
Like a user already noted, most Linux kernels (at least the Redhat ones) will return false, even if
you locked the file. This is because the lock is only ADVISORY (you can check that in /proc/locks).
What you have to do there is to evalute the 3rd parameter of flock(), $eWouldBlock. See for an
example below. Note however that if you
lock the file in non blocking mode, flock() will work as expected (and blocks the script).
<?php
$fp = fopen( "/var/lock/process.pid", "a" );
if ( !$fp || !flock($fp,LOCK_EX|LOCK_NB,$eWouldBlock) || $eWouldBlock ) {
fputs( STDERR, "Failed to acquire lock!\n" );
exit;
}
// do your work here
fclose( $fp );
unlink( "/var/lock/process.pid" );
?>
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45464
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45464&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45464&report=yes
Search -- http://master.php.net/manage/user-notes.php