Date: 09/30/98
- Next message: Martin Pool: "[PHP3] Re: php3 Digest 30 Sep 1998 10:00:00 -0000 Issue 457"
- Previous message: Rich Clingman: "[PHP3] Attach Files -- This time it works!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 30 Sep 1998 01:06:00 -0700
To: php3 <email protected>
From: pnet <email protected>
Subject: [PHP3] File Locking Question
Message-Id: <v02140b04b2371ee7e825@[209.142.59.218]>
pnet> What is going to happen?
pnet> Will PHP report an error "unable to open file..." or will linux
pnet> suspend the CGI and keep trying to execute fopen() for awhile
pnet> before returning an error?
pnet> Do I need to handle such possibilities in my PHP script with
pnet> something like: while( ! fopen($fp, "w") );
There are a few levels to the answer:
Firstly, in general fopen does not do any locking: if two processes
open the file to write then they will both be able to write over each
other's data, which is probably a bad thing. (By the way, if you open
the file with mode "w" you will "truncate" the file, deleting any
existing contents.)
Secondly, you can do fs-level locking using flock(2) or fcntl(2),
which I imagine have PHP equivalents. They do
shared-read/exclusive-write locking of files. In this case, you'll
have to write your own code to cope with contention by backing off or
whatever.
Thirdly, if you expect to handle concurrent write access to files then
I'd strongly recommend you look at using a database library that can
handle that for you, because it's not a simple problem once you have
multiple resources or complex processing to do. Try
Sleepycat/Berkeley DB2.0 or an RDBMS.
pnet> My script needs to first read the file, then increment the
pnet> count before writing it back out. I need to be sure that
pnet> process2 doesn't try to read from the file after process1 has
pnet> read it, but before process1 can increment it and write the new
pnet> value back out to the file.
If your app is that simple, then flock and fopen will probably do fine
to get atomic counter updates. Pseudocode:
open file read/write
retries = 5
while retries-- > 0
try to get an exclusive lock on the file
if it was locked:
do your update
close & unlock
return
else
sleep one second
try again
log "can't lock the file!"
try to go on without it
-- Martin PoolAs for the M$ pundits claim that the problems result from an incorrect setup/configuration: they are perfectly correct, if you hadn't installed an M$ OS, then the crashes wouldn't be happening. -- Jeff Dutky on c.o.l.a
-- PHP 3 Mailing List http://www.php.net/ To unsubscribe send an empty message to php3-unsubscribe <email protected> To subscribe to the digest list: php3-digest-subscribe <email protected> For help: php3-help <email protected> Archive: http://www.php.net/mailsearch.php3
- Next message: Martin Pool: "[PHP3] Re: php3 Digest 30 Sep 1998 10:00:00 -0000 Issue 457"
- Previous message: Rich Clingman: "[PHP3] Attach Files -- This time it works!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

