3
我寫了這個代碼來管2個命令:Ç - Linux的管道3個命令不起作用
// ls -l | tail -n 2
int pfd[2];
pid_t pid;
char *cmd1[] = {"ls", "-l", 0};
char *cmd2[] = {"tail", "-n", "2", 0};
pipe(pfd);
pid = fork();
if (pid == 0) // child
{
dup2(pfd[1], STDOUT_FILENO);
close(pfd[0]); /* the child does not need this end of the pipe */
execvp(cmd1[0], cmd1);
_exit(0);
}
else // parent
{
dup2(pfd[0], STDIN_FILENO);
close(pfd[1]); /* the child does not need this end of the pipe */
execvp(cmd2[0], cmd2);
}
此代碼工作正常。現在
我想管3級的命令和我寫了這個代碼:
// ls -l | tail -n 2 | head -n 1
int pfd1[2];
int pfd2[2];
pid_t pid1, pid2;
char *cmd1[] = {"ls", "-l", 0};
char *cmd2[] = {"tail", "-n", "2", 0};
char *cmd3[] = {"head", "-n", "1", 0};
pipe(pfd1);
pid1 = fork();
if (pid1 == 0) // child 1
{
dup2(pfd1[1], STDOUT_FILENO);
close(pfd1[0]); /* the child does not need this end of the pipe */
execvp(cmd1[0], cmd1);
_exit(0);
}
else // parent
{
pipe(pfd2);
pid2 = fork();
if (pid2 == 0) // child 2
{
dup2(pfd1[0], STDIN_FILENO);
close(pfd1[1]); /* the child does not need this end of the pipe */
dup2(pfd2[1], STDOUT_FILENO);
close(pfd2[0]); /* the child does not need this end of the pipe */
execvp(cmd2[0], cmd2);
_exit(0);
}
else // parent
{
dup2(pfd2[0], STDIN_FILENO);
close(pfd2[1]); /* the child does not need this end of the pipe */
execvp(cmd3[0], cmd3);
}
}
此代碼編譯,但它只是需要從控制檯輸入永遠。
我做錯了什麼,我該如何解決?
如果你想在原來的進程繼續(你通常會哪些),你需要'fork'一次每一個命令。每個命令之間需要一個管道。所以對於兩個命令(你的第一個例子),你需要兩個子進程(即兩個'fork'調用)和一個管道。對於第二個例子,您需要三次'fork'調用來創建三個子進程和兩個管道。 –