Wednesday, January 19, 2011

IPC using pipes[numbers]-1

//parent sends n no.s child finds prime no.s among them and semds back to parent and parent prints
#include
#include
#include
#include
#include
void main()
{ int buffer[10],temp[10];pid_t childpid;
int pd[2],rpd[2],n,no,j,st,i=0,k=0,num;
printf("\n enter the no of no.s(<=10)");
scanf("%d",&n);

st=pipe(pd);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
st=pipe(rpd);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
childpid=fork();
if (childpid==0)
{ //child
sleep(n*3);
printf("\n child process computes\n ");
close(rpd[0]);
close(pd[1]);
read(pd[0],temp,sizeof(temp));
for(i=0;i { num=temp[i];
for(j=2;j { if ((num%j)==0)
{temp[i]=-2;break;}

}
//printf("%d",temp[i]);
}
write(rpd[1],temp,sizeof(temp));
}
else
{//parent
printf("\n parent process takes and sends nos");
close(rpd[1]);
close(pd[0]);
k=0;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter no. %d ",k+1);
scanf("%d",&buffer[k]);
k++;
}
write(pd[1],buffer,sizeof(buffer));
waitpid(childpid,NULL,0);
printf("\n back to parent");
read(rpd[0],buffer,sizeof(buffer));
k=0;
while(k { if(buffer[k]!=-2)
printf("\n %d\n ",buffer[k]);

k++;
}
}
}//end of main

1 comment:

  1. #include"stdio.h"
    #include"sys/types.h"
    #include"unistd.h"
    #include"string.h"
    #include"stdlib.h"

    ReplyDelete