Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!ucdavis!csusac!usenet From: emmonsl@athena.ecs.csus.edu (L. Scott Emmons) Newsgroups: comp.unix.programmer Subject: how to use pipes (was: Re: system() question) Message-ID: <1991Mar3.214844.15444@csusac.csus.edu> Date: 3 Mar 91 21:48:44 GMT References: <9321@hub.ucsb.edu> <1991Mar2.181423.13981@csusac.csus.edu> <1991Mar3.175234.11716@csusac.csus.edu> Sender: usenet@csusac.csus.edu (News account (alt)) Distribution: comp Organization: California State University, Sacramento Lines: 89 I thought that perhaps some people would be interested in just how to use fork(), exec(), and pipe() to do the same thing as is possible with a system(). I cooked up a couple of quick programs. The first implements redirection using fork(), exec(), and pipe(). The second does the exact same things, except uses system() instead. NOTE: I implemented these on a BSD system...It can probably be ported over to sysV reasonably easily...change SIGCHLD to SIGCLD, and that should be about it... Just for kicks, I compared the output from 'time' for both versions of the program. Here are some average results: using fork/exec/pipe: 0.0u 0.1s 0:00 35% 5+6k 0+0io 0pf+0w using system: 0.2u 0.4s 0:00 70% 32+18k 0+4io 0pf+0w While for this trivial usage the times aren't that much difference, a larger program surely would be... Here are the two programs, enjoy and if you have any questions, comments, etc please feel free to email me at emmons@csus.csus.edu: ---CUT HERE start pipe.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 child_changed(); int fd[2]; main() { char ch=0; pipe(fd); if (fork()) { signal(SIGCHLD,child_changed); while(read(fd[0],&ch,1)) putchar(ch); close(fd[0]); } else { dup2(fd[1],1); execl("/usr/ucb/last","last","emmonsl",(char *)0); } } child_changed() { close(fd[1]); } ---CUT HERE end pipe.c--- ---CUT HERE start system.c--- /* This program shows how to (or one way to) use system() to do what fork(), exec(), and pipe() can do... It redirects the output of a program to a file, then reads the file back in and prints it. This code was written by L. Scott Emmons, emmons@csus.csus.edu. */ #include main() { FILE *fp; char ch; system("/usr/ucb/last emmonsl >filename"); fp=fopen("filename","r"); while (fread(&ch,1,1,fp)) putchar(ch); fclose(fp); unlink("filename"); } ---CUT HERE end system.c--- L. Scott Emmons --------------- emmons@csus.csus.edu ...[ucbvax]!ucdavis!csus!emmons Packet: kc6nfp@kg6xx.#nocal.ca.usa.na