note 29500 modified in function.flock by betz
| From: | betz@php.net | Date: | Sat, 15 Feb 2003 20:01:34 +0000 |
| Subject: | note 29500 modified in function.flock by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-44024@lists.php.net to get a copy of this message | ||
[editors note: try out php.net/dio_fcntl]
As flock() doesn't work over NFS (whereas fcntl() does, but there's no PHP interface for
that), you may have to provide some sort of backup locking system if for one reason or other you
need to use NFS (or VFAT, but as the documentation points out it's antiquated). One way of
achieving this is to use a relational database. If you have a table T with an integer column cnt and
a _single_ row, you can:
UPDATE T set cnt = cnt + 1 WHERE cnt >= 0
to obtain a shared lock (and of course you need to verify that there was one affected row). For an
exclusive lock:
UPDATE T set cnt = -1 WHERE cnt = 0
This scheme has two problems: You rely on the script to be able to release locks before exiting, and
there is no way to wait for a lock (except polling). On the whole, if you can use flock() instead,
use flock().
--was--
As flock() doesn't work over NFS (whereas fcntl() does, but there's no PHP interface for
that), you may have to provide some sort of backup locking system if for one reason or other you
need to use NFS (or VFAT, but as the documentation points out it's antiquated). One way of
achieving this is to use a relational database. If you have a table T with an integer column cnt and
a _single_ row, you can:
UPDATE T set cnt = cnt + 1 WHERE cnt >= 0
to obtain a shared lock (and of course you need to verify that there was one affected row). For an
exclusive lock:
UPDATE T set cnt = -1 WHERE cnt = 0
This scheme has two problems: You rely on the script to be able to release locks before exiting, and
there is no way to wait for a lock (except polling). On the whole, if you can use flock() instead,
use flock().
http://www.php.net/manual/en/function.flock.php