#include #include "plist.cpp" class blockqueue { public: processlink* first; blockqueue(); void print(); bool isEmpty(); void removeprocess(); void addBlock(process, int); processlink* findSpot(int, processlink*); }; class readylist { public: processlink* first; readylist(); void getready(processList &,blockqueue &, int); void print(); void addready(process); void addreadyrr(process, int); bool isEmpty(); void removeprocess(char, processList &, blockqueue &, int, int); processlink* findlast(); void addreadynew(process); void addreadynew(process, int); void removeprocessnew(char, processList &, blockqueue &, int, int); processlink* findSpot(int, processlink*); void removeprocessSJF(char, blockqueue &, int); }; blockqueue::blockqueue(void) { first = NULL; } void blockqueue::addBlock(process p, int time) { processlink* newlink = new processlink; processlink* current = first; newlink->p = p; newlink->p.ready = time+newlink->p.blocking.first->length; //cout << "the ready = " << newlink->p.ready; newlink->p.work -= newlink->p.blocking.first->time; //cout << "the work = " << newlink->p.work; newlink->p.removeblock(); //newlink->p.blocking.print(); if(current == NULL) { newlink->next = current; newlink->prev = NULL; first = newlink; } else { processlink* spot = findSpot(newlink->p.ready, current); if((spot->prev == NULL) && (spot->p.ready >= newlink->p.ready)) { newlink->next = spot; spot->prev = newlink; newlink->prev = NULL; first = newlink; } else if((spot->next == NULL) && (spot->p.ready <= newlink->p.ready)) { newlink->next = NULL; newlink->prev = spot; spot->next = newlink; } else { newlink->prev = spot->prev; spot->prev->next = newlink; spot->prev = newlink; newlink->next = spot; } } } processlink* blockqueue::findSpot(int ready, processlink* current) { if((current->next == NULL) || (current->p.ready > ready)) return current; else findSpot(ready, current->next); } void blockqueue::print() { processlink* current = first; while(current != NULL) { cout << "P" << current->p.name << " "; current = current->next; } cout << endl; } bool blockqueue::isEmpty(void) { if(first == NULL) return true; else return false; } void blockqueue::removeprocess(void) { processlink* current = first; if(current->next == NULL) { first = NULL; delete current; } else { first = current->next; first->prev = NULL; first->next = current->next->next; delete current; } } readylist::readylist(void) { first = NULL; } void readylist::getready(processList & pl,blockqueue & bl, int time) { //cout << "This is the current process list" << endl; //pl.printList(); //cout << "This is the current ready list" << endl; //print(); //int jobbyjob; //cin >> jobbyjob; //if(jobbyjob == 1) //{ if( (!(pl.isEmpty()) ) && (bl.isEmpty()) ) { process pro = pl.returnFirst()->p; if (pro.isready(time)) { addready(pro); pl.removeprocess(); getready(pl ,bl, time); } } else if( (!(bl.isEmpty()) ) && (pl.isEmpty()) ) { process pro = bl.first->p; if (pro.isready(time)) { addready(pro); bl.removeprocess(); getready(pl , bl, time); } } else if( (!(bl.isEmpty())) && (!(pl.isEmpty())) ) { process propl = pl.returnFirst()->p; process probl = bl.first->p; if (propl.ready < probl.ready) { if (propl.isready(time)) { addready(propl); pl.removeprocess(); getready(pl ,bl, time); } } else { if (probl.isready(time)) { addready(probl); bl.removeprocess(); getready(pl , bl, time); } } } } void readylist::addready(process p) { processlink* newlink = new processlink; processlink* current = findlast(); newlink->p = p; if(current == NULL) { newlink->next = current; newlink->prev = NULL; first = newlink; } else { current->next = newlink; newlink->prev = current; newlink->next = NULL; } } void readylist::addreadyrr(process p, int quantum) { processlink* newlink = new processlink; processlink* current = findlast(); newlink->p = p; newlink->p.work -= quantum; if(newlink->p.blocking.first != NULL) { block* current = newlink->p.blocking.first; while(current != NULL) { current->time -= quantum; current = current->nextBlock; } } if(current == NULL) { newlink->next = current; newlink->prev = NULL; first = newlink; } else { current->next = newlink; newlink->prev = current; newlink->next = NULL; } } void readylist::addreadynew(process p) { processlink* newlink = new processlink; processlink* current = first; newlink->p = p; if(current == NULL) { newlink->next = current; newlink->prev = NULL; first = newlink; } else { processlink* spot = findSpot(newlink->p.work, current); if((spot->prev == NULL) && (spot->p.work >= newlink->p.work)) { newlink->next = spot; spot->prev = newlink; newlink->prev = NULL; first = newlink; } else if((spot->next == NULL) && (spot->p.work <= newlink->p.work)) { newlink->next = NULL; newlink->prev = spot; spot->next = newlink; } else { newlink->prev = spot->prev; spot->prev->next = newlink; spot->prev = newlink; newlink->next = spot; } } } processlink* readylist::findSpot(int work, processlink* current) { if((current->next == NULL) || (current->p.work > work)) return current; else findSpot(work, current->next); } void readylist::print(void) { processlink* current = first; while(current != NULL) { cout << "P" << current->p.name << " "; current = current->next; } cout << endl; } processlink* readylist::findlast(void) { processlink* current = first; if (current != NULL) { while(current->next != NULL) { current = current->next; } return current; } else return current; } bool readylist::isEmpty(void) { if(first == NULL) return true; else return false; } void readylist::removeprocess(char c, processList & pl, blockqueue & bl, int time, int quantum) { processlink* current = first; if (c == 'd') { if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } else if (c == 'r') { getready(pl, bl, time); addreadyrr(current->p, quantum); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } else { bl.addBlock(current->p, time); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } } void readylist::removeprocessSJF(char c, blockqueue & bl, int time) { processlink* current = first; if (c == 'd') { if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } else if (c == 'r') { addready(current->p); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } else { bl.addBlock(current->p,time); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } } void readylist::removeprocessnew(char c, processList & pl, blockqueue & bl, int time, int quantum) { processlink* current = first; if (c == 'd') { if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } else if (c == 'r') { pl.addProcess(current->p, quantum); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } getready(pl, bl, time); } else { bl.addBlock(current->p, time); if(first != NULL) { first = current->next; if (first != NULL) { first->prev = NULL; first->next = current->next->next; } delete current; } else { first = NULL; delete current; } } }