//Catch system signal BOOL WINAPI SignalHandler( const DWORD SignalType) { //Print to screen. switch (SignalType) { //Handle the CTRL-C signal. case CTRL_C_EVENT: { fwprintf_s(stderr, L"[Notice] Get Control-C.\n"); PrintProcess(true, true); }break; //Handle the CTRL-Break signal. case CTRL_BREAK_EVENT: { fwprintf_s(stderr, L"[Notice] Get Control-Break.\n"); PrintProcess(true, true); //Exit process. return TRUE; }break; //Handle other signals. default: { fwprintf_s(stderr, L"[Notice] Get closing signal.\n"); PrintProcess(true, true); } } //Close file handle. if (ConfigurationParameter.OutputFile != nullptr) { fclose(ConfigurationParameter.OutputFile); ConfigurationParameter.OutputFile = nullptr; } //Close all file handles and WinSock cleanup. #if defined(PLATFORM_WIN) if (ConfigurationParameter.IsInitialized_WinSock) { WSACleanup(); ConfigurationParameter.IsInitialized_WinSock = false; } _fcloseall(); #elif (defined(PLATFORM_FREEBSD) || (defined(PLATFORM_LINUX) && !defined(PLATFORM_OPENWRT))) fcloseall(); #endif //Exit process. // exit(EXIT_SUCCESS); return FALSE; }
/* * * Function: RoundRobin * * Purpose: Implements the Round Robin algorithm for dipatching 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 quantum of * which is intented to use * * output Prints the average waiting time for the process list * */ void RoundRobin(GList *processList, int quantum){ int acumulatedTime = 0; float timeSummation = 0; float numberProcess = 0; float average; GList *list = g_list_copy(processList),*i,*j; InitializeList(list); process *temp = list->data, *tempNext; acumulatedTime = temp->arrival_time; int reins; for (i= list; i != NULL; i=i->next) { temp = i->data; if (temp->timeleft > 0){ // Is the process finished? if (temp->timeleft > quantum) { // Is the process going to finish before the quantum expires? reins =0; temp->timeleft = temp->timeleft - quantum; acumulatedTime = acumulatedTime + quantum; // This section checks where the process should be reinserted (In case not all the process have arrived) for (j=i; j !=NULL; j = j->next) { tempNext= j->data; if ((tempNext->arrival_time > acumulatedTime)&& (reins==0) ) { i = g_list_insert_before(i,j->prev,temp); reins++; break; } } if (reins==0) { i = g_list_append(i,temp); } // End of section } else { // Calculates the operations to obtain the data for the formula temp->last_runned = acumulatedTime; timeSummation = timeSummation + (temp->last_runned - temp->arrival_time - (temp->cpu_burst - temp->timeleft)); #ifdef DEBUG PrintProcess(temp, acumulatedTime); #endif acumulatedTime = acumulatedTime + temp->timeleft; temp->timeleft = 0; numberProcess++; } } } average = timeSummation/numberProcess; // Destroy the copy of the original list and the iterators g_list_free(list); g_list_free(i); g_list_free(j); printf("\nRound Robin\n"); printf("Average time = %2f\n",average); #ifdef DEBUG printf("%f %f\n",timeSummation,numberProcess); #endif }
//Handle the system signal. void SIG_Handler(const int Signal) { wprintf_s(L"Get closing signal.\n"); PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); exit(EXIT_SUCCESS); return; }
//Catch Control-C exception from keyboard. BOOL __fastcall CtrlHandler(const DWORD fdwCtrlType) { switch(fdwCtrlType) { //Handle the CTRL-C signal. case CTRL_C_EVENT: { wprintf_s(L"Get Control-C.\n"); PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); WSACleanup(); return FALSE; } //Handle the CTRL-Break signal. case CTRL_BREAK_EVENT: { wprintf_s(L"Get Control-Break.\n"); PrintProcess(true, true); WSACleanup(); return TRUE; } //Handle the Closing program signal. case CTRL_CLOSE_EVENT: { PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); WSACleanup(); return FALSE; } //Handle the Closing program signal. case CTRL_LOGOFF_EVENT: { PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); WSACleanup(); return FALSE; } //Handle the shutdown signal. case CTRL_SHUTDOWN_EVENT: { PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); WSACleanup(); return FALSE; } default: { PrintProcess(true, true); //Close file handle. if (OutputFile != nullptr) fclose(OutputFile); WSACleanup(); return FALSE; } } }
/* * * 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 }
int main(void){ ListOfProcesses *LReady; ListOfProcesses *LBlock; ListOfProcesses *LFinished; Memory *Mmemory; FILE *pEntry; FILE *pOut; int i; int counter = 0; int retRunning; int TamReady = 0; tempoTotal = 0; pOut = fopen("OutputFIRST.txt", "w"); if (pOut == NULL){ printf("Erro ao tentar abrir o arquivo!\n"); exit(1); } pEntry = fopen("teste.txt", "r"); if (pEntry == NULL){ printf("Erro ao tentar abrir o arquivo!\n"); exit(1); } LReady = FileReader(pEntry, &TamReady); Mmemory = MemoryCreator(); LBlock = BlockCreator(TamReady); LFinished = FinishedCreator(TamReady); Mmemory->LMemory->quantity = 0; for (i = 0; i < LReady->quantity; i++){ PrintProcess(&(LReady->proc[i]), pOut); } while ((Mmemory->LMemory->quantity + LReady->quantity + LBlock->quantity) != 0){ while ((CheckMemory(Mmemory) >= LReady->proc[0].Memory) && (LReady->proc[0].order != -1)){ ReadyToMemory(LReady, Mmemory); OrganizeList(LReady); fprintf(pOut, "\n-----------------------Mapa de Alocação de Memória-----------------------\n"); PrintMemory(Mmemory, pOut); fprintf(pOut, "-----------------------Fim Mapa de Alocação de Memória-----------------------\n"); } counter++; retRunning = RunningProcess(Mmemory, LBlock, pOut); BlockToReady(LReady, LBlock); ProcessToEverywhere(Mmemory, LBlock, LReady, LFinished, retRunning); PrintMemory(Mmemory, pOut); fprintf(pOut, "\n-----------------------Processos na Fila de Prontos-----------------------\n"); PrintListOfProcesses(LReady, pOut); fprintf(pOut, "\n-----------------------Processos em IO-----------------------\n"); PrintListOfProcesses(LBlock, pOut); fprintf(pOut, "\n-----------------------Processos Já Terminados-----------------------\n"); PrintListOfProcesses(LFinished, pOut); if ((Mmemory->LMemory->quantity == 0) && (LReady->quantity == 0) && (LBlock->quantity != 0)){ for (i = 0; i < LBlock->quantity; i++){ while (LBlock->proc[i].TimeIO[0] != 0){ ExecutaBloqueados(LBlock); Mmemory->LMemory->quantity = 1; } LBlock->proc[i].order = -1; LBlock->quantity = LBlock->quantity - 1; } } } printf("Tempo Decorrido: %ds\n", tempoTotal); fprintf(pOut, "Tempo Total Decorrido: %d\n", tempoTotal); printf("Counter: %d\n", counter); fprintf(pOut, "Numero de iterações do while: %d\n", counter); free(LReady->proc); free(LBlock->proc); free(LFinished->proc); free(LReady); free(LBlock); free(LFinished); free(Mmemory->LMemory->proc); free(Mmemory->LMemory); free(Mmemory); fclose(pEntry); fclose(pOut); return 0; }