note 5496 deleted from function.flock by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 19:08:14 +0000 |
| Subject: | note 5496 deleted from function.flock by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22076@lists.php.net to get a copy of this message | ||
In response to the very important issues above:
This kludge seems to work safely:
===Script 1 (reader)======
$fd = fopen('/tmp/php_flock_test', 'r');
flock($fd,1); #Read lock
sleep(60); #For testing purposes
$contents = fread ($fd, filesize('/tmp/php_flock_test') );
fclose($fd);
=====================
=====Script 2 (writer)==========
#Dummy initial filehandle to obtain lock via
$fd = fopen('/tmp/php_flock_test', 'a');
$locked = flock($fd,2); #Exclusive blocking lock
if (!$locked) {
print "Error: timeout obtaining lock"; exit;
}
#Second (actually used) filehandle
$fdsafe = fopen('/tmp/php_flock_test', 'w');
fwrite($fdsafe,'Replacement text');
fclose($fdsafe);
fclose($fd);
=================
The 'fake' append seems to do no harm - tested it and it works fine.