home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4978 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  1.9 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!convex!convex!tchrist
  2. From: tchrist@convex.COM (Tom Christiansen)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Pipe, et al, what am I doing wrong?
  5. Keywords: pipe, fork
  6. Message-ID: <1992Jul27.145923.5083@news.eng.convex.com>
  7. Date: 27 Jul 92 14:59:23 GMT
  8. References: <1992Jul27.115715.21337@neptune.inf.ethz.ch>
  9. Sender: usenet@news.eng.convex.com (news access account)
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. Organization: CONVEX Realtime Development, Colorado Springs, CO
  12. Lines: 55
  13. Originator: tchrist@pixel.convex.com
  14. Nntp-Posting-Host: pixel.convex.com
  15. X-Disclaimer: This message was written by a user at CONVEX Computer
  16.               Corp. The opinions expressed are those of the user and
  17.               not necessarily those of CONVEX.
  18.  
  19. From the keyboard of weingart@inf.ethz.ch:
  20. :Here is a small script, that seems to hang.  What am I doing
  21. :wrong?  A ps shows that the child is waiting for the parent
  22. :to `wait` on it.  Also, the parent seems to hang because it
  23. :does not get EOF.
  24. :
  25. :
  26. :#!/usr/local/bin/perl
  27. :#
  28. :# To test fork, pipe
  29. :# and other things.
  30. :
  31. :select(STDIN); $| = 1;
  32. :select(STDERR); $| = 1;
  33. :select(STDOUT); $| = 1;
  34. :
  35. :pipe( READH, WRITEH );
  36. :$pid = fork();
  37. :die "Could not fork.\n" if( ! defined($pid) );
  38. :
  39. :@data = ();
  40. :if( $pid == 0 ){
  41. :    open(STDOUT, ">&WRITEH");
  42. :    open(STDERR, ">&WRITEH");
  43. :    exec( '/bin/cat', '/etc/passwd' );
  44. :}else{
  45. :    open(STDIN, "<&READH");
  46. :    while( <STDIN> ){
  47. :        push(@data, $_);
  48. :        print ".";
  49. :    }
  50. :    print "\nLines: $#data\n";
  51. :}
  52. :
  53. :exit(0);
  54. :
  55. :#
  56. :# End of script.
  57. :
  58. :
  59. :Can anyone give me a working example of this?
  60.  
  61. Sure -- just close(READH) in the kid before the exec, 
  62. and close(WRITEH) in the parent before you dup STDIN.
  63.  
  64. But you're going to way to much trouble.  Using the 
  65. implicit fork of open on "|-" and "-|" is much easier,
  66. and almost always suffices.
  67.  
  68. --tom
  69. -- 
  70.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  71.  
  72.  
  73.     Real Programmers think better when playing Adventure or Rogue.
  74.