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

I need a perl script to sleep until another perl script sends it a signal or message.

What is the easiest technique to use? A real simple example would help. This is for activestate on win2K. Thanks!

  • Comment on how to implement interprocess communication

Replies are listed 'Best First'.
Re: how to implement interprocess communication
by BrowserUk (Patriarch) on Dec 07, 2004 at 20:59 UTC

    Win32 doesn't do signals (much), but has several more powerful alternatives. Take a look at Win32::IPC, Win32::Semaphore, Win32::Event, Win32::Mutex etc.

    Here's one way of achieving your requirement.

    #! perl -slw use strict; use Win32::Event; my $eventName = "MyEvent.$$"; print $eventName; my $event = new Win32::Event( 0, 0, $eventName ); my $timeout = rand 60000; print "Spawning a background script to wake me up in ", $timeout / 100 +0, " seconds"; ( my $cmd = <<EOP ) =~ s[\s+][ ]g; perl -MWin32::Event -wle" Win32::Sleep shift; print 'Waking them'; \$e=Win32::Event->open(shift) and \$e->set or die \$^E " $timeout $eventName EOP system 1, $cmd; $timeout /= 1000; my $rv = 0; print "Event should occur in ", int( --$timeout ) until $rv = $event- +>wait( 1000 ); print $rv ? 'Woken' : 'Event abandoned'; __END__ [20:54:53.64] P:\test>412941 MyEvent.648 Spawning a background script to wake me up in 17.080078125 seconds Event should occur in 16 Event should occur in 15 Event should occur in 14 Event should occur in 13 Event should occur in 12 Event should occur in 11 Event should occur in 10 Event should occur in 9 Event should occur in 8 Event should occur in 7 Event should occur in 6 Event should occur in 5 Event should occur in 4 Event should occur in 3 Event should occur in 2 Event should occur in 1 Event should occur in 0 Waking them Woken

    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      That's a great solution - thanks!

      So do I understand correctly that there is no cross platform method of accomplishing this, portable to Linux?

        So do I understand correctly that there is no cross platform method of accomplishing this, portable to Linux?

        I don't know enough about Linux to comment authoratively. I have read that traditional signals do not mix well with Perl.

        In their raw form, as used prior to 5.8.0, they could interupt individual Perl opcodes part way through execution, and so leave Perl's fat datastructures in an incomplete or unstable state. Mostly okay for exceptional purposes where the code was to die anyway, but not so good for pure IPC purposes, where the code interupted needed to continue.

        In their 'safe-signals' form, this problem is avoided by ensuring that the signal does not interupt the user's (perl) code until the interpreter is 'between' opcodes. This makes continuaton more reliable, but renders the immediacy of signals somewhat dented. Unlike a C program, where a single cpu-level opcode can rarely take more than a few milliseconds, a single Perl opcode can be doing a great deal (sorting a huge datastructure for example).

        There are the IPC::SysV & IPC::Semaphore stuff, but from what I read these are not even that dependable on systems that claim SysV compatibility, and probably do nothing at all on many non-*nix based patforms.

        Take all that with a pinch of salt as I'm really relating what I have read, not talking from experience.


        Examine what is said, not who speaks.
        "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
        "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: how to implement interprocess communication
by gellyfish (Monsignor) on Dec 07, 2004 at 16:54 UTC

    The very easiest way to do this would be with signals (depending on your OS) I would start with the perlipc manpage.

    /J\