我必须编写一个程序来执行与du |相同的操作。排序 | 命令行中的head会做,但我被卡住了,我的程序不工作。现在的输出是112 。并且程序不会终止。请帮忙,我不知道该怎么办!
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);
}
}