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); }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |