I have to write a program that will perform the same operation that du | sort | head in the command line would do, but I'm stuck, and my program is not working. The output right now is 112 . and the program doesn't terminate. Please help, I don't know what to do!
int main(void) {
int fd[2];
int fd1[2];
int pid;
if (pipe(fd) == -1) {
perror("Pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
execl("/usr/bin/du", "du", (char *) 0);
exit(3);
}
if (pipe(fd1) == -1) {
perror("Pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd[0], STDIN_FILENO);
dup2(fd1[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/sort", "sort", (char *) 0);
exit(3);
}
close(fd[0]);
close(fd[1]);
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd1[0], STDIN_FILENO);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/head", "head", (char *) 0);
exit(3);
}
}
Let head be your parent process, sort — its child process, and du — the child of sort, or the grandchild of head.
You need two pipes, thus, two arrays — fd and fd1. Let the fd pipe connect sort with head, and fd1 — du with sort.
You will need one big switch statement, which will determine whether you currently are in the parent process (head, pipe(fd) is not 0) or the child (sort, pipe(fd) is 0). If you are in sort, you need to create the fd1 pipe and run the grandchild process du. Now, since you again have two processes (three in total), you need to set a pipe according to your location — whether you are in the grandchild or the child process. You can use a similar switch statement as you did for pipe fd. The trick here is to set the input and output for fd1 pipe correctly.
Your code must do something like this: