Exec/Fork/Stdout (Eek! Help!)

Larry Starr,Mezzanine,46971,5638828 starr at mrsvr.UUCP
Wed Aug 1 07:39:55 AEST 1990


>From article <7890 at ucrmath.ucr.edu>, by yakker at ucrmath.ucr.edu (matt robinson):
> 
> I have been working on a program in X windows that needs to
> fork off a child process, exec the process, and send the data
> from stdout/stderr back to the parent, and produce it into
> a window.  Now, the hard part about this is that I'M LOST! :)

You don't mention what system/version you are working on so I'll
attempt to take a generic stab at it.

If you have popen it comes close, but will not give you stderr.

> If anyone can offer any assistance, the problem (more specifically)
> is this:  I have a compiler that produces specific error messages
> that need to be displayed to the user.  In order to do this, we 
> want to take the stdout and stderr and give it back to the window,
> at the same time executing the compiler, and having it produce its
> own code.  I assume (I might be wrong..) that the exec'd process
> needs to be forked off as a child, which will return the messages
> to the parent using pipes.  If anyone can show some example (in any
> degree) on what can be done to accomplish this, I would be in their
> debt.  If you have a piece of code to show this, that would be even
> more helpful.  Thanks for your time.

Briefly, to give a couple of pointers, you have the right idea.

1. Create your sockets.
2. Fork a child
   In the child:
   close the read end of the socket(s)
   dup stdout and stderr from the write end of the socket(s)
   exec the process.

   In the Parent:
   close the write end of the socket(s)
   read the output from the spawned process EOF indicates
   the child is done.

   Remember to wait for the child process, to prevent orphans.

   If you are on a Berkley system more information can be 
   obtained from the "IPC Tutorial".  I'm not sure on system 5.

A crude way to do the same thing is to create a command line
that redirects stdout and stderr into known filenames.
Hand the command to the "system()" function and read the 
output after the fact.

--
Larry Starr
MR Research Facility
G.E. Medical Systems



More information about the Comp.unix.questions mailing list