how to use pipes (was: Re: system() question)
L. Scott Emmons
emmonsl at athena.ecs.csus.edu
Mon Mar 4 07:48:44 AEST 1991
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 at 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 at csus.csus.edu.
*/
#include <stdio.h>
#include <signal.h>
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 at csus.csus.edu.
*/
#include <stdio.h>
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 at csus.csus.edu <or> ...[ucbvax]!ucdavis!csus!emmons
Packet: kc6nfp at kg6xx.#nocal.ca.usa.na
More information about the Comp.unix.programmer
mailing list