#include #include #include #include #include "myconst.h" #include "event.h" #include using namespace std; typedef queue procqueue; bool newEvent(); process makeNewEvent(int & , int); void printQ(procqueue &); int main () { procqueue Q; int clock=0; int maxpid = 0; process p; int i; srand48(time(NULL)); for(clock=0;clock<40;clock++) { cout << "Clock:" << setw(3) << clock << endl; if (newEvent() ) { p = makeNewEvent(maxpid, clock); Q.push(p); cout << "\tAdded " ; cout << p << endl; } if (!Q.empty()) { // let an event run; p = Q.front(); Q.pop(); p.useTicks(1); cout << "\tRan " << p; if (!p.done() ) { Q.push(p); } else { cout << " DONE! "; cout << p.ticksNeeded() << "/" << clock-p.startTime()+1 ; } cout << endl; printQ(Q); } else { cout << "\tCPU IDLE" << endl; } } } bool newEvent() { return(drand48()*100 > NEWPROCPCT); }; process makeNewEvent(int & maxpid, int clock){ maxpid++; process rv(clock, maxpid); return(rv); }; void printQ(procqueue & Q) { procqueue TQ; process p; bool first = true; if (!Q.empty()) { cout << "\tCurrent Queue: ("; while (!Q.empty()) { p= Q.front(); Q.pop(); TQ.push(p); if (first) { first = false; } else { cout << ", "; } cout << p.Pid(); } cout << ")"<< endl; while(!TQ.empty()) { p= TQ.front(); TQ.pop(); Q.push(p); } } }