note 81665 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:34:07 +0000 |
| Subject: | note 81665 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155388@lists.php.net to get a copy of this message | ||
Note Submitter: Daniel Gandra - dgandra at gmail dot com
----
I made some tests with flock () features. I made them because I read on a lot of articles that flock
() doesn't work as expected and doesn't block against another processes.
I made the tests with all operation options:
2: shared lock (read mode)
3: exclusive lock (write mode)
5: shared lock (read mode), without blocking the script execution
6: exclusive lock (write mode), without blocking the script execution
All of them worked perfect on windows xp and php 5.2.5
note: when I say "without blocking the script execution" means flock () tries to block
and if it can't, it doesn't stay waiting to acquire the lock, the php script continues
running, and yeah, in windows even!
I tested with notepad too while the script had locked the file, to see what happened if I made a
save on the locked file and... I can't, notepad alerted me: another proccess has the file
locked!
The code used is the following:
<?PHP
$content = "";
$input = fopen ("lock_test.txt", "w");
if (flock ($input, 6) == true){
echo "in...<br/>";
sleep (5);
echo "out...<br/>";
flock ($input, 3);
}
else echo "can't acquire block...";
fclose ($input);
//sleep () was used to give myself enough time to switch to another browser window,
//invoke the script again to guarantee parallelism and make the notepad test to see
//if an error occurred with other proccess
?>