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

Hi,
I was trying to write a perl script that creates a number of child processes, and try to communicate b/w parent and child processes using pipes instead of socketpair. I wrote a script to communicate b/w processes bidirectionally using pipes ,but it doesn't work as intended, Would someone take a look at the following script and tell me what I'm doing wrong here.
Since I need to run the same script in windows,I think I can't use socketpair like below for bidirectional communication rt?

socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!";

or Is there any perl module that has effective methods of message passing b/w processes.
Any help would be greatly appreciated..
Thanks,
Shijumic..

#!/usr/bin/perl -w # pipe1 - bidirectional communication using two pipe pairs # designed for the socketpair-challenged use IO::Handle; pipe(PARENT_RDR, CHILD_WTR); pipe(CHILD_RDR, PARENT_WTR); CHILD_WTR->autoflush(1); PARENT_WTR->autoflush(1); if ($pid = fork) { close PARENT_RDR; close PARENT_WTR; send_message(CHILD_WTR,HELLO,1234); my ($code1,$msg1) = receive_message(CHILD_RDR); print "Parent Received:" . $code1 . "\t" . $msg1 . "\n"; close CHILD_RDR; close CHILD_WTR; waitpid($pid,0); } else { die "cannot fork: $!" unless defined $pid; close CHILD_RDR; close CHILD_WTR; my ($code,$msg) = receive_message(PARENT_RDR); print "Child Received:" . $code . "\t" . $msg . "\n"; send_message(PARENT_WTR,Hi,1234); close PARENT_RDR; close PARENT_WTR; exit; } sub send_message { my $handle = shift; my $code = shift; my $msg = shift; $message = join(':' , $code,$msg); print $handle $message; } sub receive_message { my $handle = shift; my $msg; chomp($msg = <$handle>); ($code,$msg) = split (/:/,$msg); return ($code,$msg); }

Replies are listed 'Best First'.
Re: Message Passing b/w processes , that works both in windows and Linux
by ikegami (Patriarch) on Jun 08, 2009 at 22:02 UTC

    For starters, fork creates a thread (not a process) in Windows. Is that a problem?

    I wrote a script to communicate b/w processes bidirectionally using pipes ,but it doesn't work as intended

    The script you posted runs on neither linux nor Windows until you fix the issue with reading until a newline is encountered when none is sent. Then, it works fine on both linux and Windows.

    Since I need to run the same script in windows,I think I can't use socketpair like below for bidirectional communication rt?

    I'm pretty sure you can, but you might have to change from AF_UNIX to AF_INET.

      Thanks much for your rely. Would you please explain me,how to fix the issue with reading until a newline is encountered when none is sent in the above script.
      Thanks
      -Shijumic

        Since the readers are waiting for a newline, I sent one by changing
        print $handle $message;
        to
        print $handle "$message\n";

Re: Message Passing b/w processes , that works both in windows and Linux
by repellent (Priest) on Jun 09, 2009 at 18:14 UTC
    I have been trying to do that with IPC::Exe, so please give it a try and let me know what you think.

    The module has quite a thorough test suite thrown at it. It's been a long effort but I know I can still improve on it.