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: $!";
#!/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 | |
by shijumic (Novice) on Jun 08, 2009 at 22:45 UTC | |
by tye (Sage) on Jun 09, 2009 at 00:25 UTC | |
by ikegami (Patriarch) on Jun 09, 2009 at 01:24 UTC | |
by tye (Sage) on Jun 09, 2009 at 01:58 UTC | |
by ikegami (Patriarch) on Jun 09, 2009 at 00:10 UTC | |
|
Re: Message Passing b/w processes , that works both in windows and Linux
by repellent (Priest) on Jun 09, 2009 at 18:14 UTC |