unsigned int System::attainResource(Process *process) { wait(); int rtn = 0; unsigned int requestNum = process->getRequestNeed(); unsigned int available_bk = getAvailable(); unsigned int ownNeed_bk = process->getOwnNeed(); std::string prompt; if(requestNum <= getAvailable()) { setAvailable(getAvailable() - requestNum); process->setOwnNeed(process->getOwnNeed() + requestNum); prompt = prompt + "\nsystem" + "\t" + std::to_string(available_bk) + '\n'; std::cout << prompt; prompt.clear(); if(banker(process)) { processList.insert(process); prompt = prompt + "Thread" + "\t" + std::to_string(process->getUid()) + "\t" + std::to_string(process->getMaxNeed()) + "\t" + std::to_string(process->getOwnNeed()) + "\t" + std::to_string(process->getRequestNeed()) + "\t" + "Success\n"; std::cout << prompt; rtn = 0; } else { setAvailable(available_bk); process->setOwnNeed(ownNeed_bk); prompt = prompt + "Thread" + "\t" + std::to_string(process->getUid()) + "\t" + std::to_string(process->getMaxNeed()) + "\t" + std::to_string(process->getOwnNeed()) + "\t" + std::to_string(process->getRequestNeed()) + "\t" + "Failure\n"; std::cout << prompt; rtn = 1; } printProcessList(std::cout); } else { rtn = 1; } release(); return rtn; }
/* * this is the main function. the program starts from here. * this method: * 1- read process data from 'input.txt' * 2- calls another method to start the cpu scheduler * 3- calls another method to calculates totals and averages */ int main(void){ ProcessList *processList = malloc(sizeof(ProcessList)); int isFileExist = readDataFromFile("input.txt", processList); if(isFileExist != 1){ return -1; } //printList(intList); printf("Here is list of processes and their information that have been loaded from 'input.txt':\n"); printProcessList(processList); printf("\nNow we start the CPU scheduler using Shortest Job First Schedulling algorithm: \n"); startCPUScheduler(processList); //printProcessList(processList); calculateTotals(processList); printf("Finished\n"); return EXIT_SUCCESS; }
inline void linux_ps(Monitor* mon) { //We can pass in NULL since printProcessList uses DECAF_fprintf printProcessList(NULL); }