【发布时间】:2015-04-14 03:48:39
【问题描述】:
我正在尝试使用 fork 创建三个子进程,其中两个将 char 字符串写入管道,第三个将从 pipr 读取它并将其输出到屏幕上。我们应该创建 4 个文件并调用 exec 系统调用来访问这些文件。但我不知道如何将管道发送到其他进程。由于我尝试在主进程中创建 fd[2] 和 pipe(fd) 在子进程调用 close(fd[0]) 中,但它给了我未声明?我怎么能这样做。 这是我的代码
int main(void){
int fd[2],z,status,i;
pid_t childB, childC, childD;
char *arg[1] = {0};
z = pipe(fd);
if(z <0 ){perror("create pipe"); exit(0);}
childB = fork();
if(childB==0){execv("PipeW1",arg);}
if(childB<0){printf("fork failed\n");exit(0);}
....
在 PipeW1 方法中,我这样做了:
void main (int argc, char *argv[]){
int i;
char str[6];
close(fd[0]);
for(i=1;i<=500;i++){
sprintf(str,"%03daaa",i);
z = write(fd[1],str,6);
if(z<0) {perror("write process B"); exit(1);}
if(i%100==0){usleep(100000);}
}
close(fd[1]);
exit(0);
}
任何建议都会有所帮助!
谢谢
【问题讨论】: