Article 11963 of comp.lang.perl:
Path: feenix.metronet.com!news.utdallas.edu!convex!cs.utexas.edu!uunet!ncrgw2.ncr.com!ncrhub2!ncr-mpd!Tony.Parent
From:
[email protected] (tony parent)
Newsgroups: comp.lang.perl
Subject: Re: question about a loop of piped processes
Message-ID: <
[email protected]>
Date: 25 Mar 94 00:37:46 GMT
References: <
[email protected]>
Sender:
[email protected]
Reply-To:
[email protected]
Organization: NCR Microelectronics, Ft. Collins, CO
Lines: 94
In-reply-to:
[email protected]'s message of 23 Mar 94 22:24:14 GMT
In article <
[email protected]>
[email protected] (Chi-Sharn Wu) writes:
> Hi:
> I tried a simple example of bidirectional pipes using perl, however it
> doesn't work.
> The example forks a process "sort" which feeds result to its parent.
> Can anyone fix it ? Thanks.
>
> Chi-Sharn Wu
>
[email protected]
>
>
> =========== the example ==============
>
> #pipe(READHANDLE, WRITEHANDLE)
> pipe(IN, DATA);
> pipe(RESULT, OUT);
> if ($pid=fork) {
> close(IN);
> close(OUT);
> select(DATA); $| = 1;
> open(RESULT);
> }
> else {
> close(DATA); close(RESULT);
> select(OUT); $| = 1;
> open(STDIN, ">&IN");
> # you see the result if the following command is commented out
> open(STDOUT, ">&OUT");
> exec "sort ";
> }
>
> print DATA "bbbbbb \ntwo \nan example\n";
> while (<RESULT>) {
> print $_, "!!!!!\n";
> }
You were close, basically you had what should have been in the parent
process in the child process, and visa-versa. the following works in perl
Revision: 4.0.1.8, Patch level: 36.
pipe(IN, DATA);
pipe(RESULT, OUT);
if ($pid=fork) {
;# child process
close(DATA);
close(RESULT);
open(STDIN, "<&IN");
open(STDOUT, ">&OUT");
select(STDOUT); $| = 1;
exec "sort";
;# If here, then disaster has occured
die "exec: $!";
exit $!;
} else {
;# parent process
;# Close unused ends of communication pipes
close(IN);
close(OUT);
;# Attach files to communication pipes for easy I/O
open(FIN,"<&RESULT");
open(FOUT,">&DATA");
;# make unbuffered
select(FOUT);
$| = 1;
select STDOUT;
}
print FOUT "bbbbbb \ntwo \nan example\n";
;# close outputs (sort wont print it's output till it sees an EOF)
close(FOUT);
close(DATA);
while (<FIN>) {
print $_, "!!!!!\n";
}
exit 0;
===========================================================================
__o tonyp@merlin NCR MPD __o
-\<,
[email protected] 2001 Danfield Ct -\<,
O / O (303) 223-5100 x9769 Fort Collins, CO 80525 O / O
===========================================================================
--
===========================================================================
__o tonyp@merlin NCR MPD __o
-\<,
[email protected] 2001 Danfield Ct -\<,
O / O (303) 223-5100 x9769 Fort Collins, CO 80525 O / O
===========================================================================