#include #include #include #include #include #define RD_FD 0 #define WR_FD 1 int main(int argc, char **argv) { int childpid, gone, requestPipefd[2], answerPipefd[2]; if ( pipe(requestPipefd) < 0) { perror("pipe"); exit(1); } /* if */ if ( pipe(answerPipefd) < 0) { perror("pipe"); exit(1); } /* if */ switch ( childpid = fork() ) { case -1: { perror("fork"); exit(1); } case 0: { /* child */ char buf[128]; int nread; /* close WRITE end of answer pipe */ close(answerPipefd[WR_FD]); /* close READ end of request pipe */ close(requestPipefd[RD_FD]); printf("child: requesting '%s'...\n",argv[1]); /* request current time */ write( requestPipefd[WR_FD], argv[1], strlen(argv[1])+1 ); /* read current time from pipe */ nread = read( answerPipefd[RD_FD], buf, sizeof(buf) ); printf("child: result is '%s'\n",buf); break; } /* case 0 */ default: { /* parent */ char buf[128]; int nread; /* close READ end of answer pipe */ close( answerPipefd[RD_FD] ); /* close WRITE end of request pipe */ close( requestPipefd[WR_FD]); /* read from request pipe and copy to stdout */ nread = read( requestPipefd[RD_FD], buf, sizeof(buf) ); printf("parent: received '%s' request\n",buf); if (strcmp(buf,"getTime") == 0) { char buf[10]; unsigned int currentTime; currentTime = time(0); sprintf(buf, "%d", currentTime); write( answerPipefd[WR_FD], buf, sizeof(buf) ); } /* if */ else { write(answerPipefd[WR_FD], "not implemented", sizeof("not implemented") ); } /* else */ printf("parent: waiting for child...\n"); do /* wait for child */ { if ( (gone=wait( (int *) 0 )) < 0 ) { /* no interest for exit_status */ perror("wait"); exit(2); } /* if */ } /* do */ while (gone != childpid); } /* end default */ } /* switch */ exit(0); } /* main */