Re: Flock and file deletion
- From: gordonb.fmd30@xxxxxxxxxxx (Gordon Burditt)
- Date: Mon, 29 Sep 2008 20:29:23 -0500
I am wondering if this is possible. I have a file that is accessed by
multiple users and keeps track of activity (the file is polled by
flash every 2 seconds). As users leave, the flash program tells the
php program to remove them from the activity file, and as users access
this activity file, users who have timed out (haven't been heard from
in 60 seconds) are removed. Using a combination of flock, microsecond
sleeps, loops and the flash file repolling if it doesn't get a
completed result back, I have been able to keep anyone from stepping
on each other.
I presume here that two instances of the same user in the file is
something that must be avoided, as it constitutes "stepping on each
other". Or if you mean two instances of *any* two users constitutes
"stepping on each other", that's just as bad, if not worse.
flock() locks a file. It does not lock a file *name*. If someone
takes the old file and does a rename() or remove() on the file or
rename()s the directory structure above it, you'll end up with a
locked old file under the wrong name (or no name), and later someone
may re-create a new file under the correct name. There will be two
different files, and you won't see locks set in one on the other
one. I suggest you think *VERY* carefully about deliberately
deleting the lock file, ever. Truncating the lock file to zero
length may be acceptable.
The question I have is, when the last user exits, I want to delete the
file, but am afraid that the file deletion may step on a user just
coming in and remove their activity.
Yes, as you describe it, this is a real problem. You may not see
screwups very often in practice, but they are possible.
I had the thought of, and this
may be totally stupid, to open the file for read, flock it and delete
it while open. The theory behind this is that the flock would stay
active till the php script ended and the file handle was closed. At
which point the other waiting user would be able to access the file
for writing. Does this make sense?
Nice try, but it has holes in it. You failed to explain when
the file would get re-created after it is deleted. Even then,
consider this:
Process A tries to delete the last user, A from the file.
Process B tries to add a user B to the file.
Process C tries to add the same user B to the file.
A opens old file.
A locks old file.
A observes that A is the last user in the file.
B opens old file.
B tries to lock old file, finds it busy, and waits. (Or perhaps B doesn't
try to lock the file until A has closed/unlocked it).
A deletes old file.
A closes/unlocks old file.
B succeeds in locking old file.
B adds user B to the old file.
B unlocks old file.
C tries to open file, fails, noting it is not there, and re-creates it.
C locks new file.
C adds user B to the new file.
C unlocks new file.
Now you have two processes who each think they successfully added
user B to the file. Do I hear feet being stepped on?
.
- References:
- Flock and file deletion
- From: Bill H
- Flock and file deletion
- Prev by Date: Flock and file deletion
- Next by Date: Re: php jobs
- Previous by thread: Flock and file deletion
- Next by thread: Re: Flock and file deletion
- Index(es):
Relevant Pages
|