Eradicatore has asked for the wisdom of the Perl Monks concerning the following question:
My goal: To have a forked child just sit there waiting for commands to come in and when one comes in, do it. The commands will just be web page urls to go get and dump in a file. Ideally then the child could send on a reverse pipe a command to the parent saying the page is done.
I went to the IPC help page on perldoc.com and here was my (sad?) attempt at getting a child to wait. Just to point out, I am getting parent and child to both work in the standard "bidirectional communication" example from perldoc.com but here I'm trying to add to that to make the child wait for a readable file handle. Any help would really be appreciated.
#!/usr/bin/perl -w # pipe1 - bidirectional communication using two pipe pairs # designed for the socketpair-challenged use IO::Handle; # thousands of lines just for autoflush :-( use IO::Select; # thousands of lines just for autoflush :-( pipe(PARENT_RDR, CHILD_WTR); # XXX: failure? pipe(CHILD_RDR, PARENT_WTR); # XXX: failure? CHILD_WTR->autoflush(1); PARENT_WTR->autoflush(1); $sel = new IO::Select( PARENT_RDR ); if ($pid = fork) { close PARENT_RDR; close PARENT_WTR; print CHILD_WTR "Parent Pid $$ is sending this\n"; chomp($line = <CHILD_RDR>); print "Parent Pid $$ just read this: `$line'\n"; close CHILD_RDR; close CHILD_WTR; waitpid($pid,0); } else { die "cannot fork: $!" unless defined $pid; close CHILD_RDR; close CHILD_WTR; print PARENT_WTR "Before: Child Pid $$ is sending this\n"; while(@ready = $sel->can_read) { foreach $fh (@ready) { chomp($line = <$fh>); print "Child Pid $$ just read this: `$line'\n"; } } print PARENT_WTR "After: Child Pid $$ is sending this\n"; close PARENT_RDR; close PARENT_WTR; exit; }
Justin Eltoft
"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: IPC, trying for have child wait for commands
by tye (Sage) on Jun 05, 2001 at 22:29 UTC | |
by Eradicatore (Monk) on Jun 05, 2001 at 22:38 UTC | |
by tye (Sage) on Jun 05, 2001 at 22:48 UTC | |
by Eradicatore (Monk) on Jun 05, 2001 at 22:56 UTC | |
by John M. Dlugosz (Monsignor) on Jun 05, 2001 at 23:22 UTC | |
Re: IPC, trying for have child wait for commands
by Eradicatore (Monk) on Jun 06, 2001 at 21:32 UTC |