[comp.unix.questions] Exec/Fork/Stdout

yakker@ucrmath.ucr.edu (matt robinson) (07/30/90)

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! :)

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.

Matt D. Robinson
yakker@ucrmath.ucr.edu
3023yakk@ucsbuxa.ucsb.edu

murthy@la.excelan.com (M.D. Srinivas Murthy) (07/31/90)

In article <7890@ucrmath.ucr.edu> yakker@ucrmath.ucr.edu (matt robinson) writes:
>
>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! :)
>

Here is a small program which forks a child and captures the stdout and stderr
of child with one descriptor :

------------------------- Cut here --------------------------------------
#include <stdio.h>

main()
{
	int fd[2] ;
	char buf[80];
	int ch ;

	pipe(fd);

	if ( fork() ) {
		close(fd[1]);
		memset(buf,0,80);
		while(read(fd[0],buf,80)) {
			printf("read: %s\n",buf);
			memset(buf,0,80);
		}
	}
	else {
		close(fd[0]);
		close(1);
		close(2);
		dup(fd[1]);
		dup(fd[1]);
		close(fd[1]);
		printf("Child writing to stdout\n");
		fflush(stdout) ;
		fprintf(stderr,"Child writing to stderr\n");
		fflush(stderr) ;
	}
}

---------------------- End --------------------------------------------

however, note that the stderr and stdout of the child may get intermingled.
Of course you can open two pipes and keep the stderr and stdout separate.

if you want to exec a program in the child, you can do it after the stdout
and stderr are mapped to the pipe communicating with the parent.

starr@mrsvr.UUCP (Larry Starr,Mezzanine,46971,5638828) (08/01/90)

From article <7890@ucrmath.ucr.edu>, by yakker@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