パイプの 1 つの実装は次のとおりです。
#define STD_INPUT 0
#define STD_OUTPUT 1
pipeline(char *process1, char *process2)
{
int fd[2];
pipe(&fd[0]);
if (fork() != 0) {
/* The parent process executes these statements. */
close(fd[0]);
close(STD_OUTPUT);
dup(fd[1]);
close(fd[1]); /* this file descriptor not needed anymore */
execl(process1, process1, 0);
}
else {
/* The child process executes these statements. */
close(fd[1]);
close(STD_INPUT);
dup(fd[0]);
close(fd[0]); /* this file descriptor not needed anymore */
execl(process2, process2, 0);
}
}
それぞれの dup 呼び出しに続く 2 つのステートメントの使用に混乱しています。
close(fd[1]); /* this file descriptor not needed anymore */
と
close(fd[0]); /* this file descriptor not needed anymore */
記述子は不要になったと言われていますが、私にとってそれらの記述子はパイプの両端を表しているのに、なぜ不要になったのでしょうか?