#include #include #include int randnum(int max) { return (rand() % max); } struct block { int time; int length; block* nextBlock; block* prevBlock; }; class blockList { public: int temp; block* first; blockList(); void addBlock(int); void print(); }; class process { public: int ready; int work; int type; int name; int numBlocks; blockList blocking; process* next; process* prev; int getWork(int); blockList getBlocking(); int valid(int, int); process(); process(int); void display(); bool isready(int); void removeblock(); }; blockList::blockList(void) { first = NULL; } void blockList::addBlock(int prevBlockTime) { block* newblock = new block; newblock->time = prevBlockTime; newblock->length = (4 + randnum(12)); newblock->nextBlock = first; newblock->prevBlock = NULL; first = newblock; } void blockList::print(void) { block* current = first; if(current == NULL) cout << "No Blocking Occurs" << endl; else if (current->nextBlock == NULL) { while(current != NULL) { cout << "(" << current->time << ", " << current->length << ")" << endl; current = current->nextBlock; } } else { temp = 1; while(current != NULL) { if (temp == 1) { cout << "(" << current->time << ", " << current->length << ")" << endl; current = current->nextBlock; temp++; } else { cout << "\t\t\t\t" << "(" << current->time << ", " << current->length << ")" << endl; current = current->nextBlock; } } temp=1; } } process::process(void) { int test = randnum(10); ready = randnum(30); work = getWork(test); next = NULL; prev = NULL; name = 0; blocking = getBlocking(); } process::process(int start) { int test = randnum(10); ready = (start + randnum(30)); work = getWork(test); next = NULL; prev = NULL; name = 0; blocking = getBlocking(); } void process::display(void) { cout << "P" << name << '\t' << type << "\t" << ready << "\t" << work << "\t"; blocking.print(); } int process::getWork(int test) { if(test < 6) { type = 1; if(randnum(10) < 2) numBlocks = 1; else numBlocks = 0; return (5 + randnum(8)); } else if(test < 9) { type = 2; numBlocks = 0; return (10 + randnum(31)); } else { type = 3; int temp = (25 + randnum(51)); numBlocks = temp/10; return temp; } } blockList process::getBlocking(void) { int count = numBlocks; int subten = 10; blockList list; while(count != 0) { int test = ((work - subten) + randnum(10)); test = valid(test, subten); list.addBlock(test); subten += 10; count--; } return list; } int process::valid(int num1, int num2) { if(num1 > 0) return num1; else valid(((work - num2) + randnum(10)), num2); } bool process::isready(int time) { if ( ready <= time ) return true; else return false; } void process::removeblock(void) { block* current = blocking.first; block* copy = blocking.first; int holder = current->time; if ( current->nextBlock == NULL ) { blocking.first = NULL; delete current; } else { while(copy->nextBlock != NULL) { copy->nextBlock->time -= holder; copy = copy->nextBlock; } //delete copy; //blocking.print(); blocking.first = current->nextBlock; blocking.first->prevBlock = NULL; blocking.first->nextBlock = current->nextBlock->nextBlock; delete current; } }