home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!convex!convex!tchrist
- From: tchrist@convex.COM (Tom Christiansen)
- Newsgroups: comp.lang.perl
- Subject: Re: Pipe, et al, what am I doing wrong?
- Keywords: pipe, fork
- Message-ID: <1992Jul27.145923.5083@news.eng.convex.com>
- Date: 27 Jul 92 14:59:23 GMT
- References: <1992Jul27.115715.21337@neptune.inf.ethz.ch>
- Sender: usenet@news.eng.convex.com (news access account)
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- Organization: CONVEX Realtime Development, Colorado Springs, CO
- Lines: 55
- Originator: tchrist@pixel.convex.com
- Nntp-Posting-Host: pixel.convex.com
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
-
- From the keyboard of weingart@inf.ethz.ch:
- :Here is a small script, that seems to hang. What am I doing
- :wrong? A ps shows that the child is waiting for the parent
- :to `wait` on it. Also, the parent seems to hang because it
- :does not get EOF.
- :
- :
- :#!/usr/local/bin/perl
- :#
- :# To test fork, pipe
- :# and other things.
- :
- :select(STDIN); $| = 1;
- :select(STDERR); $| = 1;
- :select(STDOUT); $| = 1;
- :
- :pipe( READH, WRITEH );
- :$pid = fork();
- :die "Could not fork.\n" if( ! defined($pid) );
- :
- :@data = ();
- :if( $pid == 0 ){
- : open(STDOUT, ">&WRITEH");
- : open(STDERR, ">&WRITEH");
- : exec( '/bin/cat', '/etc/passwd' );
- :}else{
- : open(STDIN, "<&READH");
- : while( <STDIN> ){
- : push(@data, $_);
- : print ".";
- : }
- : print "\nLines: $#data\n";
- :}
- :
- :exit(0);
- :
- :#
- :# End of script.
- :
- :
- :Can anyone give me a working example of this?
-
- Sure -- just close(READH) in the kid before the exec,
- and close(WRITEH) in the parent before you dup STDIN.
-
- But you're going to way to much trouble. Using the
- implicit fork of open on "|-" and "-|" is much easier,
- and almost always suffices.
-
- --tom
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
-
-
- Real Programmers think better when playing Adventure or Rogue.
-