Code:
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int pid, status;
int pid1, pid2;
int filedes[2];
pipe(filedes);
switch(pid=fork())
{
case 0:
{
//close(1);dup(0);
pid1 = fork();
if(pid1==0)
{
dup2(filedes[1], 1);
// close(filedes[0]);
execl ("/bin/ls", "ls", "-lh", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
pid2 = fork();
if(pid2==0)
{
dup2(filedes[0], 0);
// close(filedes[1]);
execl ("/bin/cat", "cat", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
break;
}
default:
{
wait(&status);
}
}
close(filedes[0]);
close(filedes[1]);
return 0;
}
ok... now 2 questions. why doesn't this program exits ? it remains in cat (i belive). And second why is there the need to close the other side of the pipe ?