in reply to Ways to sequence calls from multiple processes

Just a little reminder: you forgot to explicitly unlock the file after the operation. I think this may be optional, but IMHO it always a good thing to do it explicitly, so your code should look like:
open(FH, ">>lock.txt") or die "can't open lock.txt: $!\n"; flock(FH, 2) or die "Can't flock lock.txt: $!\n"; now_access_the_common_resource(); flock(FH, LOCK_UN); close(FH);
Best regards,

Replies are listed 'Best First'.
Re^2: Ways to sequence calls from multiple processes
by Anonymous Monk on Nov 28, 2004 at 23:17 UTC
    although, it may actually be better to not explicitly unlock in this case. For example, if someone else is waiting for the lock on FH, they might conceivably get the lock after the LOCK_UN step, but before the close(FH) AND therefore before the first process has flushed it's write operations to disk. alternatives are using autoflushing ($| = 1) or using the FileHandle->autoflush() method.