Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Singleton process

Reply
Thread Tools

Singleton process

 
 
Roy Johnson
Guest
Posts: n/a
 
      10-28-2003
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?
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      10-28-2003
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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-28-2003

http://www.velocityreviews.com/forums/(E-Mail Removed)-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] * http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      10-28-2003
Also sprach Ben Morrow:

> (E-Mail Removed)-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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      10-28-2003
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.
 
Reply With Quote
 
Mark Jason Dominus
Guest
Posts: n/a
 
      10-29-2003
In article <bnm7vm$eel$(E-Mail Removed)>,
Ben Morrow <(E-Mail Removed)> wrote:
>
>(E-Mail Removed)-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/



 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-29-2003

(E-Mail Removed) (Mark Jason Dominus) wrote:
> In article <bnm7vm$eel$(E-Mail Removed)>,
> Ben Morrow <(E-Mail Removed)> wrote:
> >
> >(E-Mail Removed)-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.
(E-Mail Removed)
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      10-29-2003
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.
 
Reply With Quote
 
Roy Johnson
Guest
Posts: n/a
 
      10-29-2003
(E-Mail Removed) (Mark Jason Dominus) wrote in message news:<bnopno$7e7$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-29-2003

(E-Mail Removed) (Roy Johnson) wrote:
> (E-Mail Removed) (Mark Jason Dominus) wrote in message
> news:<bnopno$7e7$(E-Mail Removed)>...
> > 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. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments