/* This program illustrates the use of fork and pipes for interprocess communication. It sets up a client-server relationship between processes. The parent process acts as server, the children clients. */ #include #include #include #include #include #include "simpleipc.c" void parentfun(int pipe); void childfun(int pipe); int main() { int i, j; int pfd[2]; // global ipc pipe. pid_t procid; // used to distinguish parent/child after fork i = pipe(pfd); // create pipe if (i!=0) { printf("pipe creation failed\n"); exit(1); } procid = fork(); // create child process if (procid == 0) // child childfun(pfd[1]); else parentfun(pfd[0]); // parent; exit(0); } /* The parent serves fibonacci numbers to child */ void parentfun(int pipe) { int a, b, x; a = 0; b = 1; while(1<2) { printf("server ready for next client request ...\n"); entry(pipe,4,&b); // entry(pipe,4,iptovoidp(&b)); x = b; // compute next fibonacci number to get ready. b = a+b; a = x; for(x=0;x<25000000;x++) {} // simulated delay } } // parentfun void childfun(int pipe) { int i, x; pid_t myid, parentid; myid = getpid(); parentid = getppid(); printf("My process id is %d, and my parent's is %d\n",myid,parentid); for(i=0;i<10;i++) // get first 10 fibs from server { // request(pipe,sizeof(int),iptovoidp(&x)); request(pipe,sizeof(int),&x); printf("I got %d from the parent process\n",x); } close(pipe); printf("process %d exiting.\n",myid); } // childfun