Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my $lock = "/var/www/html/shhabcam/LOGS/lock_file.txt"; sysopen LOCK, $lock, O_CREAT or die "Can't sysopen $lock: $!" +; flock LOCK, LOCK_EX or die "Can't flock $lock: $!";

this blocks successfully with two users...does it have any issues with 6 or more users?

Replies are listed 'Best First'.
Re: 'flock' with multiple users
by haukex (Archbishop) on Sep 08, 2017 at 15:24 UTC
    this blocks successfully with two users...does it have any issues with 6 or more users?

    If it blocks for two users, it should block just fine for more, but exactly that may be the problem - depending on how frequently you need access, your processes might suffer from starvation. If this is a concern, you may want to consider different strategies. I see you are working in a directory called "logs" - what do you need the lock file for?

      I am acquiring successive records from an Oracle table; immediately after a good retrieval I am marking the record as 'processed' but I find that the same record can be acquired by several users. Not using the lockfile except as a locking mechanism.

        If you already have a database, why bother with locks?

        Oracle offers various methods for making sure that processes don't step on each others feet. Usually, you want to do stuff in a transaction.

        For example an easy way could be to UPDATE all rows that you will be processing with your PID, and then select all those that you got:

        update top 10 jobs set processed_by = ? where processed_by is null order by job_id ; select * from jobs where processed_by = ?

        What I meant is the frequency, i.e. how many locks per second your processes will be attempting. However, if you're already using a database, you should definitely be using its locking facilities instead of flocking a file!

Re: 'flock' with multiple users
by afoken (Chancellor) on Sep 08, 2017 at 18:28 UTC

    If all users are on the same machine, there should not be any problems.

    If you try to place a lock file on an NFS share, good luck. Locking on NFS has a long and ugly history, so better don't hope for working locks on NFS. (See also http://man7.org/linux/man-pages/man2/flock.2.html#NOTES) For other networked filesystems, RTFM. But don't expect miracles. Getting locks right transparently over a network is not trivial.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
A reply falls below the community's threshold of quality. You may see it by logging in.