Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!abcfd20.larc.nasa.gov!ames!excelan!murthy From: murthy@la.excelan.com (M.D. Srinivas Murthy) Newsgroups: comp.unix.questions Subject: Re: Exec/Fork/Stdout (Eek! Help!) Message-ID: <1621@excelan.COM> Date: 30 Jul 90 20:37:36 GMT References: <7890@ucrmath.ucr.edu> Sender: news@excelan.COM Reply-To: murthy@la.excelan.com (M.D. Srinivas Murthy) Organization: Excelan, Inc., San Jose, Califonia Lines: 50 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 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.