1
我需要實現這樣的IPC架構:多管道不工作
runtime data -> filter1 -> filter2 -> output
。 (與data | filter1 | filter2
相同)。
我可以將數據傳遞給第一個篩選器,但對於第二個我不能(可能是因爲在第一個子標準輸出fd沒有關閉)。如何正確實現這樣的模式?
P.S. filter1和filter2只是從標準輸入讀取並寫入標準輸出。
我的代碼:
int main() {
int fd1[2];
pipe(fd1);
pid_t pid1;
if ((pid1 = fork()) > 0) {
char data[] = "Hello world!";
close(fd1[0]);
write(fd1[1], data, sizeof(data));
close(fd1[1]);
wait(NULL);
exit(EXIT_SUCCESS);
} else if (pid1 == 0) {
int fd2[2];
pipe(fd2);
pid_t pid2;
dup2(fd1[0], STDIN_FILENO);
dup2(fd2[1], STDOUT_FILENO);
close(fd1[0]); close(fd1[1]);
close(fd2[0]); close(fd2[1]);
if ((pid2 = fork()) > 0) {
execl("./upcase", "upcase", NULL);
perror("execl");
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
close(fd1[0]); close(fd1[1]);
dup2(fd2[0], STDIN_FILENO);
close(fd2[0]); close(fd2[1]);
execl("./reverse", "reverse", NULL);
perror("execl");
exit(EXIT_FAILURE);
} else {
perror("pid2");
exit(EXIT_FAILURE);
}
} else {
perror("pid1");
exit(EXIT_FAILURE);
}
}
「*也許是因爲在第一個孩子的stdout FD沒有關閉*」 - 好像你已經發現的問題。由於第一個過濾器從來沒有在其輸入管道上看到EOF(因爲它被另一個進程保持打開),所以它永遠不會完成。 – Dolda2000
@ Dolda2000是的,但我怎樣才能關閉標準輸出外部進程? – 0x1337
您已經在最親的過程中關閉了它;問題似乎是你不會在你的「反向」孩子中關閉它。 – Dolda2000