Kathryn Cassidy said:
> Does flock work ok on windows as well as unix as the script has to run on
> both? I've heared rumours that it's a bit flaky on some systems.
I don't know, but I bet it doesn't ;)
Another possibility is to use O_EXCL with the POSIX open system call, you
can do this in Perl using "use POSIX;". A thread on Bugtraq discussed it
earlier this month IIRC.
A simple (ish) mechanism in conjunction with this is to use a file called
"userdata.lock" (assuming your user data file is called "userdata").
Create it using O_EXCL using the POSIX api -- this will ensure that your
proc has exclusive open rights on it -- retry if the open fails, and sleep
Once you get the file open, you can then write to the real userdata, then
delete the "userdata.lock" file. Since open(O_EXCL) and unlink() are both
atomic operations you're safe.
BTW to avoid causing deadlock if your proc gets killed, trap signals
appropriately, and also copy your pid into the .lock file; if you limit
the number of open-retries, and your CGI hits the limit, it can then read
the file and kill ($pid, 0) to test if it is still running. If it's not
still running then unlink the lock file and re-enter the retry loop.
Here's hoping the windows support for file locking works OK! ;)
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!