2

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);
}

}

4

1 に答える 1