#include #include const int NumOfNodes = 4; char *msg = "hello"; int MsgSize = 5; main(char * argv[],int argc) { void client(int,int,int); int pid,left[2], right[2]; int i; int order; int n; char *newMsg; if (pipe(left) < 0 || pipe(right) < 0) { fprintf(stderr," pipe failed \n"); exit(-1); } printf("master receiving pipe id %d\n",left[0]); order = 0; if ((pid = fork()) < 0) { fprintf(stderr," fork failed \n"); exit(-1); } else if (pid > 0) /* parent process */ { close(left[1]); close(right[0]); for (i = 0; i < NumOfNodes; i ++) { /* push data into the pipe */ if (write(right[1],msg,MsgSize) != MsgSize) fprintf(stderr,"write error in main\n"); printf("msg wrote %d %s\n",i, msg); } while (wait((int *)0) != pid); newMsg = (char *)malloc(50); for (i = 0; i < NumOfNodes; i ++) { n = 0; newMsg[n] = 0; if ((n = read(left[0],newMsg,MsgSize)) != MsgSize) fprintf(stderr,"read error in main %d\n",n); newMsg[n] = 0; printf("msg read back %d %s\n",i,newMsg); } if ((n = read(left[0],newMsg,50)) <= 0) fprintf(stderr,"last read error in main %d\n",n); newMsg[n] = 0; printf("msg read back last : %s\n",newMsg); close(left[0]); close(right[1]); exit(0); } else /* child process, pid == 0 */ { client(left[1],right[0],order); /* passing the read fd */ /* The first one (left[1]) is used to write back to the master by the */ /* very last node on the pipe line. All intermediate nodes should use */ /* right[1] to pass information to its child */ close(left[1]); close(right[0]); exit(0); } } void client(int toMaster, int fromLeft, int level) { int pid, right[2]; char *item; int i; int n; printf("at level %d with toMaster %d\n",level,toMaster); item = (char *)malloc(30); if (level < NumOfNodes) /* keep expanding */ { if (pipe(right) < 0) { fprintf(stderr," pipe failed \n"); exit(-1); } level ++; if ((pid = fork()) < 0) { fprintf(stderr," fork failed in client\n"); exit(-1); } else if (pid > 0) /* parent process */ { close(right[0]); printf("before read at level %d \n",level); for (i = 0; i < NumOfNodes; i ++) { n = 0; if ((n = read(fromLeft,item,MsgSize)) != MsgSize) fprintf(stderr,"read error in client %d\n",level); printf("read at level %d i %d == %s\n",level, i, item); if (write(right[1],item,MsgSize) != MsgSize) /* push data into the pipe */ fprintf(stderr,"write error in client %d\n",level); } while (wait((int *)0) != pid); close(right[1]); exit(0); } else /* child process, pid == 0 */ { client(toMaster,right[0],level); close(right[1]); exit(0); } } /* if level < NumOfNodes */ else /* level == NumOfNodes, last one on the pipe, write back to master */ { printf("action in final client\n"); for (i = 0; i < NumOfNodes; i ++) { if ((n = read(fromLeft,item,MsgSize)) != MsgSize) fprintf(stderr,"read error in last %d\n",level); printf("last read at i %d == %s\n",i,item); if (write(toMaster,item,MsgSize) != MsgSize) /* push data into the pipe */ fprintf(stderr,"write error in last %d\n",level); } item[n] = 0; strcat(item,"greeting from last"); printf("last msg in client : %s\n",item); n = strlen(item); printf("strlen %d\n",n); if (write(toMaster,item,n) <= 0) fprintf(stderr,"final write error\n"); exit(0); } }