#include<dirent.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int canal_son[2];
int canal_father[2];
pipe(canal_father);
pipe(canal_son);
char mesaj_son[20];
char mesaj_father[20];
if (fork()==0)
{
printf("Son %d\n",getpid());
read(canal_father[0],mesaj_father,4);
int j;
for(j=0;j<5;j++)
{
printf("The message from father is: %s \n",mesaj_father);
read(canal_father[0],mesaj_father,4);
write(canal_son[1],"son",3);
}
exit(0);
}
int i=5;
for (i=0;i<5;i++)
{
write(canal_father[1],"mesas",4);
read(canal_son[0],mesaj_son,10);
printf("we are in father:%s\n",mesaj_son);
}
}
我要做的是创建用于在孩子和父亲之间进行通信的管道,唯一的问题是当我尝试同时使用两者时,我的进程会冻结,就像它期待一些输入,如果我只使用一个管道,canal_father 或 canal_son一切都很完美,有没有人知道使用 2 个管道有什么问题?
非常感谢。