2 つの子プロセスを作成する必要があります。1 つの子は、コマンド「ls -al」を実行し、その出力を次の子プロセスの入力にリダイレクトする必要があります。これにより、入力データに対してコマンド「sort -r -n -k 5」が実行されます。最後に、親プロセスはそれ (データは既にソート済み) を読み取り、ターミナルに表示する必要があります。(プログラムを実行したときの) ターミナルでの最終結果は、次のコマンドをシェルに直接入力した場合と同じになるはずです: "ls -al | sort -r -n -k 5"。このために、次のメソッドを使用する必要があります: pipe()、fork()、execlp()。
プログラムはコンパイルされますが、端末に目的の出力が得られません。何が悪いのかわかりません。コードは次のとおりです。
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main()
{
int fd[2];
pid_t ls_pid, sort_pid;
char buff[1000];
/* create the pipe */
if (pipe(fd) == -1) {
fprintf(stderr, "Pipe failed");
return 1;
}
/* create child 2 first */
sort_pid = fork();
if (sort_pid < 0) { // error creating Child 2 process
fprintf(stderr, "\nChild 2 Fork failed");
return 1;
}
else if(sort_pid > 0) { // parent process
wait(NULL); // wait for children termination
/* create child 1 */
ls_pid = fork();
if (ls_pid < 0) { // error creating Child 1 process
fprintf(stderr, "\nChild 1 Fork failed");
return 1;
}
else if (ls_pid == 0) { // child 1 process
close(1); // close stdout
dup2(fd[1], 1); // make stdout same as fd[1]
close(fd[0]); // we don't need this end of pipe
execlp("bin/ls", "ls", "-al", NULL);// executes ls command
}
wait(NULL);
read(fd[0], buff, 1000); // parent reads data
printf(buff); // parent prints data to terminal
}
else if (sort_pid == 0) { // child 2 process
close(0); // close stdin
dup2(fd[0], 0); // make stdin same as fd[0]
close(fd[1]); // we don't need this end of pipe
execlp("bin/sort", "sort", "-r", "-n", "-k", "5", NULL); // executes sort operation
}
return 0;
}