Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

SOLVED [flock on Ubuntu 9.10]

by poctob (Initiate)
on Nov 13, 2009 at 21:50 UTC ( #807066=perlquestion: print w/replies, xml ) Need Help??

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

I have a simple program strait out of the text book:
#!/usr/bin/env perl use strict; use warnings; open(FH, "+< rotator_lock") or die "can't open: $!"; flock(FH, 2) or die "can't flock: $!"; # update file, then... sleep(120); close(FH) or die "can't close: $!";
However I am able to start multiple instances of this application without any errors or limitations. I was under impression that flock would prevent this app from running multiple instances. Can someone advise what I am doing wrong here? Thanks in advance

Replies are listed 'Best First'.
Re: flock on Ubuntu 9.10
by ikegami (Pope) on Nov 13, 2009 at 22:11 UTC
    flock blocks until a lock can be obtained (unless you tell it otherwise).
    use strict; use warnings; use Fcntl qw( :flock ); sleep(1); open(my $fh, '+<', $0) or die "open: $!\n"; print localtime().": $$'s flock attempt starts\n"; flock($fh, LOCK_EX) or die "flock: $!\n"; print localtime().": $$'s flock obtained\n"; sleep(5); print localtime().": $$ ends\n";
    $ for q in 1 2 3 4 ; do perl 807067.pl & done Fri Nov 13 17:21:04 2009: 20079's flock attempt starts Fri Nov 13 17:21:04 2009: 20079's flock obtained Fri Nov 13 17:21:04 2009: 20080's flock attempt starts Fri Nov 13 17:21:04 2009: 20082's flock attempt starts Fri Nov 13 17:21:04 2009: 20081's flock attempt starts Fri Nov 13 17:21:09 2009: 20079 ends Fri Nov 13 17:21:09 2009: 20081's flock obtained Fri Nov 13 17:21:14 2009: 20081 ends Fri Nov 13 17:21:14 2009: 20082's flock obtained Fri Nov 13 17:21:19 2009: 20082 ends Fri Nov 13 17:21:19 2009: 20080's flock obtained Fri Nov 13 17:21:24 2009: 20080 ends

    Now try using LOCK_EX | LOCK_NB...

    Fri Nov 13 17:26:42 2009: 20116's flock attempt starts Fri Nov 13 17:26:42 2009: 20116's flock obtained Fri Nov 13 17:26:42 2009: 20117's flock attempt starts flock: Resource temporarily unavailable Fri Nov 13 17:26:42 2009: 20118's flock attempt starts flock: Resource temporarily unavailable Fri Nov 13 17:26:42 2009: 20119's flock attempt starts flock: Resource temporarily unavailable

    You can detect a denied lock using

    use Errno qw( EWOULDBLOCK ); if (flock(..., ... | LOCK_NB)) { lock obtained } elsif ($! == EWOULDBLOCK) { lock denied } else { some error }

    Update: Additions.

Re: flock on Ubuntu 9.10
by gmargo (Hermit) on Nov 13, 2009 at 22:14 UTC

    You are using flock(FH, LOCK_EX) which will wait until the lock is available. Easy to see if you put a print before and after the flock.

    Add the non-blocking flag to get an instantaneous read on the lock state: flock(FH, LOCK_EX|LOCK_NB). Import the constants with use Fcntl qw(:flock);.

      What can I say you and ikegami hit the nail right on the head. Adding non-blocking flag did the trick. The only thing I would like to add, so not really related to my original post, is that if Proc::Daemon::Init is used to daemonize the process, flock call has to be after Proc::Daemon::Init call, otherwise Proc::Daemon::Init releases the lock as it forks the app. Thank you!
        Part of daemonizing involves forking and exiting the current process. Proc::Deamon uses exit to exit the current process. This closes open file handles, releasing associated locks.
Re: flock on Ubuntu 9.10
by trwww (Priest) on Nov 13, 2009 at 23:35 UTC

    I was under impression that flock would prevent this app from running multiple instances.

    Apologies for the terseness, but I can't imagine for the life of me how the code you've posted could correspond to your impression in any way. How would locking a file prevent the current process from running?

    Have a look at File::Pid::Quick.

    regards,

      by dieing. That's what happens if he were to use LOCK_EX | LOCK_NB

        Indeed. I should have just kept the commentary to myself. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807066]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2019-11-22 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (107 votes). Check out past polls.

    Notices?