[英]Executing the ls | wc linux command in c program using 2 processes communicating trough a pipe
我目前在以下练习中遇到问题:
我想“模仿” pipe 命令行ls | wc
ls | wc
在 linux bash 中使用以下程序。 我要做的是:
ls
程序,将 output 通过 pipe 传递给在该 Z78E6221F6393D1356681DB398F14CED6 上执行wc
程序的读取器当我做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;
}
我究竟做错了什么? 提前感谢您的回答!
你的代码很混乱。 您有许多不相关的标题,并重复#include <errno.h>
。 在main()
function 中,您有:
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)
子句由子执行。 它关闭 pipe 的写入端,将读取端复制到标准输入并关闭 pipe 的读取端。 然后它在wc
上执行execlp()
。 只有当代码执行wc
失败时才会执行else
子句,然后只有 pipe 的读取端保持打开状态。 同时,原始进程简单地退出。 这将关闭 pipe,因此wc
命令没有输入,并报告0 0 0
作为结果。
你需要重写代码。 父进程应该等到它的两个子进程都执行。 尤其是在调试的时候,千万不要忽略children的退出状态,要上报。
这是一些有效的代码。 请注意,它避免了浓密的决策树——它是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
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.