我目前在进行以下练习时遇到问题:
我想“模仿”管道命令行 ls | wc
在 Linux bash 中使用以下程序。
我所做的是:
- 创建管道
- 创建读者子级和编写器子级
- 编写器子进程关闭管道的读取端并将其标准输出重定向到管道的写入端
- 读者 child 关闭管道的写入端并使管道的读取端成为他的标准输入
- 两个子进程都执行 exec,编写器执行 ls 程序,通过管道将输出传递给读取器,读取器在该输出上执行 wc 程序。<
当我这样做时ls | wc
在 Linux 终端中我得到以下结果:
8 8 101
但是如果我执行我的程序,我会得到以下结果:
0 0 0
这是我的程序:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <libgen.h>
#include <signal.h>
#include <errno.h>
int main(void){
int mypipe[2];
pid_t pid1, pid2;
if (pipe(mypipe)<0)
perror ("pipe error"), exit(1);
if ((pid1=fork())<0)
perror ("fork error"), exit(1);
else if (pid1==0) {
//reader child
close (mypipe[1]);
if (dup2(mypipe[0], STDIN_FILENO)!=STDIN_FILENO)
perror ("dup2 error"), exit(1);
close (mypipe[0]);
if (execlp("wc", "wc", NULL)<0)
perror("execlp1 error"), exit(1);
else { //pid >0, parent
if ((pid2=fork())<0)
perror ("fork error"), exit(2);
else if (pid2==0) {
//writer child
close(mypipe[0]);
if (dup2(mypipe[1], STDOUT_FILENO) != STDOUT_FILENO)
perror("dup2 error"), exit(1);
close (mypipe[1]);
if (execlp("ls", "ls", NULL)<0)
perror ("execlp error"), exit(1);
}
else { //parent
close(mypipe[0]);
close(mypipe[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
exit(0);
}
}
}
return 0;
}
我做错了什么?预先感谢您的回答!
最佳答案
你的代码很困惑。您有许多不相关的 header ,并重复 #include <errno.h>
。在 main()
函数,你有:
int main(void)
{
int mypipe[2];
pid_t pid1, pid2;
if (pipe(mypipe) < 0)
perror("pipe error"), exit(1);
if ((pid1 = fork()) < 0)
perror("fork error"), exit(1);
else if (pid1 == 0)
{
// reader child
close(mypipe[1]);
if (dup2(mypipe[0], STDIN_FILENO) != STDIN_FILENO)
perror("dup2 error"), exit(1);
close(mypipe[0]);
if (execlp("wc", "wc", NULL) < 0)
perror("execlp1 error"), exit(1);
else // pid >0, parent
{
…
}
}
return 0;
}
else if (pid1 == 0)
子句由子句执行。它关闭管道的写入端,将读取端复制到标准输入并关闭管道的读取端。然后它会execlp()
上wc
。仅当代码执行失败时wc
会else
子句被执行,然后只有管道的读取端保持打开状态。同时,原来的进程就会退出。这会关闭管道,因此 wc
命令未收到任何输入,并报告 0 0 0
结果。
您需要重写代码。父进程应该等待,直到它的两个子进程都执行。特别是在调试时,您不应该忽略子进程的退出状态,并且应该报告它。
这是一些有效的代码。请注意,它避免了茂密的决策树——它是 if
的线性序列。/else if
/…/else
代码。一般来说,这比一组复杂的多级条件更容易理解。
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
int fd[2];
pid_t pid1, pid2;
if (pipe(fd) < 0)
perror("pipe error"), exit(1);
else if ((pid1 = fork()) < 0)
perror("fork error"), exit(1);
else if (pid1 == 0)
{
/* ls */
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
execlp("ls", "ls", (char *)0);
perror("exec ls");
exit(1);
}
else if ((pid2 = fork()) < 0)
perror("fork error"), exit(1);
else if (pid2 == 0)
{
/* wc */
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
execlp("wc", "wc", (char *)0);
perror("exec wc");
exit(1);
}
else
{
close(fd[0]);
close(fd[1]);
int status1;
int status2;
int corpse1 = waitpid(pid1, &status1, 0);
int corpse2 = waitpid(pid2, &status2, 0);
printf("ls: pid = %d, corpse = %d, exit status = 0x%.4X\n", pid1, corpse1, status1);
printf("ls: pid = %d, corpse = %d, exit status = 0x%.4X\n", pid2, corpse2, status2);
}
return 0;
}
程序的示例运行pipe41
在我的机器上制作:
$ pipe41
175 175 1954
ls: pid = 44770, corpse = 44770, exit status = 0x0000
ls: pid = 44771, corpse = 44771, exit status = 0x0000
$ ls | wc
175 175 1954
$
关于c - 执行 ls | c 程序中的 wc linux 命令使用 2 个进程通过管道进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70690887/