我尝试使用pipe()
和fork()
实现此目的:
ls | WC
首先我检查了管道是否工作正常,这是代码。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
int main(void){
char *param1[]={"ls",NULL};
char *param2[]={"wc",NULL};
int p[2];
pipe(p);
pid_t process1, process2;
if((process1=fork())==0){ // child 1
dup2(p[1],1); // redirect stdout to pipe
close(p[0]);
execvp("ls", param1);
perror("execvp ls failed");
}
else if(process1>0){ // parent
wait(NULL);
}
if((process2=fork())==0){ // child 2
dup2(p[0],0); // get stdin from pipe
close(p[1]);
char buff[1000]={0};
read(STDIN_FILENO,buff,250);
printf("---- in process2 -----\n%s\n",buff);
}
else if(process2>0){ // parent
wait(NULL);
}
return 0;
}
我检查它工作正常。我将read()
和printf()
替换为exec()
,例如:
if((process2=fork())==0){ // child 2
dup2(p[0],0); // get stdin from pipe
close(p[1]);
execvp("wc",param2);
perror("execvp wc failed");
}
我的终端刚挂了。我认为wc
进程无法获得任何输入。所以我的问题是,为什么read()
和printf()
有效,execvp()
不起作用?
答案 0 :(得分:1)
您不必每次创建新进程时都等待,也要关闭父进程中的描述符,例如:
if((process1=fork())==0){ // child 1
dup2(p[1],1); // redirect stdout to pipe
close(p[0]);
execvp("ls", param1);
perror("execvp ls failed");
}
else if(process1==-1){ // fork failed
exit(1);
}
close(p[1]); // no need for writing in the parent
if((process2=fork())==0){ // child 2
dup2(p[0],0); // get stdin from pipe
char buff[1000]={0};
read(STDIN_FILENO,buff,250);
printf("---- in process2 -----\n%s\n",buff);
}
else if(process2==-1){ // second fork failed
close(p[0]); // ensure there is no reader to the pipe
wait(NULL); // wait for first chidren
exit(1);
}
close(p[0]); // no need for reading in the parent
wait(NULL);
wait(NULL); // wait for the two children