问题描述
有两个进程的代码。还需要添加第三个过程。进程相互传达“你好,世界”。你好,世界 -> (你好,世界) -> {(你好,世界)}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int ps[2];
int pid[2];
if(pipe(ps)!=0) {
printf("pipe error\n");
return 1;
}
if((pid[0]=fork())==0) { //first process
close(ps[0]);
dup2(ps[1],1);
close(ps[1]);
puts("Hello,World");
exit(0);
}
if((pid[1]=fork())==0) { //second process
int i;
char bf[120]="";
close(ps[1]);
dup2(ps[0],0);
close(ps[0]);
for(i=0;(bf[i]=getchar())!=EOF;i++);
bf[i-1]='\0';
printf("from %d: read %d '%i'\n",getpid(),strlen(bf),bf);
exit(0);
}
close(ps[0]);
close(ps[1]);
waitpid(pid[0],NULL,0);
waitpid(pid[1],0);
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)