I need to implement a locking scheme so that multiple processes can share a set of resources, while a special process can obtain exclusive access to that set of resources. These are batch processes: at the beginning of each transaction I propose to acquire the appropriate lock, and release it at the end, ad infinitum.flock has the semantics I need (LOCK_SH, LOCK_EX, LOCK_UN). I experimented with it using Perl Flock.pm and a dummy file whose only purpose is to be flock'ed against. I was a little surprised at how slow it was, and it wasn't evident from 'top' where the time was being spent. (It wasn't CPU-bound, even though the loop being executed consisted of nothing but LOCK_SH and LOCK_UN.) I don't want to be guilty of premature optimization, but I wanted to know whether flock is the standard method for managing shared and exclusive access to a shared resource in *nix, even when the shared resource is not an actual file, or whether there was another facility I'm unaware of.UPDATE: @msw correctly guessed that I'd (inadvertently) locked on an NFS file instead of a local one. Using a local file completely cleared up the performance hit I was seeing. I'm leaving the question open to learn more about whether file locking is really the best way to go for this class of problem.
Generic shared resource locking techniques - is flock the solution?
linux;lock