home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / sgi / misc / 258 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  3.2 KB

  1. Path: sparky!uunet!psinntp!cmcl2!adm!news
  2. From: HERBER@fnal.fnal.gov (Randolph J. Herber, CD/DCD/SPG, x2966)
  3. Newsgroups: comp.sys.sgi.misc
  4. Subject: Re: unix pipes and X
  5. Message-ID: <34985@adm.brl.mil>
  6. Date: 12 Jan 93 18:48:48 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 97
  9.  
  10. +From: LANGER STEVEN C <sglanger@vela.acs.oakland.edu>
  11. +Newsgroups: comp.sys.sgi.graphics
  12. +Subject: unix pipes and X
  13. +Date: 12 Jan 93 02:54:40 GMT
  14. +To:       info-iris-graphics@BRL.MIL
  15.  
  16. +Despite the help of several netters, I cannot seem to master the 
  17. +mechanics of inter-process communication via pipes. Ultimately,
  18. +I want 2-way communication between a parent and child process.
  19. +This is what I've tried so far;
  20.  
  21. +/*
  22. +* parent.c    launches child and should get child's output
  23. +* standard include files
  24. +*/
  25.  
  26. +int pipes[2];
  27.  
  28.     ...
  29.  
  30. +    pipe (pipes);
  31.  
  32. +    if (fork() != 0)
  33.  
  34.     ...
  35.  
  36. +Everything runs, the child prints to the screen via the stderr and I get
  37. +no error codes, but the parent callback func is never entered. Could
  38. +anyone please tell me what the error is?
  39.  
  40. +--thanks in advance, steve
  41. +-- 
  42. +Steve Langer                     sglanger@argo.acs.oakland.edu (VMS)
  43. +Oakland University               sglanger@vela.acs.oakland.edu (Ultrix)
  44. +---------------------------------------------------------------------------
  45. +Standard disclaimers apply. In addition, the author makes no guarantees,
  46. +concerning the grammatical accuracy of his writing. Therefore, any ^B's, ^C's, 
  47. +midword character additions/deletions and other non-sense which occurs (after 
  48. +the work leaves the author's decade old text editor via his decade old Amiga, 
  49. +struggles through a local 1200 baud Merit server to be further mauled via the 
  50. +remote VAX mail servers) is someone elses problem - namely yours.
  51. +---------------------------------------------------------------------------
  52.  
  53. Remember that pipes are:
  54.     1. UNIdirectional
  55.     2. have two ends each; a pipe() system call returns both ends
  56.     3. fork() creates copies of both ends of pipes.
  57.     4. parents and children should close immediately after the fork()
  58.        any unnecessary ends
  59.  
  60.     int to_child[2], from_child[2], pid;
  61.  
  62.     if(pipe(to_child)) {
  63.         fprintf(stderr,"pipe(to_child) failed.\n");
  64.         exit(1);
  65.     }
  66.  
  67.     if(pipe(from_child)) {
  68.         fprintf(stderr,"pipe(from_child) failed.\n");
  69.         exit(1);
  70.     }
  71.  
  72.     switch(pid = fork()) {
  73.     case -1:
  74.         fprintf(stderr,"fork() failed.\n");
  75.         exit(1);
  76.     case 0:
  77.         close(0);    make to_child pipe stdin
  78.         if(dup(to_child[0]) != 0) {
  79.             fprint(stderr,"unable to make child's stdin\n");
  80.             exit(1);
  81.         }
  82.         close(to_child[1]);    /* unnecessary end */
  83.         close(1);    make to_child pipe stdout
  84.         if(dup(from_child[1]) != 1) {
  85.             fprint(stderr,"unable to make child's stdout\n");
  86.             exit(1);
  87.         }
  88.         close(from_child[0]);    /* unnecessary end */
  89.         exec...(child program);
  90.         fprintf(stderr,"child program failed to run.\n");
  91.         exit(1);
  92.     default:
  93.         close(to_child[0]);    /* unnecessary end */
  94.         close(from_child[1]);    /* unnecessary end */
  95.  
  96.         ...
  97.  
  98.         /* if necessary, wait on pid here */
  99.     }
  100.  
  101.     close(to_child[0]); close(to_child[1]);        /* assure all ends */
  102.     close(from_child[0]); close(from_child[1]);    /* are closed */
  103.  
  104. Randolph J. Herber, herber@fnalf.fnal.gov, +1 708 840 2966
  105. (Speaking for myself and not for US, US DOE, FNAL nor URA.)
  106. (Product, trade, or service marks herein belong to their respective owners.)
  107.