Join With Me...

3:08 AM

Sum of N numbers program in C using Fork and Pipe IPC

main()
{
int a[2],i,b[2],c[2],s[]={1,2,3,4,5,6,7,8,9,10},temp=0;
int p,res=0,res1=0,n;
printf("Enter The Total Elements\n");
scanf("%d",&n);
printf("Enter All Elements One By One\n");
for(i=0;i
scanf("%d",(s+i));
//printf("%s",line);
pipe(a);
pipe(b);
p=fork();
if(n%2==1)
{
n++;
*(s+i)=0;
}
for(i=0;i
{
if(p>0)
{
//printf("NO IS %d  ",i);
write(a[1],(s+i),1);
read(b[0],&temp,1);
res=res+temp;
}
else
{
//printf("NO IN @ IS:  %d ",i);
read(a[0],&res1,1);
res=*(s+i+1)+res1;
write(b[1],&res,1);
//printf("TEMP VAL :%d  ",res);
}
//system("sleep 1");
}
printf("RES :%d",res);
exit(1);
}


0 comments:

Post a Comment