home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / sgi / graphics / 124 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.8 KB  |  112 lines

  1. Newsgroups: comp.sys.sgi.graphics
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!ux1.cso.uiuc.edu!cs.uiuc.edu!vela!sglanger
  3. From: sglanger@vela.acs.oakland.edu (LANGER STEVEN C)
  4. Subject: unix pipes and X
  5. Message-ID: <1993Jan12.025440.1874@vela.acs.oakland.edu>
  6. Organization: Oakland University, Rochester MI.
  7. Distribution: na
  8. Date: Tue, 12 Jan 1993 02:54:40 GMT
  9. Lines: 101
  10.  
  11. Despite the help of several netters, I cannot seem to master the 
  12. mechanics of inter-process communication via pipes. Ultimately,
  13. I want 2-way communication between a parent and child process.
  14. This is what I've tried so far;
  15.  
  16. /*
  17. * parent.c    launches child and should get child's output
  18. * standard include files
  19. */
  20.  
  21. int pipes[2];
  22.  
  23.  
  24. void cbfunc (Widget w, XtPointer data, XtPointer dummy);
  25.  
  26. main (int argc, char **argv)
  27. {
  28.  
  29.     XtAppContext app_parent;
  30.     Widget topLevel;
  31.  
  32.     topLevel = XtVaAppInitialize (&app_parent, "Parent", NULL, 0,
  33.                 &argc, argv, NULL, NULL);
  34.     pipe (pipes);
  35.  
  36.     if (fork() != 0)
  37.     {
  38.         XtAppAddInput (app_parent, pipes[0], XtInputReadMask, 
  39.                 cbfunc, NULL);
  40.     }
  41.     else
  42.     {
  43.         status = dup2 (pipes[1], STDOUT_FILENO);
  44.         close (pipes[0]);
  45.         if (status < 0)
  46.         {    
  47.             fprintf (stderr, "pipe error\n");
  48.             exit (0);
  49.         }
  50.         execlp ("child", "child", (char *)0);
  51.     }
  52.  
  53.     XtRealizeWidget (topLevel);
  54.     XtAppMainLoop (app_parent);
  55. }
  56.       
  57. void cbfunc (Widget w, XtPointer data, XtPointer dummy)
  58. {
  59.     char buf[BUFSIZE] = "\0";
  60.  
  61.     read (pipes[0], buf, sizeof(buf));
  62.     fprintf (stderr, "in Parent buf=%s\n", buf);
  63. }
  64.  
  65.  
  66.  
  67. /*
  68. *
  69. * child.c    prints to stdout & stderr every 10 seconds
  70. * necessary includes
  71. */
  72.  
  73.  
  74. void alarmcb (void);
  75.  
  76. main (void)
  77. {
  78.     XtAppContext app_child;
  79.  
  80.     app_child = XtCreateApplicationContext ();
  81.     signal (SIGALM, alarmcb);
  82.     alarm (10);
  83.  
  84.     XtAppMainLoop (app_child);
  85. }
  86.  
  87. void alarmcb (void)
  88. {
  89.     fprintf (stdout, "in child \n");
  90.     fprinf (stderr, "in child\n");
  91.     signal (SIGALM, alarmcb);
  92.     alarm (10);
  93. }
  94.     
  95.     
  96. Everything runs, the child prints to the screen via the stderr and I get
  97. no error codes, but the parent callback func is never entered. Could
  98. anyone please tell me what the error is?
  99.  
  100. --thanks in advance, steve
  101. -- 
  102. Steve Langer                     sglanger@argo.acs.oakland.edu (VMS)
  103. Oakland University               sglanger@vela.acs.oakland.edu (Ultrix)
  104. ---------------------------------------------------------------------------
  105. Standard disclaimers apply. In addition, the author makes no guarantees,
  106. concerning the grammatical accuracy of his writing. Therefore, any ^B's, ^C's, 
  107. midword character additions/deletions and other non-sense which occurs (after 
  108. the work leaves the author's decade old text editor via his decade old Amiga, 
  109. struggles through a local 1200 baud Merit server to be further mauled via the 
  110. remote VAX mail servers) is someone elses problem - namely yours.
  111. ---------------------------------------------------------------------------
  112.