Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Inter-Process Communication

by Nitrox (Chaplain)
on Jul 20, 2002 at 23:45 UTC ( #183703=perlquestion: print w/replies, xml ) Need Help??

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

This turned into a long discussion in the Chatterbox so SoPW seemed like the next logical step in my pursuit for enlightenment.

I've written a Perl program that sits in a while() loop doing a few tasks, one of which is polling the serial port for data. When data is received the program does some control calls to a win32 application via OLE.

Now I'm looking for a way to pass data(a simple string) back to my Perl program. Any ideas?

One of the issues is that the solution needs to be "non-blocking" since my program needs to continue polling the serial port and lives in a single threaded world.

The win32 app has the ability to run PerlScript via the ActiveX engine, so one solution I thought might work would be to spawn a thread in my Perl app that opened a tcp socket and listened. The win32 app would then run a PerlScript that connected to the socket and passed the string. In turn, the child would then pass the string to the parent. Unfortunately I do not see a way to get the child thread to communicate with the parent.

So, what is the best solution to pass data to a running Perl process in the Win32 environment?


Replies are listed 'Best First'.
Re: Inter-Process Communication
by dpuu (Chaplain) on Jul 21, 2002 at 02:29 UTC
    I'm not sure about the specifics of your Win32 installation. Under cygwin, fork and pipe are implemented, so the following code works:
    pipe B,A; my $child = fork; die "fork: $!" unless defined $child; if ($child != 0) { use IO::Handle; close B; print A "hello\n"; A->flush; sleep 1; print A "world\n"; exit; } close A; while (<B>) { print; }
    You can do use select to poll the filehandle. Of course, you could simply use your socket instead of opening the pipe. --Dave.
      I'm running ActiveState's Perl 5.6.1 build633 and your example runs without errors, but with odd results.

      It produces no output and hangs indefinetly, but when I ctrl-C the console it then outputs 'hello'. Seems to be a deadlock situation?

      Also, instead of fork'ing an entire copy, is there a way to spawn a thread that is capable of accessing variables in the parent thread?


Re: Inter-Process Communication
by perigeeV (Hermit) on Jul 21, 2002 at 02:45 UTC

    Win32::Pipe will let you establish a bidirectional named pipe. I guess the question becomes: can you establish a named pipe in server context with your win32 app? If so, then your win32 app sets up a pipe as a server, and your Perl script calls

    if( $Pipe = new Win32::Pipe( $PipeName ) ) { # talk... } else { # continue polling. The client's call failed. }

    That way your win32 app is blocking, but your Perl app isn't.

      Unfortunately the win32 app is a Home Automation engine that currently doesn't execute its scripts in a separate thread, so I can't put it in a blocked state either.


        OK, some other ideas:

        Set up some shared memory. Most of the CPAN stuff is SYSV based, but perhaps use Cache::Cache.

        Or perhaps have your Win32 app write the data to a normal file, and then send a signal to the perl process. Put a signal handler in the script so that it knows when it is time to read the file.

Re: Inter-Process Communication
by ehdonhon (Curate) on Jul 21, 2002 at 02:12 UTC

    It depends on the purpose of your application, I suppose.

    One potential solution would be for your Win32 app to append the message to a text file that your Perl process has open for reading. Then your process could do a non-blocking select periodically to poll the file.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183703]
Approved by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2023-09-21 22:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found