home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!florida!essbaum
- From: essbaum@rchland.vnet.ibm.com (Alexander Essbaum)
- Subject: Re: fork and STDOUT
- Sender: news@rchland.ibm.com
- Message-ID: <1992Dec31.185804.9837@rchland.ibm.com>
- Date: Thu, 31 Dec 1992 18:58:04 GMT
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <1992Dec30.223911.16297@rchland.ibm.com> <1htthbINNqg9@shelley.u.washington.edu>
- Nntp-Posting-Host: florida.rchland.ibm.com
- Organization: IBM Rochester
- Lines: 67
-
- In article <1htthbINNqg9@shelley.u.washington.edu>, wiml@stein.u.washington.edu (William Lewis) writes:
- |> essbaum@rchland.vnet.ibm.com (Alexander Essbaum) writes:
- |> >if (!($pid = fork)) {
- |> > open (cmd,"$cmd|"); # $cmd is a call to another perl script
- |> > print <cmd>;
- |> > exit 0;
- |> >}
- |>
- |> >the stuff returned in <cmd> (which comes from print "..."; and `...`; lines
- |> >in $cmd) doesn't print properly. it seems to be getting buffered such that
- |> >when $cmd exits, all of STDERR gets printed, then all of STDOUT.
- |>
- |> Generally, stder doesn't get redirected along with stdout. (That's
- |> its purpose for being separate.) I'd guess that the error output is
- |> going straight to your tty when $cmd runs, but stdout is getting trapped
- |> in the pipe. Then you print the pipe's contents on the next line. So
- |> all the error output will precede the normal output.
- |>
-
- yes, this is what i thought. how do i fix the "trapped in the pipe"?
- i'm now using:
-
-
- if (!($pid = fork)) { # if child
- select(STDERR); $| = 1;
- select(STDOUT); $| = 1;
- print "child running\n";
- open (cmd,"$cmd 2>&1 |");
- print "cmd forked...\n";
- print <makepla>;
- print "child exitting...\n";
- exit 0;
- }
- print "parent continuing after fork...\n";
-
-
- $cmd is a call to a perl script that contains:
-
- print "blah blah\n";
- if (error) { die "died\n"; }
- print "more blah blah\n";
-
- what i get (in the xterm i ran the parent from) is:
-
- child running
- cmd forked...
- parent continuing after fork...
- <parent exits>
- <pause for ~1 minute while $cmd runs and *should* be printing stuff>
- died
- blah blah
- child exitting...
-
-
- shouldn't the 2>&1 in the "open" at least merge STDOUT and STDERR so the
- "blah blah" shows up before "died"? also, i need to have the print lines
- in $cmd printed in real time (no buffering). $cmd may not exit and i need
- to see where it's hung. if it never exits, the buffered prints never show
- up.
-
- if it matters, the parent must be able to loop and spawn multiple $cmd's.
- it's ok that they all print to the same tty as long as they do it as they
- go.
-
- many thanks in advance for any help!
-
- axel
-