/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/*HELLO I M SERVER*/
///PIPE COULD BE EASILY IMPLEMENTED BY CREATING A NEW PROCESS....FIFO COULD BE EASILY IMLEMENTING BY CREATING A NEW THREADS.......
///no problem like in fifo .......that writer have to write untill reader is reading
//How fork work with diffrent process.....created by fork,,,,,,,?
//its property of fork that where executing program counter find sleep or wait or block in one process.....it leaves that process & continues with other process....& also continues ...to process created by it.....when it completes all those process ......then it will come back to that process process that was left in end......
//suppose there are 5 processes....i provide sleep(1) in 1st parent process...then it continues with..2nd process ...then 3rd process....suppose there is again sleep() in 4th process...then it will leave 4th process ........continues to 5th process....then 6th process....after completion of 6th process...it will come back to 4th process to finish....after this 4th process .......again it will come back to 1st process to complete at last..........
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main()
{
int res=0,i,count=1;
int ret,fd1[2],fd2[2],fd3[3],arr1[2],arr2[2],arr3[2];
char buff[4],set[4],rd[4],wr[4];
ret=pipe(fd1);//descriptors to read write for three pipes will be diffrent
if(ret==0)
printf("client1 pipe created\n");
ret=pipe(fd2);
if(ret==0)
printf("client2 pipe created\n");//3 pipes are created for clients ..each client will will write data into its own pipe..& then again read result from same pipe/..
ret=pipe(fd3);
if(ret==0)
printf("client3 pipe created\n");
ret=pipe(arr1);
if(ret==0)
printf("process1 pipe is created\n");
ret=pipe(arr2);
if(ret==0)
printf("process2 pipe is created\n");//each process will read data from its own pipe...& after processing that data..put result into same pipe../
ret=pipe(arr3);
if(ret==0)
printf("process3 pipe is created\n");
ret=fork();//fork is called will duplicates the process ..1st parent starts executtion..but due to read command in parent 1st of all...will block parent to operate..bcz there nothing in pipe to read
// ******ONE MORE DIFFRENCE BETWEEN FIFO & PIPE************WHEN FIFO IS NOT USED WITH NON_BLOCK****?
//THERE IS A VERY NICE THING WITH PIPE THAT ..PIPE WILL BLOCK FOR READ WHEN THERE IS NOTHING IN PIPE..NOT BLOCK FOR WRITE...UNLIKE FIFO...WRITER SHOULD NOT BE THERE NECESSARLLY...WHILE READING..WRITER SHOULD HAVE WRITTEN SOMETHING BUT WRITER SHOULD NOT BE THERE NECCESSARILY..........
if(ret==0)//
{
printf("**in child process c1**\n");
sprintf(rd,"%d",fd1[0]);//CONVERT INT TYPE DESCRIPTORS TO CHAR TO SEND IT AS ARGUMENT IN OTHER PROCESS......
sprintf(wr,"%d",fd1[1]);
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./client1","client1",rd,wr,NULL);//in client we write the data to be operated..& make it wait ...to read result in end ..once in sleep(1) 1sec sleep..it leaves client process..will come back to same client processes after finishing all other processes
printf("ret=%d in execl\n",ret);
printf("done\n");
}
else
{
//sleep(1);//if i did not make wait to parent..then parent will operate..but block at its read..so scheduler is provided to child process ....in child process ..when operator & operand is written to pipe..then it will come back in parent process to read
printf("in parent\n");
count=read(fd1[0],buff,4);
printf("count frm c1 to server=%d bytes=%s\n",count,buff);
count=write(arr1[1],buff,4);
printf("write count frm server to p1=%d\n",count);
ret=fork();
if(ret==0)
{
printf("**in child process c2**\n");
sprintf(rd,"%d",fd2[0]);
sprintf(wr,"%d",fd2[1]);//AGAIN INT TYPES ARE CONVERD INTO CHAR
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./client2","client2",rd,wr,NULL);//execl WILL REPLACE THE PROCESS..1st arg is path..2nd is name of compiled file ....then list of any no. of arguments that should be of char type..
printf("ret=%d in execl\n",ret);//bcz...execl will replace pcb ..of this child process..so these statements will not be printed......
printf("done\n");
}//this child has to end here
else
{
printf("in parent\n");
count=read(fd2[0],buff,4);//when 1st client has written its data into pipe..that data could be read by this int type lowest possible integer...for i=1
printf("count frm c2 to server=%d bytes=%s\n",count,buff);
count=write(arr2[1],buff,4);//in the same time ..data is written to processing pipe..so that new client should be read in same buffer ..in same space....
printf("write count frm server to p2=%d\n",count);
ret=fork();//fork again in parent process.....to create new child to replace client3 process
if(ret==0)
{
printf("**in child process c3**\n");
sprintf(rd,"%d",fd3[0]);
sprintf(wr,"%d",fd3[1]);
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./client3","client3",rd,wr,NULL);
printf("ret=%d in execl\n",ret);
printf("done\n");
}
else//this else if not closed here to continue only parent process....it will closed at end....
{
printf("in parent\n");
count=read(fd3[0],buff,4);
printf("count frm c3 to server=%d bytes=%s\n",count,buff);
count=write(arr3[1],buff,4);//read data from diffrent fifo to write it into diffrent fifo
printf("write count frm server to p3=%d\n",count);
if(res==0)//again fork to replace the process1 in created child process
{
printf("**in child of fork to replace pro1**\n");
sprintf(rd,"%d",arr1[0]);
sprintf(wr,"%d",arr1[1]);
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./process1","process1",rd,wr,NULL);//process is replaced here...so will read data..write result back to it
printf("ret=%d in execl\n",ret);
}
else
{
sleep(1);//have to make sleep here to make parent wait....to execute child 1st....or i should have to apply semaphore ..i will do in next project using pipe & semaphore..for synchronisation....
count=read(arr1[0],&res,4);
printf("count frm p1 to server=%d res in ser=%d\n",count,res);
count=write(fd1[1],&res,4);
printf("count wriiten frm server to c1=%d\n",count);
res=fork();
if(res==0)
{
printf("**in child of fork to replace pro2**\n");
sprintf(rd,"%d",arr2[0]);
sprintf(wr,"%d",arr2[1]);
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./process2","process2",rd,wr,NULL);
printf("ret=%d in execl\n",ret);
}
else
{
sleep(1);//have to make sleep here to make parent wait....to execute child 1st....or i should have to apply semaphore ..i will do in next project using pipe & semaphore..for synchronisation....
count=read(arr2[0],&res,4);
printf("count frm server to p2=%d res in ser=%d\n",count,res);
count=write(fd2[1],&res,4);
printf("count wriiten frm server to c2=%d\n",count);
res=fork();
if(res==0)
{
printf("**in child of fork to replace pro3**\n");
sprintf(rd,"%d",arr3[0]);
sprintf(wr,"%d",arr3[1]);
printf("wr=%s rd=%s\n",wr,rd);
ret=execl("./process3","process3",rd,wr,NULL);
printf("ret=%d in execl\n",ret);
}
else
{
sleep(1);//have to make sleep here to make parent wait....to execute child 1st....or i should have to apply semaphore ..i will do in next project using pipe & semaphore..for synchronisation....
count=read(arr3[0],&res,4);
printf("count frm p3 to server=%d res in ser=%d\n",count,res);
count=write(fd3[1],&res,4);
printf("count wriiten server to c3=%d\n",count);
}
}//all brackets of else are closed here bcz...every parent should be end at this point...finally return 0 to shell.....so that no child operate as orphan child.......
/* HI I M PROCESS1 */ /*PROCESSING PROGRAM IN WHICH PROCESSING OF DATA SEND BY CLIENT TAKES PLACE*/ #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<unistd.h> #include<string.h> int a=0,b=0; int main(int argc,char* argv[])//arguments are read here...in any file if we have read write descriptors in that file.....means we have keys of both ends of that pipe.....so read & write anything to that pipe { printf("##IN PROCESS1##\n"); int fd,ct,wfd,count,res; char buff[4]; sscanf(argv[1],"%d",&fd); sscanf(argv[2],"%d",&wfd);//before use of descriptors..1st convert those descriptors into int type printf("fd=%d wfd=%d\n",fd,wfd); ct=read(fd,buff,4);//read data into process printf("ct p1=%d\n",ct); printf("p1 =%s\n",buff); //buff[3]='/'; b=atoi(buff+1);//atoi(buff) will takes a pointer always as argument & convert like..all data in that buff into int...like character 4 & 3 into int 43,,,& place into int..& we know that int takes a block of combined memory,,so 4 & 3 either of int type but placed in same block of int ..so cannot be seperated ..//in that case..first 2nd byte in that buff is converted into int..i.e. 3. buff[1]='\0';//then make 2nd byte null a=atoi(buff);//then 1st byte into int i.e. 4///////but here is problem..that if client want to send to letters in combined form..that is 11+4...then sepration of 11 by using this same logic ,,is not possible.......so a very small modification of code is required..i.e.count lengh of characters like this..i will do manipulation in next implimentation........ printf("a=%d b=%d\n",a,b); res=a+b;//both ints are added...if we try to add char values..then ascii values of those chars will be added printf("res=%d\n",res);//result will be printed into process count=write(wfd,&res,4);//written back to server printf("count back p1=%d\n",count); return 0; }