首先要鬆散的,如果嵌套或事情會變得與管道更多的進程確實無法讀取。
對於3流程效仿以下的bash行爲./addone | ./addone | ./addone
,你會想要寫的東西,如:拇指
#include <unistd.h> // dup2(), pipe(), fork(), execvp()
#include <sys/wait.h> // waitpid()
int main() {
int pipe1[2], pipe2[2];
int pid0, pid1, pid2;
char *argp = {"./addone", "first_argument", "second_argument", NULL};
pipe(pipe1);
pid0 = fork();
if (pid0 == 0) {
// close the read end of pipe1:
close(pipe1[0]);
// redirect stdout to the write end of pipe1:
dup2(pipe1[1], 1);
execvp("./addone", argp);
}
pipe(pipe2);
pid1 = fork();
if (pid1 == 0) {
// close the write end of pipe1:
close(pipe1[1]);
// close the read end of pipe2:
close(pipe2[0]);
// redirect stdin to the read end of pipe1:
dup2(pipe1[0], 0);
// redirect stdout to the write end of pipe2:
dup2(pipe2[1], 1);
execvp("./addone", argp);
}
pid2 = fork();
if (pid2 == 0) {
// close unused pipe1:
close(pipe1[0]);
close(pipe1[1]);
// close the write end of pipe2:
close(pipe2[1]);
// redirect stdin to the read end of pipe2:
dup2(pipe2[0], 0);
execvp("./addone", argp);
}
waitpid(pid0, NULL, 0);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
}
規則:1套管道的每對流程或'#processes - 1'。以「proc1 |」的方式進行3個進程proc2 | proc3'你需要2套管子。另一個區別是'proc2'中'stdin'和'stdout'都被重定向了。要創建3個進程,你需要從父進程fork()兩次或者從子進程運行fork()來創建一個孫子進程。 – alvits
,正如你已經知道如何使'p1 | p2',想想'p1 | p2 | p3'或者是(p1 | p2)| p3'或'p1 | (p2 | p3)'。 –
你能告訴我如何做dup2部分?它真的讓人困惑 –