|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-11-15 20:01 UTC] chris at beckr dot org
[2009-11-16 11:19 UTC] vrana@php.net
[2009-11-16 11:20 UTC] svn@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 10 10:00:02 2026 UTC |
Description: ------------ Example 1 of the flock function documentation shows how to lock a file for writing. Once the lock has been obtained, the file is ftruncate()d and date is written. However, the file is opened using "w", which means that the file will already be truncated even before the lock is issued, which means that any readers (LOCK_SH) that validly obtain a lock in the meantime will see an empty file. Using "r+" instead of "w", everything works as expected. Reproduced on OS X (HFS+) and Linux (ext3). Reproduce code: --------------- --- From manual page: function.flock#Examples --- A sample reader counterpart: <?php while (true) { $fp = fopen("/tmp/lock.txt", "r"); if (flock($fp, LOCK_SH)) { // open for reading $contents = ''; while (!feof($fp)) { $contents .= fread($fp, 8192); } echo($contents); flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; } fclose($fp); } ?> Expected result: ---------------- The reader should print only lines with "Write something here" Actual result: -------------- The reader sometimes prints empty lines