note 29811 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:49:23 +0000 |
| Subject: | note 29811 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155438@lists.php.net to get a copy of this message | ||
Note Submitter: derf (at) tamu (dott) edu
----
This addresses a few topics posted earlier:
First (to sbsaylors), using a seperate lockfile has problems since you must seperately check the
lockfile, then create the lockfile, then perform your action. In the event of two processes running
at the same time, you may wind up in the situation where there is no file and execution proceeds as
follows:
A:checks-for-file
B:checks-for-file
A:creates-file
B:creates-file
A:open-file
B:open-file
A:writes
B:writes
... now, both A and B have written different things to the file, which in most systems would be
buffered in seperate memory for A and B. Thus, if A closes first, when B closes its buffer will
overwrite A. If B closes first, A will overwrite B.
Other situations may arise where everything looks ok, because B's entire execution happens
before A opens the file. But then, B will delete the lock file and A will be unprotected.
Second (to the nameless person): flock doesn't exist to guarantee that your program
doesn't stop running. It only makes sure that other programs that bother to flock() won't
mess with the file while you're messing with it.
Proper method to do this would be to write into a temporary file, then copy that file over the
original in a single operation. If the execution stops, only the temp file is corrupt.