///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);