note 29811 added to function.flock
| From: | derf at rack1 dot php dot net | Date: | Tue, 25 Feb 2003 21:27:52 +0000 |
| Subject: | note 29811 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44516@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.flock.php
http://master.php.net/manage/user-notes.php?action=edit+29811
http://master.php.net/manage/user-notes.php?action=delete+29811
http://master.php.net/manage/user-notes.php?action=reject+29811