#define READY 0 #define RUNNING 1 #define WAITING 2 #define DONE 3 void rr(char *s1, char *s2, int quantum, int x1, int y1, int z1, int x2, int y2, int z2) { int i; int state1; int state2; int cpuLeft1; int cpuLeft2; int ioLeft1; int ioLeft2; int qleft; state1 = READY; state2 = READY; cpuLeft1 = x1; cpuLeft2 = x2; ioLeft1 = y1; ioLeft2 = y2; i = 0; qleft = quantum; while (1) { /* change state here if necessary and check if done */ if ( (state1 == DONE) && (state2 == DONE) ) { break; } if ((state1 == RUNNING) && (cpuLeft1== 0)) { if (ioLeft1 == 0) { state1 = DONE; s1[i] = 0; } else state1 = WAITING; } else if ((state2 == RUNNING) && (cpuLeft2 == 0) ) { if (ioLeft2 == 0) { state2 = DONE; s2[i] = 0; } else state2 = WAITING; } if ((state1 == RUNNING) && (qleft == 0) ) { state1 = READY; if (state2 == READY) { state2 = RUNNING; qleft = quantum; } } if ((state2 == RUNNING) && (qleft == 0) ) { state2 = READY; if (state1 == READY) { state1 = RUNNING; qleft = quantum; } } if ((state1 == WAITING) && (ioLeft1 == 0)) { state1 = READY; cpuLeft1 = z1; } if ((state2 == WAITING) && (ioLeft2 == 0)) { state2 = READY; cpuLeft2 = z2; } if ( (state1 == READY) && (state2 == READY)) { state1 = RUNNING; qleft = quantum; } else if ( (state1 == READY) && (state2 != RUNNING)) { state1 = RUNNING; qleft = quantum; } else if ( (state2 == READY) && (state1 != RUNNING)) { state2 = RUNNING; qleft = quantum; } /* put somthing in based on the state */ if (state1 == RUNNING) { s1[i] = 'R'; cpuLeft1--; qleft--; } if (state1 == WAITING) { s1[i] = 'w'; ioLeft1--; } if (state1 == READY) { s1[i] = 'r'; } if (state2 == RUNNING) { s2[i] = 'R'; cpuLeft2--; qleft--; } if (state2 == WAITING) { s2[i] = 'w'; ioLeft2--; } if (state2 == READY) { s2[i] = 'r'; } i++; } } void fcfs(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2) { rr(s1,s2,x1+z1+x2+z2,x1,y1,z1,x2,y2,z2); }