Singleton process

Discussion in 'Perl Misc' started by Roy Johnson, Oct 28, 2003.

  1. Roy Johnson

    Roy Johnson Guest

    I've got a program that I want to be the only instance of itself
    running at any given time. The method I've chosen for doing that is
    probably not the preferred method, and it certainly won't work under
    Windows.

    Here's what I'm doing:
    ### Ensure that this is the only instance running
    my @myprocs = grep(/perl $0/, `ps -f`);
    for (@myprocs) {
    my ($pid) = (split)[1];
    next if ($pid == $$);
    die "Found $_\n";
    }

    What *should* I be doing, for maximum reliability on both Unix and
    Windows?
    Roy Johnson, Oct 28, 2003
    #1
    1. Advertising

  2. Also sprach Roy Johnson:

    > I've got a program that I want to be the only instance of itself
    > running at any given time. The method I've chosen for doing that is
    > probably not the preferred method, and it certainly won't work under
    > Windows.
    >
    > Here's what I'm doing:
    > ### Ensure that this is the only instance running
    > my @myprocs = grep(/perl $0/, `ps -f`);
    > for (@myprocs) {
    > my ($pid) = (split)[1];
    > next if ($pid == $$);
    > die "Found $_\n";
    > }
    >
    > What *should* I be doing, for maximum reliability on both Unix and
    > Windows?


    Locking, I guess. Let the program create a lockfile at the beginning:

    use Fcntl;

    sysopen LOCK, "/tmp/prog.lock", O_CREAT|O_EXCL or
    exit 1;

    ...

    END { unlink "/tmp/prog.lock" }

    This should be portable between UNIX and Windows.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Oct 28, 2003
    #2
    1. Advertising

  3. Roy Johnson

    Ben Morrow Guest

    -aachen.de wrote:
    > Also sprach Roy Johnson:
    >
    > > I've got a program that I want to be the only instance of itself
    > > running at any given time. What *should* I be doing, for maximum
    > > reliability on both Unix and Windows?

    >
    > Locking, I guess. Let the program create a lockfile at the beginning:
    >
    > use Fcntl;
    >
    > sysopen LOCK, "/tmp/prog.lock", O_CREAT|O_EXCL or
    > exit 1;
    >
    > ...
    >
    > END { unlink "/tmp/prog.lock" }
    >
    > This should be portable between UNIX and Windows.


    What if the process is killed and leaves a stale lockfile around?
    You want to flock() the file as well; this will be broken when the
    process dies, at least on Unix. Not sure about Windows: I've had nasty
    experiences with programs dieing and leaving locks locked... :(

    Ben

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
    Ben Morrow, Oct 28, 2003
    #3
  4. Also sprach Ben Morrow:

    > -aachen.de wrote:
    >> Also sprach Roy Johnson:
    >>
    >> > I've got a program that I want to be the only instance of itself
    >> > running at any given time. What *should* I be doing, for maximum
    >> > reliability on both Unix and Windows?

    >>
    >> Locking, I guess. Let the program create a lockfile at the beginning:
    >>
    >> use Fcntl;
    >>
    >> sysopen LOCK, "/tmp/prog.lock", O_CREAT|O_EXCL or
    >> exit 1;
    >>
    >> ...
    >>
    >> END { unlink "/tmp/prog.lock" }
    >>
    >> This should be portable between UNIX and Windows.

    >
    > What if the process is killed and leaves a stale lockfile around?
    > You want to flock() the file as well; this will be broken when the
    > process dies, at least on Unix. Not sure about Windows: I've had nasty
    > experiences with programs dieing and leaving locks locked... :(


    Define a signal-handler for a couple of signals (this however is not
    quite so portable any longer). Anyway, stale lockfiles are nothing
    special and show up once in a while.

    Another approach would be to let the processes write their PIDs into the
    file. That way, a new process could see whether a) any PIDs are to be
    found in it and b) whether processes belonging to this PID are still
    alive (by sending signal 0).

    Other than that I don't expect Perl scripts to continuously crash. At
    least mine don't.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Oct 28, 2003
    #4
  5. Roy Johnson

    Bart Lateur Guest

    Ben Morrow wrote:

    >What if the process is killed and leaves a stale lockfile around?
    >You want to flock() the file as well; this will be broken when the
    >process dies, at least on Unix. Not sure about Windows:


    Just flocking a file, that would be my idea. Whether it works on
    Windows, depends on the version of Windows. Win98 and friends don't do
    flock(). NT can, I think.

    --
    Bart.
    Bart Lateur, Oct 28, 2003
    #5
  6. In article <bnm7vm$eel$>,
    Ben Morrow <> wrote:
    >
    >-aachen.de wrote:
    >> Also sprach Roy Johnson:
    >>
    >> > I've got a program that I want to be the only instance of itself
    >> > running at any given time. What *should* I be doing, for maximum
    >> > reliability on both Unix and Windows?

    >>
    >> Locking, I guess. Let the program create a lockfile at the beginning:
    >>
    >> use Fcntl;
    >>
    >> sysopen LOCK, "/tmp/prog.lock", O_CREAT|O_EXCL or
    >> exit 1;
    >>
    >> ...
    >>
    >> END { unlink "/tmp/prog.lock" }
    >>
    >> This should be portable between UNIX and Windows.

    >
    >What if the process is killed and leaves a stale lockfile around?


    Can I suggest the following delightful and foolproof solution to this
    problem?

    http://perl.plover.com/yak/flock/samples/slide006.html

    The full talk on this topic is available from

    http://perl.plover.com/yak/flock/
    Mark Jason Dominus, Oct 29, 2003
    #6
  7. Roy Johnson

    Ben Morrow Guest

    (Mark Jason Dominus) wrote:
    > In article <bnm7vm$eel$>,
    > Ben Morrow <> wrote:
    > >
    > >-aachen.de wrote:
    > >> Also sprach Roy Johnson:
    > >>
    > >> > I've got a program that I want to be the only instance of itself
    > >> > running at any given time. What *should* I be doing, for maximum
    > >> > reliability on both Unix and Windows?
    > >>

    > >What if the process is killed and leaves a stale lockfile around?

    >
    > Can I suggest the following delightful and foolproof solution to this
    > problem?
    >
    > http://perl.plover.com/yak/flock/samples/slide006.html


    Nice, as usual :).

    The problem of Win32 failing to break locks when a process is killed
    (and I believe sometimes even failing to break them across reboots) is
    of course not a Perl problem.

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    - Pericles of Athens, c.430 B.C.
    Ben Morrow, Oct 29, 2003
    #7
  8. Roy Johnson

    Bart Lateur Guest

    Mark Jason Dominus wrote:

    >Can I suggest the following delightful and foolproof solution to this
    >problem?
    >
    > http://perl.plover.com/yak/flock/samples/slide006.html


    Hmm... I can think of systems that refuse to flock files exclusively
    when they're not opened for writing. Neither of the above examples
    complies to that condition.

    See paragraph 6 in

    <http://www.perldoc.com/perl5.8.0/pod/func/flock.html>

    Note that the fcntl(2) emulation of flock(3) requires that
    FILEHANDLE be open with read intent to use LOCK_SH and requires
    that it be open with write intent to use LOCK_EX.

    --
    Bart.
    Bart Lateur, Oct 29, 2003
    #8
  9. Roy Johnson

    Roy Johnson Guest

    (Mark Jason Dominus) wrote in message news:<bnopno$7e7$>...
    > Can I suggest the following delightful and foolproof solution to this
    > problem?
    >
    > http://perl.plover.com/yak/flock/samples/slide006.html


    For some reason, both suggestions (using $0 and using DATA) fail on my
    Unix box. I am using flock, though, and when I create a throwaway file
    for the purpose, it works properly.
    Roy Johnson, Oct 29, 2003
    #9
  10. Roy Johnson

    Ben Morrow Guest

    (Roy Johnson) wrote:
    > (Mark Jason Dominus) wrote in message
    > news:<bnopno$7e7$>...
    > > Can I suggest the following delightful and foolproof solution to this
    > > problem?
    > >
    > > http://perl.plover.com/yak/flock/samples/slide006.html

    >
    > For some reason, both suggestions (using $0 and using DATA) fail on my
    > Unix box. I am using flock, though, and when I create a throwaway file
    > for the purpose, it works properly.


    Out of interest: which Unix are you on, what error do you get, and
    does your perl use flock() or fcntl() locking (if you can tell)?

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off the
    shoulder of Orion; I've watched C-beams glitter in the darkness near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
    Ben Morrow, Oct 29, 2003
    #10
  11. Roy Johnson

    Roy Johnson Guest

    Ben Morrow <> wrote in message news:<bnphpr$sbi$>...
    > (Roy Johnson) wrote:
    > > For some reason, both suggestions (using $0 and using DATA) fail on my
    > > Unix box. I am using flock, though, and when I create a throwaway file
    > > for the purpose, it works properly.

    >
    > Out of interest: which Unix are you on, what error do you get, and
    > does your perl use flock() or fcntl() locking (if you can tell)?


    I'm on Solaris (5.8).
    The error message I get is
    Bad file number

    I don't know how to check which form of locking is being done. Here's
    a sample program:

    #!perl

    use strict;
    use warnings;

    use Fcntl ':flock';
    open SELF, $0 or die "Could not open $0: $!\n";
    flock SELF, LOCK_EX | LOCK_NB
    or die "Could not flock $0: $!\n";
    close SELF;
    print "Done\n";
    Roy Johnson, Oct 30, 2003
    #11
  12. Roy Johnson

    Guest

    (Roy Johnson) wrote:
    > Ben Morrow <> wrote in message
    > news:<bnphpr$sbi$>...
    > > (Roy Johnson) wrote:
    > > > For some reason, both suggestions (using $0 and using DATA) fail on
    > > > my Unix box. I am using flock, though, and when I create a throwaway
    > > > file for the purpose, it works properly.

    > >
    > > Out of interest: which Unix are you on, what error do you get, and
    > > does your perl use flock() or fcntl() locking (if you can tell)?

    >
    > I'm on Solaris (5.8).
    > The error message I get is
    > Bad file number
    >
    > I don't know how to check which form of locking is being done. Here's
    > a sample program:
    >
    > #!perl
    >
    > use strict;
    > use warnings;
    >
    > use Fcntl ':flock';
    > open SELF, $0 or die "Could not open $0: $!\n";
    > flock SELF, LOCK_EX | LOCK_NB
    > or die "Could not flock $0: $!\n";
    > close SELF;
    > print "Done\n";


    I get the same thing on my version of perl on that OS. changing it to open
    SELF, "+<$0" worked.

    Apparently this is an instance of the "can't lock exclusively if you don't
    intend to write" issue.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    , Oct 30, 2003
    #12
  13. Roy Johnson

    JR Guest

    (Roy Johnson) wrote in message news:<>...
    > Ben Morrow <> wrote in message news:<bnphpr$sbi$>...
    > > (Roy Johnson) wrote:
    > > > For some reason, both suggestions (using $0 and using DATA) fail on my
    > > > Unix box. I am using flock, though, and when I create a throwaway file
    > > > for the purpose, it works properly.

    > >
    > > Out of interest: which Unix are you on, what error do you get, and
    > > does your perl use flock() or fcntl() locking (if you can tell)?

    >
    > I'm on Solaris (5.8).
    > The error message I get is
    > Bad file number
    >
    > I don't know how to check which form of locking is being done. Here's
    > a sample program:
    >
    > #!perl
    >
    > use strict;
    > use warnings;
    >
    > use Fcntl ':flock';
    > open SELF, $0 or die "Could not open $0: $!\n";
    > flock SELF, LOCK_EX | LOCK_NB
    > or die "Could not flock $0: $!\n";
    > close SELF;
    > print "Done\n";


    This is one way to check which form of locking your system supports.
    This may be useful to you.

    #!/perl/ -w
    use strict;
    use IO::File;
    use Fcntl ":flock";

    *FH1 = new_tmpfile IO::File or die "Can't open: $!\n";

    eval {flock FH1, LOCK_SH};
    die "No locks supported.\n" if $@;

    open FH2, ">>&FH1" or die "Can't open: $!\n";

    (flock FH2, LOCK_SH|LOCK_NB) ? print "Shared locks supported.\n" :
    print "Shared locks not supported.\n";

    (flock FH2, LOCK_EX|LOCK_NB) ? print "Exclusive locks supported.\n" :
    print "Exclusive locks not supported.\n";
    JR, Oct 30, 2003
    #13
  14. Roy Johnson

    Ben Morrow Guest

    (JR) wrote:
    > This is one way to check which form of locking your system supports.
    > This may be useful to you.


    <snip test for shared/exclusive lock support>

    That wasn't the question. There are two ways of locking files under
    Unix, using the flock(2) or fcntl(2) syscalls. Perl can (generally
    speaking) be built to use either. I was asking which.

    AFAIK, all forms of locking support both shared and exclusive locks:
    seems pretty useless to me if they don't...

    Ben

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
    Ben Morrow, Oct 30, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,234
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    162
  3. Trans
    Replies:
    12
    Views:
    273
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    203
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    199
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page