Linux编程练习 --进程间通信2--两个管道实现双向通信

利用两个管道进行进程间双向通信在第一篇练习已经大致作出说明,下面将进行一个更为综合的练习

首先看题目:

设有二元函数f(x,y) = f(x) + f(y)
其中: f(x) = f(x-1) * x (x >1)
f(x)=1 (x=1)
f(y) = f(y-1) + f(y-2) (y> 2)
f(y)=1 (y=1,2)
请编程建立3 个并发协作进程,它们分别完成f(x,y)、f(x)、f(y)

实现的方法很多,这里只用管道实现,大致流程如下:

1.在父进程代码中初始化四个管道,两个用于父-子进程1,另外两个父-子进程2;

2.父进程创建2个子进程,子进程1计算函数f(x),子进程2计算函数f(y);

3.父进程向子进程1、2发送数据;

4.子进程1、2均在管道里读出数据,并进行计算;

5.计算完毕后,子进程1、2向父进程发送结果;

6。父进程接受数据,打印出来

 

下面是实现过程:

[cpp]  view plain copy
  1. /*pipe3.c*/  
  2. #include <unistd.h>  
  3. #include <sys/stat.h>  
  4. #include <sys/types.h>  
  5. #include <stdio.h>  
  6. #include <fcntl.h>  
  7. #define MAXLINE 1024  
  8. #define READ    0  
  9. #define WRITE   1  
  10. /*函数x*/  
  11. int functionx(int nx);  
  12. /*函数y*/  
  13. int functiony(int ny);  
  14. main(void)  
  15. {  
  16.     pid_t pid_x,pid_y;  
  17.     int fdx1[2],fdy1[2],fdx2[2],fdy2[2];  
  18.     /*初始化管道*/  
  19.     pipe(fdx1);  
  20.     pipe(fdy1);  
  21.     pipe(fdx2);  
  22.     pipe(fdy2);  
  23.     /*创建子进程1*/  
  24.     pid_x = fork();  
  25.     if(pid_x < 0)  
  26.     {  
  27.         printf("Create process error!/n");  
  28.         exit(0);  
  29.     }  
  30.     if(pid_x == 0)  
  31.     {  
  32.         int numx,funx;  
  33.         printf("childx process ID:%d/n",getpid());  
  34.         close(fdx1[WRITE]);  
  35.         close(fdx2[READ]);  
  36.         /*从管道读出x*/  
  37.         read(fdx1[READ],&numx,sizeof(int));  
  38.         /*函数计算*/  
  39.         funx = functionx(numx);  
  40.         printf("childx  x=%d/n",funx);  
  41.         /*向管道发送*/  
  42.         write(fdx2[WRITE],&funx,sizeof(int));  
  43.         close(fdx1[READ]);  
  44.         close(fdx2[WRITE]);  
  45.     }  
  46.     if(pid_x > 0)  
  47.     {  
  48.         /*创建子进程2*/  
  49.         pid_y =fork();  
  50.         if(pid_y < 0)  
  51.         {  
  52.             printf("Create process error!/n");  
  53.             exit(0);  
  54.         }  
  55.         if(pid_y == 0)  
  56.         {  
  57.             int numy,funy;  
  58.             printf("childy process ID:%d/n",getpid());  
  59.             close(fdy1[WRITE]);  
  60.             close(fdy2[READ]);  
  61.             /*从管道读出x*/  
  62.             read(fdy1[READ],&numy,sizeof(int));  
  63.             /*函数计算*/  
  64.             funy = functiony(numy);  
  65.             printf("childy  y=%d/n",funy);  
  66.             /*向管道发送*/  
  67.             write(fdy2[WRITE],&funy,sizeof(int));  
  68.         }  
  69.         if(pid_y > 0)  
  70.         {     
  71.             int x,y,funxy;  
  72.             int fx,fy;  
  73.             sleep(1);  
  74.             printf("parentxy process ID:%d/n%",getpid());  
  75.             /*参数输入*/  
  76.             printf("enter x,y/n");  
  77.             scanf("%d,%d",&x,&y);  
  78.             close(fdx2[WRITE]);  
  79.             close(fdx1[READ]);  
  80.             close(fdy2[WRITE]);  
  81.             close(fdy1[READ]);  
  82.             /*管道发送*/  
  83.             write(fdx1[WRITE],&x,sizeof(int));  
  84.             write(fdy1[WRITE],&y,sizeof(int));  
  85.             /*等待子进程计算*/  
  86.             sleep(1);  
  87.             /*管道读入*/  
  88.             read(fdx2[READ],&fx,sizeof(int));  
  89.             read(fdy2[READ],&fy,sizeof(int));  
  90.             funxy = fx+fy;  
  91.             printf("f(x) = %d/nf(y) = %d/nfun(x,y) = %d",fx,fy,funxy);  
  92.             waitpid(pid_x,NULL,0);  
  93.             waitpid(pid_y,NULL,0);  
  94.         }  
  95.     }     
  96. }  
  97. int functionx(int nx)  
  98. {  
  99.     int sum = 1;  
  100.     int i = 1;  
  101.     if(nx < 0)  
  102.     {  
  103.         printf("errorx!/n");  
  104.         exit(0);  
  105.     }  
  106.     while(i <= nx)  
  107.     {  
  108.         sum *=i++;  
  109.     }  
  110.     return sum;  
  111. }  
  112. int functiony(int ny)  
  113. {  
  114.     int f1=1,f2=1,f3;  
  115.     int i =3;  
  116.     if(ny <= 2)  
  117.         return f1;  
  118.     while(i <= ny)  
  119.     {  
  120.         f3 = f1+f2;  
  121.         f1 = f2;  
  122.         f2 = f3;  
  123.         i++;  
  124.     }  
  125.     return f3;  
  126. }  

 

编译:

$ gcc pipe3.c -o pipe3

运行:

$ ./pipe

 

我们输入测试数据x=3,y=4

输出为f(x)=6,f(y)=3,f(x,y)=9

成功

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值