Friday, April 08, 2005

Of pipes, dups, and forks

It has been a long time since I used pipes, I/O duplication, and process forking in a C program. However, I need to remember how it is done. The UNIX command ls | wc is implemented this way in C.

pipe(p);

if ((pid1 = fork()) == 0) {
close(p[1]);
dup2(p[0],0);
close(p[0]);
execl("wc","wc",(char *)0);
exit(1);
}
if ((pid1 = fork()) == 0) {
close(p[0]);
dup2(p[1],1);
close(p[1]);
execl("ls","ls",(char *)0);
exit(1);
}
close(p[0]);
close(p[1]);

while(wait(&status) != pid1);

1 comment:

F said...

i totally get this...not hehehe.

Published Scholars in the Philippines

Using Google Scholar data, webometrics ranks 453 scientists in the Philippines (June 2016 report). Each of these scientists has at least an...