1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<limits.h>
#include<math.h>
main(){
int p1[2],p2[2],p3[2],p4[2];
int i,j,s=0;
int X,Y,Z,a,b,c;
typedef struct
{ int x ,y ,z;
}vect;
pipe(p1);pipe(p2);pipe(p3);pipe(p4);
vect *vecteur=malloc(2*sizeof(vect));
if(fork()!=0)
if(fork()!=0)
if(fork()!=0)
{printf("p1=%d %d\n",getpid(),getppid());
for(i=0;i<2;i++)
{
printf("x= ");
scanf("%d",&(vecteur[i].x));
printf("\ny= ");
scanf("%d",&(vecteur[i].y));
printf("\nz= ");
scanf("%d",&(vecteur[i].z));
}
write(p1[1],vecteur,2*sizeof(vect));
close(p1[1]);
wait();
a=read(p2[0],&X,sizeof(int));
//printf("a=%d\n",a);
b=read(p3[0],&Y,sizeof(int));
c=read(p4[0],&Z,sizeof(int));
s=a+b+c;
printf("\ns=%d",s);
}
else
{sleep(5);
//printf("f1=%d %d\n",getpid(),getppid());
i=0;close(p1[1]);
read(p1[0],vecteur,2*sizeof(vect));
X=(vecteur[i].x) * (vecteur[i+1].x);
//printf("X=%d\n",X);
write(p2[1],&X,sizeof(int));close(p1[0]);
}
else
{ sleep(5);close(p1[1]);
//printf("f2=%d %d\n",getpid(),getppid());
i=0;
read(p1[0],vecteur,2*sizeof(vect));
Y=(vecteur[i].y) * (vecteur[i+1].y);
//printf("Y=%d\n",Y);
write(p3[1],&Y,sizeof(int)); close(p1[0]);
}
else
{ sleep(5);close(p1[1]);
//printf("f3=%d %d\n",getpid(),getppid());
i=0;
read(p1[0],vecteur,2*sizeof(vecteur));
Z=(vecteur[i].z) * (vecteur[i+1].z);
//printf("Z=%d\n",Z);
write(p4[1],&Z,sizeof(int));close(p1[0]);
}
} |