a Post-script to flocking question

D

Dick Rosser

Sorry, I also meant to ask if there is any
error trapping with flock. If a second person
accesses the database befor the first persons lock
is in place, is the database corrupted. How does one
work around this?

Regards
Dick Rosser
webmasterone@kidwatch-uk,net
 
T

Tassilo v. Parseval

Also sprach Dick Rosser:
Sorry, I also meant to ask if there is any
error trapping with flock. If a second person
accesses the database befor the first persons lock
is in place, is the database corrupted. How does one
work around this?

You don't, but flock() does. This is exactly the scenario it is used
for. Since locks are usually only advisable, both parties accessing the
database have to use flock(). If there is already a lock on it, the
other process will block until the lock is released. You can also do
non-blocking locks in which case flock() returns a false value if the
file is already locked:

use Fcntl qw:)flock);
...
flock FH, LOCK_EX|LOCK_NB or die $!;

This can be used for allowing the waiting processes do something useful
in the meantime:

while (! flock FH, LOCK_EX|LOCK_NB) {
warn $!;
# do some useful stuff here
}
# and now we have the lock and can work with the file

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top