note 63832 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:38:42 +0000 |
| Subject: | note 63832 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155401@lists.php.net to get a copy of this message | ||
Note Submitter: heshan at sjtu dot edu dot cn
----
dranger at export dash japan dot com 's suggestion is correct and useful. My purpose is just
preventing any other process from disturbing the current process which is rewriting a
"cache" file, and flock() itself can help me to achieve this.
$fp = fopen( "test.txt", "ab" );
if( $fp && flock( $fp, LOCK_EX ) ) {
ftruncate( $fp, 0 );
fwrite( $fp, $part_one );
sleep( 10 ); // for test purpose, assume the whole writing process takes 10 seconds
fwrite( $fp, $part_two );
fclose( $fp );
}
it works well, I open 3 browser windows to request this page, and the third one actually takes more
than 20( about 26 ) seconds to finish. and the content of cache.txt is correct and up-to-time.
tested under:
1, windows, apache2, php4.4.1
2, freebsd, apache, php4.2.3