Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!wuarchive!csus.edu!ucdavis!csusac!usenet From: emmonsl@athena.ecs.csus.edu (L. Scott Emmons) Newsgroups: comp.unix.programmer Subject: More on how to do a pipe (was re: system() problem) Message-ID: <1991Mar6.025034.8697@csusac.csus.edu> Date: 6 Mar 91 02:50:34 GMT Sender: usenet@csusac.csus.edu (News account (alt)) Organization: California State University, Sacramento Lines: 46 After some thinking about my pipe(), fork(), exec() implementation in my last post, I knew there was something funky about the way I did it... Here is another version of my pipe program, about as efficient as my previous version, but a bit cleaner (unless use of SIGCHLD is used for something other than just closing the pipe() down.) Here it is, hope you find it useful: ---CUT HERE begin pipe2.c--- /* This program shows how to (or one way to) implement fork() and exec() on a pipeline, redirecting the stdout of the exec()d program into the pipeline. This code was written by L. Scott Emmons, emmons@csus.csus.edu. */ #include #include int fd[2]; main() { char ch=0; pipe(fd); if (fork()) { close(fd[1]); while(read(fd[0],&ch,1)) putchar(ch); close(fd[0]); } else { dup2(fd[1],1); execl("/usr/ucb/last","last","emmonsl",(char *)0); } } ---CUT HERE end pipe2.c--- L. Scott Emmons --------------- emmons@csus.csus.edu ...[ucbvax]!ucdavis!csus!emmons Packet: kc6nfp@kg6xx.#nocal.ca.usa.na