home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10289 < prev    next >
Encoding:
Text File  |  1992-08-21  |  1.5 KB  |  44 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ferkel.ucsb.edu!piggy!chupchup
  3. From: chupchup@ferkel.ucsb.edu (Robert Earl)
  4. Subject: Re: need help reading from a child
  5. Message-ID: <chupchup.714463805@piggy>
  6. Organization: (EVIL!)
  7. References: <1992Aug21.202934.3736@mercury.unt.edu>
  8. Date: Sat, 22 Aug 1992 06:10:05 GMT
  9. Lines: 33
  10.  
  11. gene@ponder.csci.unt.edu (Gene De Lisa) writes:
  12.  
  13. | This code hangs on the read (or fread).
  14.  
  15. |      if (pipe(pipefds) < 0) {
  16. |       perror("pipe");
  17. |       return(NULL);
  18. |      }
  19.  
  20.     [ code deleted... ]
  21.  
  22. |       dup2(pipefds[0],0);
  23. |       dup2(pipefds[1],1);
  24. |       dup2(pipefds[1],2);
  25.  
  26. What you're doing here is setting up a loop: the same process that
  27. writes to its stdout/err is going to read what it just wrote on stdin,
  28. because writes to pipefds[1] come out on reads on pipefds[0].
  29.  
  30. You need to close pipefds[0] in the child and dup2() pipefds[1] onto
  31. the desired fds.  Close pipefds[1] in the parent, then read from
  32. pipefds[0].
  33.  
  34. If you actually want to write stuff to the child's stdin, make another
  35. call to pipe() and close the uneeded descriptors in each process.  All
  36. the usual warnings about potential deadlocks apply.  To do this right
  37. you would need to use ptys or something similar - see the FAQ.  (Or
  38. was that just the comp.lang.perl FAQ?)
  39. -- 
  40. "If you've got a pig that likes jumping fences, | robert earl
  41. you have to make its fence a lot higher all at  | rearl@ucsd.edu
  42. once -- if you do it by increments, all you're  | rearl@piggy.ucsb.edu
  43. doing is training a jumping pig." -Henry Spencer|
  44.