// // Program to simulate CPU scheduling. // // Input (from standard input) is one or more lines, each giving: // Job ID (string) // Arrival time (integer) // Running time (integer) // Priority (integer, where higher numbers indicate higher priority) // We assume that input is sorted by arrival time. // // Output is, for each scheduling algorithm implemented, the following // information for each job, plus average turnaround: // Job ID and other input info // Start time // End time // Turnaround time // #include // C++ stream I/O #include // C-style I/O #include // has exit(), etc. #include // C++ template library class #include // C++ template library class #include // C++ template library class // for info on C++ template library classes see // http://www.sgi.com/tech/stl/ using namespace std; // C++ "standard" namespace // ---- Class for representing each job ---- class jobInfo { public: // Constructor // parameters are jobID, etc., from program input jobInfo(string _jobID, int _arrival, int _runtime, int _prty) : jobID(_jobID), arrival(_arrival), runtime(_runtime), prty(_prty), start(0), end(0), turnaround(0), timeLeft(0) { } // Variables // input string jobID; int arrival; int runtime; int prty; // to be filled in by scheduler function int start; int end; int turnaround; // to be used as work by scheduler function int timeLeft; }; typedef vector::size_type v_size_type; // ---- Function to print contents of vector of jobInfo objects ---- void print(const char *headerMsg, vector &v) { printf("\n%s\n\n", headerMsg); printf("%10s %10s %10s %10s %10s %10s %10s\n\n", "job", "arrival", "runtime", "priority", "start", "end", "turnaround"); int totalTurnaround = 0; for (v_size_type i = 0; i < v.size(); ++i) { printf("%10s %10d %10d %10d %10d %10d %10d\n", v[i].jobID.c_str(), v[i].arrival, v[i].runtime, v[i].prty, v[i].start, v[i].end, v[i].turnaround); totalTurnaround += v[i].turnaround; } printf("\nAverage turnaround time = %12.2f\n", (double) totalTurnaround / (double) v.size()); } // ---- Scheduler functions (declarations) ---- void scheduleFCFS(vector &v); /* void scheduleSJF(vector &v); void schedulePriority(vector &v); void scheduleRR(vector &v, const int quantum); */ // ---- Main program ---- int main(void) { vector jobs; string jobID; int arrival, runtime, prty; while (cin >> jobID >> arrival >> runtime >> prty) { jobs.push_back(jobInfo(jobID, arrival, runtime, prty)); } scheduleFCFS(jobs); print("First come, first served", jobs); /* scheduleSJF(jobs); print("Shortest job first", jobs); schedulePriority(jobs); print("Priority", jobs); scheduleRR(jobs, 1); print("Round robin, quantum = 1", jobs); scheduleRR(jobs, 2); print("Round robin, quantum = 2", jobs); */ } // ---- Scheduler functions (definitions) ---- // // These functions should fill in the start, end, and turnaround fields // of each element of the vector of jobInfo objects. // First come, first served. void scheduleFCFS(vector &v) { int time = 0; // simulated time list readyQueue; // queue of ready jobs (indices into v) v_size_type nextJob = 0; // next job in v to arrive // compute ending time int endTime = 0; for (v_size_type i = 0; i < v.size(); ++i) endTime += v[i].runtime; // schedule jobs until ending time is reached while (time < endTime) { // add jobs whose arrival time is <= current time to ready queue while (nextJob < v.size() && v[nextJob].arrival <= time) readyQueue.push_back(nextJob++); // if there's anything ready, schedule it if (readyQueue.size() > 0) { v_size_type job = readyQueue.front(); readyQueue.pop_front(); v[job].start = time; time += v[job].runtime; v[job].end = time; v[job].turnaround = v[job].end - v[job].arrival; } // otherwise increment time and try again else { time = v[nextJob].arrival; } } }