//David Reing //CSE 660 //gcc -o PipeRW PipeRW.c #include #include #include #include #include #include main() { int pfd[2]; int i; int error = 5; pipe(pfd); //create ProcessB int ProcessB; ProcessB=fork(); if (ProcessB < 0) { /* error*/ printf ("ProcessB failed"); exit(-1);} if (ProcessB == 0) { //only processB should do this code close(pfd[0]); char written [6] = {'0','0','0','a','a','a'}; for ( i = 1; i < 301; i++) { //loop through 300 times if (i < 10) { //there are two leading zeroes that need to be handled written[2] = (char)(48+i); } else if (i < 100) { //there is one leading zero to add in //get the ones digit int ones = i % 10; written[2] = (char)(48+ones); //get the tens digit int tens = i/10; written[1] = (char)(48+tens); } else { //we have no leading zeroes to worry about //get the ones digit int ones = i % 10; written[2] = (char)(48+ones); //shorten the value by 1 digit int temp = i/10; //get the ones digit again ones = temp % 10; //get the tens digit int tens = temp/10; written[1] = (char)(48+ones); written[0] = (char)(48+tens); } /*int j; for (j = 0; j < 6; j++) { //print the 100 characters printf("%c",written[j]); } */ error = write(pfd[1],written, 6); if (error < 0 ) { //error printf("Error Writing Pipe"); exit(1); } if ( (i%50) == 0) { //sleep every 50 iterations sleep(1); } } //exit so that future code is not executed close(pfd[1]); exit(0); } //create ProcessC int ProcessC; ProcessC=fork(); if (ProcessC < 0) { /* error*/ printf ("ProcessB failed"); exit(-1);} if (ProcessC == 0) { //only processC should do this code close(pfd[0]); char alphabet [26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int alphaSpot = 0, timesThrough = 0; char written [3] = {'A', 'x', '0'}; for ( i = 0; i < 260; i++) { //loop through 300 iterations //write alphabet[alphaSpot] + x + timesThrough into the pipe written[0] = alphabet[alphaSpot]; written[2] = (char)(48+timesThrough); //get ready to get the next character alphaSpot++; if (alphaSpot == 26) { //if needed move back to the start of the alphabet alphaSpot = 0; timesThrough ++; } /*int j; for (j = 0; j < 3; j++) { //print the 100 characters printf("%c",written[j]); } */ error = write(pfd[1],written, 3); if (error < 0 ) { //error printf("Error Writing Pipe"); exit(1); } if ( (i%60) == 0) { //sleep every 60 iterations sleep(2); } } //exit so that future code is not executed close(pfd[1]); exit(0); } close(pfd[1]); for ( i = 0; i > -1; i++) { //loop until the pipe is empty char next100 [100]; int empty = read(pfd[0],next100,sizeof(next100)); if (empty < 0 ) { //error printf("Error Reading Pipe"); exit(1); } int j; for (j = 0; j < 100; j++) { //print the 100 characters printf("%c",next100[j]); } if ( (i%30) == 0) { //sleep every 60 iterations sleep(1); } if (empty <= 0) { //check if the pipe is empty i = -5; close(pfd[0]); exit(0); } } }