Re: Practical flock() implementation
| From: | Manuel Lemos | Date: | Sat, 23 Jun 2001 21:08:45 +0000 |
| Subject: | Re: Practical flock() implementation | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-54992@lists.php.net to get a copy of this message | ||
Hello CC,
On 23-Jun-01 17:25:54, you wrote:
>I'm having trouble understanding the docs and annotations on flock(). I've
>got a script that needs to overwrite a data file's contents without letting
>Instance B of that script truncate the data file while Instance A is still
>writing new content to it. How do other people do this? It seems to me
Let me guess: you are developing a cache system, right? :-)
>that if I fopen() in "w" or "w+" mode, then the file will be truncated
>before it can be determined whether there's a lock already in effect or the
>current instance can acquire a lock; but flock() requires a filepointer
>argument, so don't I *have* to fopen the file before going to flock()? Or
>should I fopen first in "r" mode, acquire the lock, then re-open in "w"
>mode, counting on the lock to stay in effect even though it's a different
>file pointer?
I know that second solution works, but I don't know about its portability.
>I'd also be grateful if someone could clarify the difference between
>blocking and locking (i.e. "If you don't want flock() to block while
>locking, add LOCK_NB (4 prior to PHP 4.0.1) to operation.").
If you don't want to wait in case it is not possible to obtain the lock
right away, using non-blocking locks.
Regards,
Manuel Lemos
Web Programming Components using PHP Classes.
Look at: http://phpclasses.UpperDesign.com/?user=mlemos@acm.org
--
E-mail: mlemos@acm.org
URL: http://www.mlemos.e-na.net/
PGP key: http://www.mlemos.e-na.net/ManuelLemos.pgp
--