/* * * Function: NonPreemptive * * Purpose: Implements the non preemptive algorithm for dispatching and * then prints the Average Waiting Time (AWS) of the processess * in the list * * Parameters: * input A pointer to the start of the list and the parameter * which it will use to apply the algorithm * * output Prints the average waiting time for the process list * */ void NonPreemptive(GList *processList, enum OPTION param) { int acumulatedTime = 0; float timeSummation = 0; float numberProcess = 0; float average; process *temp; GList *i = g_list_copy(processList); // This section calculates the things for the first member and then deletes the link temp = i->data; acumulatedTime = temp->arrival_time; temp->last_runned = acumulatedTime; acumulatedTime = acumulatedTime + temp->cpu_burst; timeSummation = timeSummation + (temp->last_runned - temp->arrival_time); numberProcess++; i = g_list_delete_link(i, i); i = SortProcessList(i, param); // Organizes the left elements by the parameter required // The rest of the elements are processed for (i = i; i != NULL; i=i->next) { temp = i->data; temp->last_runned = acumulatedTime; acumulatedTime = acumulatedTime + temp->cpu_burst; timeSummation = timeSummation + (temp->last_runned - temp->arrival_time); numberProcess++; } average = timeSummation/numberProcess; g_list_free(i); if (param==1) { printf("\nNon Preemptive cpu_burst\n"); } else if (param==2) { printf("\nNon Preemptive priority\n"); } printf("Average time = %2f\n",average); #ifdef DEBUG printf("%f %f\n",timeSummation,numberProcess); #endif }
int main (int argc, const char * argv[]) { FILE *fp; /* Pointer to the file */ int quantum = 0; /* Quantum value for Round Robin */ int parameters[NUMVAL]; /* Process parameters in the line */ int i; /* Number of parameters in the process */ /* Check if the parameters in the main function are not empty */ if (argc < NUMPARAMS){ printf("Need a file with the process information\n\n"); printf("Abnormal termination\n"); return (EXIT_FAILURE); } else { /* Open the file and check that it exists */ fp = fopen (argv[1],"r"); /* Open file for read operation */ if (!fp) /* The file does not exists */ ErrorMsg("'main'","Filename does not exist or is corrupted\n"); else { /* The first number in the file is the quantum */ quantum = GetInt(fp); if (quantum == EXIT_FAILURE) ErrorMsg("'main'","The quantum was not found"); else { /* Read the process information until the end of file is reached */ while (!feof(fp)){ /* For every four parameters create a new process */ for (i = 0; ((i < NUMVAL) && (!feof(fp))); i++) { parameters[i] = GetInt(fp); } /* Do we have only four parameters? */ if (i == NUMVAL) { /* Create a new process with its information */ CreateProcessList(parameters[0], parameters[1], parameters[2], parameters[3]); } } /* Start by sorting the processes by arrival time */ SortProcessList(ARRIVALTIME); /* Apply all the scheduling algorithms and print the results */ FirstComeFS(); NonPreemptive(CPUBURST); NonPreemptive(PRIORITY); Preemptive(CPUBURST); Preemptive(PRIORITY); RoundRobin(quantum); } } } }
/* * * Function: Preemptive * * Purpose: Implements the preemptive algorithm for dispatching and * then prints the Average Waiting Time (AWS) of the processess * in the list * * Parameters: * input A pointer to the start of the list and the parameter * which it will use to apply the algorithm * * output Prints the average waiting time for the process list * */ void Preemptive(GList *processList, enum OPTION param) { int acumulatedTime = 0; float timeSummation = 0; float numberProcess = 0; float average; GList *arr; // List of arrivals GList *i = NULL; // Running list process *temp, *tempNext; arr = g_list_copy(processList); InitializeList(arr); do{ if (arr!=NULL) { //Is a process spected to arrive? temp = arr->data; if (i==NULL) { acumulatedTime = temp->arrival_time; } i = g_list_append(i,temp); // Add the process and then order the running list if (param == 1) i = SortProcessList(i,3); else i = SortProcessList(i,param); arr = arr->next; // Advance the list of arrivals to the next item } if (arr!=NULL) { // Is a process spected to arrive next? temp = i->data; // For modify current item tempNext = arr->data; // To check when does the new process arrives if (temp->timeleft > 0){ // Is this process finished? if (temp->timeleft > (tempNext->arrival_time - acumulatedTime )) { // Will the actual process finish before the next process arrives? temp->timeleft =temp->timeleft - (tempNext->arrival_time - acumulatedTime); // If not finish before the new arrival, reduce the time still have to run acumulatedTime = acumulatedTime + (tempNext->arrival_time - acumulatedTime); // Aument the acumulated time i = g_list_append(i,temp); //append the process to be runned again } else { // If finishes before the new arrival temp->last_runned = acumulatedTime; // Last time the process runned timeSummation = timeSummation + (temp->last_runned - temp->arrival_time - (temp->cpu_burst - temp->timeleft)); //Add to the counter of the formula #ifdef DEBUG PrintProcess(temp, acumulatedTime); #endif acumulatedTime = acumulatedTime + temp->timeleft; // Aument the acumulated time temp->timeleft = 0; // Finish the process numberProcess++; // Aument the number of processess } } } else{ // No more arrivals are expected temp = i->data; // To transform the current item if (temp->timeleft > 0){ // Is this process finished? temp->last_runned = acumulatedTime; // Last time the process runned timeSummation = timeSummation + (temp->last_runned - temp->arrival_time - (temp->cpu_burst - temp->timeleft)); #ifdef DEBUG PrintProcess(temp, acumulatedTime); #endif acumulatedTime = acumulatedTime + temp->timeleft; //Aument the acumulated time temp->timeleft = 0; //Finish the process numberProcess++; // Aument the numer of processess } } i = i->next; // Go to the next process in the list }while(i!=NULL || arr!= NULL); // Do it until the list is finished average = timeSummation/numberProcess; // Calculate the average g_list_free(arr); // Destroy the copy of the original list g_list_free(i); if (param==1) { printf("\nPreemptive cpu_burst\n"); } else if (param==2) { printf("\nPreemptive priority\n"); } printf("Average time = %2f\n",average); #ifdef DEBUG printf("%f %f\n",timeSummation,numberProcess); #endif }