home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / windows / x / motif / 8325 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  3.3 KB

  1. From: pirson@misty.boeing.com (Chris Pirson)
  2. Date: Tue, 5 Jan 1993 00:54:10 GMT
  3. Subject: Re: HELP pipe communication?
  4. Message-ID: <66040009@misty.boeing.com>
  5. Organization: Boeing Commercial Airplane BCS Support
  6. Path: sparky!uunet!bcstec!misty!pirson
  7. Newsgroups: comp.windows.x.motif
  8. References: <1993Jan3.192240.1168@vela.acs.oakland.edu>
  9. Lines: 80
  10.  
  11. sglanger@vela.acs.oakland.edu (LANGER STEVEN C) writes:
  12.  
  13.    >I've got a foreground X app that launches a child process (non-
  14.    >graphical) for data acquisition. I want to establish 2-way pipe
  15.    >communication between said parent-child.
  16.  
  17.    >Vol 4 of the Xt Intrinsics Manual by Adrian Nye shows on pg.282-283
  18.    >how to use popen, but this only gives one way communication and
  19.    >since the example didn't show how to modify the child, I could
  20.    >not even get the example to work.
  21.  
  22.    >Volume 6, Motif Programming Manual by Dan Heller on pg. 699 says,
  23.  
  24.    >1. call pipe to get the input and output channels
  25.    >2. call fork to spawn the child
  26.    >3. use dup2 on the child side to redirect stdout and stdin.
  27.    >4.  use XtAppAddInput to monitor the pipes.
  28.  
  29.    >  Sadly, there is no code example of this procedure.
  30.  
  31.    >I've since gone to the man pages and I see that pipe(int *fildes)
  32.    >returns two ints;
  33.    >    filedes[0] being for read and
  34.    >    filedes[1] being for write
  35.  
  36.    >XtAppAddInput requirs bona fide FILE pointers. How do I get them from
  37.    >pipes it  returns? Also, my man server knows a total of 2 
  38.    >sentences about buf2. I really could use a code fragment from both sides
  39.    >to put this puzzle together.  Any UNIX interprocess communication
  40.    >GURUS with X experience out there? I'll post a summary of methods
  41.    >if there is interest.
  42.  
  43.    >--thanks in advance, steve
  44.  
  45.  
  46.    >P.S. I'm running an SGI Indigo with Os 4.0.1
  47.  
  48. I have done this sucessfully by taking the sample source code in the
  49. pipe man page and used XtAppAddInput to get the input back to my
  50. application.  XtAppAddInput (O'Reilly Volume 5, pg 86) uses a file
  51. descriptor returned from the pipe as-is, not a FILE *.
  52.  
  53. I have this running on an HP750 running HP-UX 8.07, X11R4.
  54.  
  55. --------snip------------snip------------snip------------snip--------
  56. static int start_cpu_monitor(void)
  57. {
  58.     int    pipefd[2];
  59.  
  60.     if(pipe(pipefd) == -1) return OUT_OF_CHANNELS;
  61.  
  62.     if((cpu_monitor_pid = fork()) == (pid_t)0) {
  63.         /* child process comes here */
  64.         close(1);    /* replace stdout with the pipe */
  65.         dup(pipefd[1]);
  66.         close(pipefd[0]);
  67.         execlp(CPU_MONITOR, CPU_MONITOR, CPU_UPDATE_INTERVAL, (char *)0);
  68.         fprintf(stderr, "WARNING: CPU Monitor failed to start\n");
  69.  
  70.     }else if(cpu_monitor_pid > (pid_t)0) {
  71.         /* parent process comes here */
  72.  
  73.         /* give input pipe to X server  */
  74.  
  75.         XtAppAddInput(UxAppContext, pipefd[0],
  76.             (XtPointer)XtInputReadMask, handle_cpu_monitor_input, NULL);
  77.     }
  78.     return EXEC_GOOD;
  79. }
  80. --------snip------------snip------------snip------------snip--------
  81. CPU_MONITOR is a function like vmstat, sending status to stdout (now the
  82. pipe) periodically.
  83. handle_cpu_monitor_input is an XtInputCallbackProc which is called any
  84. time something comes in over the pipe from CPU_MONITOR.
  85.  
  86. --
  87. Chris Pirson                 |B\______o=o=o.        Flight Test Engineering
  88.                              --=o=o=o=o=o=o=>       Boeing Commercial Airplanes
  89. pirson@misty.boeing.com           / ,'              Boeing Field, Seattle, WA
  90. uunet!bcstec!misty!pirson   ..   /,'   76 MIPS at 40,000 feet!
  91.