note 62568 added to function.flock
| From: | NielsJaeckel at osu1 dot php dot net | Date: | Fri, 03 Mar 2006 15:43:34 +0000 |
| Subject: | note 62568 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104776@lists.php.net to get a copy of this message | ||
hi dranger at export dash japan dot com,
you are right with your thoughts about process-switching. But I think the following kind of code
would work in race-conditions, because it is very improbably that the CPU switches after just ONE
statement:
<?php
// here my own lock-function WITHOUT flock()
// you can call it a mutex...
// lock ... compare to P(mutex)
function lock($filename) {
$filename .= '.lock';
while (true) {
if (! file_exists($filename)) {
if (! file_exists($filename)) {
if (! file_exists($filename)) {
return touch($filename);
}
}
}
}
}
// unlock ... compare to V(mutex)
function unlock($filename) {
unlink($filename . '.lock');
}
?>
If you want to solve the reader/writer problem (many readers, just one exclusive writer) you can add
some flock() - code instead of touch(...)
hth
----
Server IP: 66.163.161.117
Probable Submitter: 84.179.211.73
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/note/edit/62568
Del: integrated -- http://master.php.net/note/delete/62568/integrated
Del: useless -- http://master.php.net/note/delete/62568/useless
Del: bad code -- http://master.php.net/note/delete/62568/bad+code
Del: spam -- http://master.php.net/note/delete/62568/spam
Del: non-english -- http://master.php.net/note/delete/62568/non-english
Del: in docs -- http://master.php.net/note/delete/62568/in+docs
Del: other reasons-- http://master.php.net/note/delete/62568
Reject -- http://master.php.net/note/reject/62568
Search -- http://master.php.net/manage/user-notes.php