home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7637 < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.6 KB  |  81 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!florida!essbaum
  3. From: essbaum@rchland.vnet.ibm.com (Alexander Essbaum)
  4. Subject: Re: fork and STDOUT
  5. Sender: news@rchland.ibm.com
  6. Message-ID: <1992Dec31.185804.9837@rchland.ibm.com>
  7. Date: Thu, 31 Dec 1992 18:58:04 GMT
  8. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  9. References: <1992Dec30.223911.16297@rchland.ibm.com> <1htthbINNqg9@shelley.u.washington.edu>
  10. Nntp-Posting-Host: florida.rchland.ibm.com
  11. Organization: IBM Rochester
  12. Lines: 67
  13.  
  14. In article <1htthbINNqg9@shelley.u.washington.edu>, wiml@stein.u.washington.edu (William Lewis) writes:
  15. |> essbaum@rchland.vnet.ibm.com (Alexander Essbaum) writes:
  16. |> >if (!($pid = fork)) {
  17. |> >    open (cmd,"$cmd|");   # $cmd is a call to another perl script
  18. |> >    print <cmd>;
  19. |> >    exit 0;
  20. |> >}
  21. |> 
  22. |> >the stuff returned in <cmd> (which comes from print "..."; and `...`; lines
  23. |> >in $cmd) doesn't print properly.  it seems to be getting buffered such that
  24. |> >when $cmd exits, all of STDERR gets printed, then all of STDOUT. 
  25. |> 
  26. |>   Generally, stder doesn't get redirected along with stdout. (That's
  27. |> its purpose for being separate.) I'd guess that the error output is
  28. |> going straight to your tty when $cmd runs, but stdout is getting trapped
  29. |> in the pipe. Then you print the pipe's contents on the next line. So
  30. |> all the error output will precede the normal output.
  31. |> 
  32.  
  33. yes, this is what i thought.  how do i fix the "trapped in the pipe"?
  34. i'm now using:
  35.  
  36.  
  37.   if (!($pid = fork)) {  # if child
  38.     select(STDERR); $| = 1;
  39.     select(STDOUT); $| = 1;
  40.     print "child running\n";
  41.     open (cmd,"$cmd 2>&1 |");
  42.     print "cmd forked...\n";
  43.     print <makepla>;
  44.     print "child exitting...\n";
  45.     exit 0;
  46.   }
  47.   print "parent continuing after fork...\n";
  48.  
  49.  
  50. $cmd is a call to a perl script that contains:
  51.  
  52.   print "blah blah\n";
  53.   if (error) { die "died\n"; }
  54.   print "more blah blah\n";
  55.  
  56. what i get (in the xterm i ran the parent from) is:
  57.  
  58. child running
  59. cmd forked...
  60. parent continuing after fork...
  61. <parent exits>
  62. <pause for ~1 minute while $cmd runs and *should* be printing stuff>
  63. died
  64. blah blah
  65. child exitting...
  66.  
  67.  
  68. shouldn't the 2>&1 in the "open" at least merge STDOUT and STDERR so the
  69. "blah blah" shows up before "died"?  also, i need to have the print lines
  70. in $cmd printed in real time (no buffering).  $cmd may not exit and i need
  71. to see where it's hung.  if it never exits, the buffered prints never show
  72. up.
  73.  
  74. if it matters, the parent must be able to loop and spawn multiple $cmd's.
  75. it's ok that they all print to the same tty as long as they do it as they
  76. go.
  77.  
  78. many thanks in advance for any help!
  79.  
  80. axel
  81.