// JobQueue.h Dave Reed 9/3/02 // // JobQueue class, stores and manipulates CPU jobs /////////////////////////////////////////////////////////////////////////////// #ifndef _JOB_QUEUE_ #define _JOB_QUEUE_ #include #include #include #include "Queue.h" #include "PriorityQueue.h" using namespace std; enum JobStatus {OK, DONE, IO}; class JobQueue { public: JobQueue(string filename); // constructor bool JobsRemaining(); // determines if any jobs in queue int GetCurrentID(); // gets ID of current job int GetCurrentStart(); // gets start time of current job void InterruptCurrentJob(int newStart); // interrups current job, sets restart void RemoveCurrentJob(); // removes current job from queue JobStatus ExecuteCurrentJob(); // executes current job one cycle private: class Job { public: int ID; Queue bursts; }; PriorityQueue jobQueue; }; JobQueue::JobQueue(string filename) // constructor: assumes filename contains data to be placed in queue { ifstream infile(filename.c_str()); char ch; int start, id, len; string line; while ((infile >> start >> ch >> id) && getline(infile, line)) { Job j; j.ID = id; istrstream istr(line.c_str()); while (istr >> len) { j.bursts.Enter(len); } jobQueue.Enter(j, start); } } bool JobQueue::JobsRemaining() // Returns: true if queue contains a job, false if empty { return !jobQueue.IsEmpty(); } int JobQueue::GetCurrentID() // Returns: ID of current job (at front of queue) { return jobQueue.Front().ID; } int JobQueue::GetCurrentStart() // Returns: start time of current job (at front of queue) { return jobQueue.FrontPriority(); } void JobQueue::InterruptCurrentJob(int newStart) // Results: replaces the current job in queue with new start time { jobQueue.Enter(jobQueue.Front(), newStart); jobQueue.Remove(); } void JobQueue::RemoveCurrentJob() // Results: removes current job from queue { jobQueue.Remove(); } JobStatus JobQueue::ExecuteCurrentJob() // Returns: status of executing current job for one cycle { jobQueue.Front().bursts.Front()--; if (jobQueue.Front().bursts.Front() == 0) { jobQueue.Front().bursts.Remove(); if (jobQueue.Front().bursts.IsEmpty()) { return DONE; } else { return IO; } } else { return OK; } } #endif