winner compareHands(uint8_t * const handA, uint8_t * const handB) { uint32_t valA = getHandValue(handA); uint32_t valB = getHandValue(handB); if(valA == valB) return tieBreaker(handA,handB); else return valA > valB ? PLAYER_ONE : PLAYER_TWO; }
void rr(FILE* ifp, FILE* ofp, FILE* rfp, int verbose, int dispRand){ int failsafe = false; int failsafeT = 50; int test = false; int quantumCounter = 0; int allTerminated = false; int t = 0; // Time process* current; process* termCheck; process* root = (process*) malloc(sizeof(process)); qi* rqRoot = (qi*) malloc(sizeof(qi)); rqRoot->next = NULL; rqRoot->last = NULL; rqRoot->rqTail = rqRoot; root = parse(ifp, root); char* origInput = (char*) malloc(sizeof(char*)); // Original Input char* sortedInput = (char*) malloc(sizeof(char) * INPUT_SIZE); // Sorted Input origInput = getInput(root); root = sortLL(root, root->numProcesses, artm); sortedInput = getInput(root); fprintf(ofp,"The original Input was: %s\nThe (sorted) Input is: %s\n\n", origInput, sortedInput); if(verbose == true) fprintf(ofp,"%s\n\n", "This detailed printout gives the state and remaining burst for each process"); while(!allTerminated){ // Sets state and the printInt you need to print setState(root, quantumCounter); // Verbose Section if(verbose == true){ process* printCurrent = root->next; fprintf(ofp, "Before Cycle%5d:", t); while(printCurrent != NULL){ fprintf(ofp, "%11s %d", printCurrent->stateString, printCurrent->printInt); printCurrent = printCurrent->next; } fprintf(ofp, "%s", ".\n"); } // Increments all counters related to waiting processes incrWaiting(rqRoot); // Sets unstarted programs to ready if t == their arrival time rrSetReady(root, t, rqRoot); // Sets blocked processes to ready if their IOBurstTime == 0 // Also increments all counters related to blocked processes blocked(t, root, rqRoot, ofp); // Switch if any in the ready queue have the same arrival time tieBreaker(rqRoot); if(current == NULL || t == 0){ if(test){ fprintf(ofp,"%s\n", "Queue->Running"); } current = dequeue(rqRoot); if(current != NULL){ current->state = RUNNING; if(current->CPUBurstTime == 0){ current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU); } if(test) fprintf(ofp, "%s%d\n", "current == Null or t == NULL, CPUBurstTime: ", current->CPUBurstTime); } } else if(current->state == RUNNING){ current->CPUBurstTime--; current->totalCPUTime--; root->totalTimeRunning++; quantumCounter++; if(current->totalCPUTime == 0){ quantumCounter = 0; if(test == true){ fprintf(ofp,"%s\n", "Running->Terminated"); } current->state = TERMINATED; current->finishingTime = t; current->turnaroundTime = current->finishingTime - current->arrivalTime; current = dequeue(rqRoot); if(current != NULL){ current->state = RUNNING; if(current->CPUBurstTime == 0) current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU); if(test) fprintf(ofp, "%s%d\n", "Terminated, CPUBurstTime: ", current->CPUBurstTime); } } else if(current->CPUBurstTime == 0){ if(test){ fprintf(ofp,"%s\n", "Running->Blocked"); } quantumCounter = 0; current->state = BLOCKED; if(current->IOBurstTime == 0) current->IOBurstTime = randomOS(current->maxIOBurstTime, rfp, ofp, dispRand, IO); current = dequeue(rqRoot); if(current != NULL){ current->state = RUNNING; if(current->CPUBurstTime == 0) current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU); if(test) fprintf(ofp, "%s%d\n", "CPUBurstOut, CPUBurstTime: ", current->CPUBurstTime); } } else if(quantumCounter == quantum){ if(test){ fprintf(ofp,"%s\n", "Running->Preempted (quantum)"); } quantumCounter = 0; current->state = READY; enqueue(t, current,rqRoot); if(test){ fprintf(ofp,"%s\n", "Queue->Running"); } current = dequeue(rqRoot); if(current != NULL){ current->state = RUNNING; if(current->CPUBurstTime <= 0){ current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU); } if(test) fprintf(ofp, "%s%d\n", "quantum hit, CPUBurstTime: ", current->CPUBurstTime); } } } t++; if(failsafe == true && t >= failsafeT){ fprintf(ofp,"%s\n", "FAILSAFE"); break; } // Check if all processes are terminated termCheck = root->next; allTerminated = true; while(termCheck != NULL){ if(termCheck->state != TERMINATED) allTerminated = false; termCheck = termCheck->next; } } fprintf(ofp,"\n%s\n", "The scheduling algorithm used was Round Robin"); // Summary Data int i = 0; process* tempRoot = (process*) malloc(sizeof(process)); tempRoot = parse(ifp,tempRoot); process* tempCurrent = tempRoot->next; current = root->next; while(current != NULL){ fprintf(ofp, "\n%s %d:\n", "Process", i); fprintf(ofp, "\t%s = (%d,%d,%d,%d)\n", "(A,B,C,IO)", tempCurrent->arrivalTime, tempCurrent->maxCPUBurstTime, tempCurrent->totalCPUTime, tempCurrent->maxIOBurstTime); fprintf(ofp, "\t%s: %d\n", "Finishing time", current->finishingTime); fprintf(ofp, "\t%s: %d\n", "Turnaround time", current->turnaroundTime); root->totalTurnaroundTime += current->turnaroundTime; fprintf(ofp, "\t%s: %d\n", "I/O time", current->timeBlocked); fprintf(ofp, "\t%s: %d\n", "Waiting Time", current->waitingTime); // Waiting Time needs to be edited root->totalWaitingTime += current->waitingTime; i++; current = current->next; tempCurrent = tempCurrent->next; } fprintf(ofp, "\n%s:\n", "Summary Data"); fprintf(ofp, "\t%s: %d\n", "Finishing time", t-1); fprintf(ofp, "\t%s: %f\n", "CPU Utilization", (double) root->totalTimeRunning / (double) (t-1)); fprintf(ofp, "\t%s: %f\n", "I/O Utilization", (double) root->totalTimeBlocked / (double) (t-1)); fprintf(ofp, "\t%s: %f processes per hundred cycles\n", "Throughput", ((double) root->numProcesses / (double) (t-1)) * 100); fprintf(ofp, "\t%s: %f\n", "Average turnaround time", (double) root->totalTurnaroundTime / (double) root->numProcesses); fprintf(ofp, "\t%s: %f\n", "Average waiting time", (double) root->totalWaitingTime / (double) root->numProcesses); }