Question about flock() on Linux
| From: | Andrew Sitnikov | Date: | Sat, 03 Feb 2001 19:37:49 +0000 |
| Subject: | Question about flock() on Linux | ||
| Groups: | php.dev php.general | ||
| Request: | Send a blank email to php-general+get-37988@lists.php.net to get a copy of this message | ||
Hello php-general,
Script flock.php:
<?
$file_name = 'flock.txt';
for($i=0;$i<1000;$i++){
if ($fd = fopen($file_name,'a+')){
if (flock($fd,LOCK_EX)){
fseek($fd,0,SEEK_END);
fwrite($fd,$i.' : '.$i."\n");
flock($fd,LOCK_UN);
}else{
die('Can not lock file');
}
fclose($fd);
}else{
echo 'Can not open file';
}
}
?>
Run it: ab -n 10 -c 10 http://host/flock.php
Why file have similar lines ?
432 : 431 : 431
432
433 : 433
...
84841 : 841
2 : 842
May be flock not working or I do not understand, how it should work ?
How correctly to write in a file from different threads?
P.S I use Linux-2.2.17+PHP4.0.4pl1
Best regards,
Andrew Sitnikov
e-mail : sitnikov@infonet.ee
GSM: (+372) 56491109