创建 2 child 个进程
Create 2 child processes
我必须在 3 个进程之间创建这种通信:
1 个进程(parent):
- 将偶数发送到 child 1;
- 发送奇数到child2;
- 获取从 child 1 和 child 2
发送的号码
2 进程(child 1):
- 从 parent 得到偶数;
- 将偶数发送到 child 2;
- 发送 2*value_of_number 到 parent
3 进程(child 2):
- 从parent得到奇数;
- 发送 2*value_of_number 到 parent
这是我的代码:
int main()
{
int p12[2],p13[2],p23[2];
int p21[2],p31[2];
if(pipe(p12)<0){
perror("pipe 12 error\n");
exit(1);
}
if(pipe(p13)<0){
perror("pipe 13 error\n");
exit(1);
}
if(pipe(p23)<0){
perror("pipe 23 error\n");
exit(1);
}
if(pipe(p21)<0){
perror("pipe 21 error\n");
exit(1);
}
if(pipe(p31)<0){
perror("pipe 31 error\n");
exit(1);
}
switch(fork()){
case -1:{
perror("fork 1 error\n");
exit(1);
}
case 0:{//1 child
close(p12[1]);
close(p13[1]);
close(p13[0]);
close(p23[0]);
close(p21[0]);
close(p31[1]);
close(p31[0]);
int paros;
int ket;
while(read(p12[0],&paros,sizeof(int))>0){
cout<<"2: "<<paros<<endl;
if(write(p23[1],&paros,sizeof(int))==-1){
perror("write 23 error\n");
exit(1);
}
ket=2*paros;
if(write(p21[1],&ket,sizeof(int))==-1){
perror("write 21 error\n");
exit(1);
}
}
close(p21[1]);
close(p12[0]);
close(p23[1]);
exit(0);
}
default:{
switch(fork()){
case -1:{
perror("fork 2 error\n");
exit(1);
}
case 0:{//2 child
close(p13[1]);
close(p12[1]);
close(p12[0]);
close(p23[1]);
close(p31[0]);
close(p21[1]);
close(p21[0]);
int szamok;
int ket;
while(read(p13[0],&szamok,sizeof(int))>0){
cout<<"3: "<<szamok<<endl;
ket=2*szamok;
if(write(p31[1],&ket,sizeof(int))==-1){
perror("write 31 error\n");
exit(1);
}
}
while(read(p23[0],&szamok,sizeof(int))>0){
cout<<"3: "<<szamok<<endl;
}
close(p31[1]);
close(p13[0]);
close(p23[0]);
exit(0);
}
default:{
close(p12[0]);
close(p13[0]);
close(p23[0]);
close(p23[1]);
close(p21[1]);
close(p31[1]);
}
}
}
}
int i=1;
while(i<=10){
if(i%2==0){
if(write(p12[1],&i,sizeof(int))==-1){
perror("write 12 error\n");
exit(1);
}
}
else{
if(write(p13[1],&i,sizeof(int))==-1){
perror("write 13 error\n");
exit(1);
}
}
i++;
}
int szam;
while(read(p21[0],&szam,sizeof(int))>0){
cout<<"1: "<<szam<<endl;
}
while(read(p31[0],&szam,sizeof(int))>0){
cout<<"1: "<<szam<<endl;
}
close(p12[1]);
close(p13[1]);
close(p31[0]);
close(p21[0]);
while(wait(NULL)>0){};
exit(0);
}
但由于某些原因它不起作用...
您的 parent 将 5 个数字写入每个管道,然后等待 child 1 的所有响应,然后再继续 child 2。将数字写入 children,你没有关闭那些管道,所以那些 children 不知道 parent 已经停止。在 child 1 的情况下,它仍在等待从 parent 接收更多号码。因此,它永远不会关闭自己的管道并退出。因此,parent 卡在它的第一个读取循环中,从 child 1 开始,永远不会到达 child 二。
写入p12[1]和p13[1]后,加上
close(p12[1]);
close(p13[1]);
并且该过程将 运行 完成
我必须在 3 个进程之间创建这种通信:
1 个进程(parent):
- 将偶数发送到 child 1;
- 发送奇数到child2;
- 获取从 child 1 和 child 2 发送的号码
2 进程(child 1):
- 从 parent 得到偶数;
- 将偶数发送到 child 2;
- 发送 2*value_of_number 到 parent
3 进程(child 2):
- 从parent得到奇数;
- 发送 2*value_of_number 到 parent
这是我的代码:
int main()
{
int p12[2],p13[2],p23[2];
int p21[2],p31[2];
if(pipe(p12)<0){
perror("pipe 12 error\n");
exit(1);
}
if(pipe(p13)<0){
perror("pipe 13 error\n");
exit(1);
}
if(pipe(p23)<0){
perror("pipe 23 error\n");
exit(1);
}
if(pipe(p21)<0){
perror("pipe 21 error\n");
exit(1);
}
if(pipe(p31)<0){
perror("pipe 31 error\n");
exit(1);
}
switch(fork()){
case -1:{
perror("fork 1 error\n");
exit(1);
}
case 0:{//1 child
close(p12[1]);
close(p13[1]);
close(p13[0]);
close(p23[0]);
close(p21[0]);
close(p31[1]);
close(p31[0]);
int paros;
int ket;
while(read(p12[0],&paros,sizeof(int))>0){
cout<<"2: "<<paros<<endl;
if(write(p23[1],&paros,sizeof(int))==-1){
perror("write 23 error\n");
exit(1);
}
ket=2*paros;
if(write(p21[1],&ket,sizeof(int))==-1){
perror("write 21 error\n");
exit(1);
}
}
close(p21[1]);
close(p12[0]);
close(p23[1]);
exit(0);
}
default:{
switch(fork()){
case -1:{
perror("fork 2 error\n");
exit(1);
}
case 0:{//2 child
close(p13[1]);
close(p12[1]);
close(p12[0]);
close(p23[1]);
close(p31[0]);
close(p21[1]);
close(p21[0]);
int szamok;
int ket;
while(read(p13[0],&szamok,sizeof(int))>0){
cout<<"3: "<<szamok<<endl;
ket=2*szamok;
if(write(p31[1],&ket,sizeof(int))==-1){
perror("write 31 error\n");
exit(1);
}
}
while(read(p23[0],&szamok,sizeof(int))>0){
cout<<"3: "<<szamok<<endl;
}
close(p31[1]);
close(p13[0]);
close(p23[0]);
exit(0);
}
default:{
close(p12[0]);
close(p13[0]);
close(p23[0]);
close(p23[1]);
close(p21[1]);
close(p31[1]);
}
}
}
}
int i=1;
while(i<=10){
if(i%2==0){
if(write(p12[1],&i,sizeof(int))==-1){
perror("write 12 error\n");
exit(1);
}
}
else{
if(write(p13[1],&i,sizeof(int))==-1){
perror("write 13 error\n");
exit(1);
}
}
i++;
}
int szam;
while(read(p21[0],&szam,sizeof(int))>0){
cout<<"1: "<<szam<<endl;
}
while(read(p31[0],&szam,sizeof(int))>0){
cout<<"1: "<<szam<<endl;
}
close(p12[1]);
close(p13[1]);
close(p31[0]);
close(p21[0]);
while(wait(NULL)>0){};
exit(0);
}
但由于某些原因它不起作用...
您的 parent 将 5 个数字写入每个管道,然后等待 child 1 的所有响应,然后再继续 child 2。将数字写入 children,你没有关闭那些管道,所以那些 children 不知道 parent 已经停止。在 child 1 的情况下,它仍在等待从 parent 接收更多号码。因此,它永远不会关闭自己的管道并退出。因此,parent 卡在它的第一个读取循环中,从 child 1 开始,永远不会到达 child 二。
写入p12[1]和p13[1]后,加上
close(p12[1]);
close(p13[1]);
并且该过程将 运行 完成