|
|
|
|
|
round robin simulator
|
|
|
Analysing Round Robin Scheduling Computing 125 assignment 3 we will study the effect of several parameters used with Round Robin scheduling. We need to write a discrete simulation to study the performance of this strategy under different time slice lengths and different context switching times. We did this by writing a simulation program to imitate the behaviour of a single-CPU system that has a pre-emptive RR scheduler. We used certain variables to represent process arrival and service times, where each line represents a process arriving into the simulated system. The first number is the arrival time (in integer seconds), and the second number is the amount of time the process is required to complete (in floating-point seconds). The Round Robin schedule program is shown in pictorial form … This program simulates Round Robin scheduling with some simulated test data and then performs some calculations to extract certain statistics about how various combinations of context switching times and time-slices affect the wait times and turnaround times of the processes (jobs) being scheduled. Jobs arrive and are sent to the queue, if the CPU is not busy a job is selected from the head of the queue and is allocated a fixed amount of time called the time slice. New arrivals are joined at the end of the queue like an ordinary waiting queue. The Code….. /////////////////////////////////////////////////////////////////////////////////////// ////////////////////// COMP 125 /////////////////////////////// ////////////////////// /////////////// ////////////////////// ASSIGNMENT 3 Simula.cpp ////////////////// ////////////////////////////////////////////////////////////////////////////////////// #include "Simula.h" // provides access to Simula.h file #include // provides priority_queue #include // provide use of lists #include // provides i/o functionality #include // provides setprecision #include // provides file i/o functionality #include // provides size_t, atol #include // provides isspace using namespace std; void main() { Simula roundRobin; roundRobin.run(); system ("pause"); } void Simula::run() { cout << "Enter simulation value: "; //the running of the prgram cin >> simulation; cout << "Enter timeSlice: "; //required output cin >> timeSlice; cout << endl; cout << "average waiting time: " << waitingTime << endl; cout << "average time required: " << timeReq << endl; cout << "utilization of the processor: " << completeJobs <<"%"<< endl; cout << "short jobs (percent): " << compShort << "%" << endl; cout << "average waiting time for short jobs: " << aveWait << endl; } Simula::Simula() { simulation = 0.0; //values of the variables timeSlice = 0.0; completeJobs = 0.0; compShort = 0.0; waitingTime = 0.0; timeReq = 0.0; aveWait = 0.0; } Simula::Simula(int sim, int time, int comj, int comsj, int wait, int totime) { simulation = sim; timeSlice = time; completeJobs = comj; compShort = comsj; waitingTime = wait; timeReq = totime; } //Insert a job into the queue //Pre: The element x is defined //Post:If there is space on the queue, the job is inserted onto the queue //and true is returned.Otherwise, the queue is unchanged and false is returned int Simula::arrival(const number& x) { wait* oldJob; bool success; //program flag indicates success //of failure oldJob = rear; //save old rear rear = new wait; //allocates new job if(rear == NULL) //check to see if it was successfull { success = false; //indicate insert failed rear = oldJob; //if not, restore rear } else { if(front == NULL) { front = rear; front->previous = NULL; } else { oldJob->next = rear; //link the new job rear->previous = oldJob; } rear->Jobnumber = x; //store x in new job rear->next = NULL; success = true; //idicate success; numJobs++; } return success; } bool Simula::CPUempty() { return front == NULL; //testing weather the queue is empty } bool Simula::CPUfull() { return false; //testing weather the queue is full } //A job departs the queue //Pre: none; //Post: If the queue is not empty, the value at the front of the queue is removed //its value is placed in x, and true is returned.If the queue is empty, x is not //defined and false is returned bool Simula::departure(number& x) { wait* oldJob; //prgram flag success or failure bool success; if(front == NULL) success = false; else { x = front->Jobnumber; //copy front of queue into x success = true; //indicate success numJobs--; if(front->next == NULL) { delete front; front = NULL; rear = NULL; } else { //save oldJob of queue oldJob = front; //reset the front of queue front = oldJob->next; if(front == NULL) rear = NULL; else front->previous = NULL; delete oldJob; } } return success; } //Remove the timed out Job //Pre: none //Post: If the queue is not empty, the value at the front of the queue is removed //its value is placed into x and true is returned //if the queue is empty x is not defined and false is returned bool Simula::timeout(number x) { bool success; if(front==NULL) //program flag indicates success or failure success = false; else if(front->Jobnumber == x) { success = true; //indicates success numJobs--; if(front->next == NULL) { delete front; front = NULL; rear = NULL; } else { wait* oldJob; //save old front of queue oldJob = front; //reset front of queue front = oldJob->next; if(front == NULL) //return head job rear = NULL; else front->previous = NULL; delete oldJob; } } else { wait* curr = front; wait* prev = NULL; while((curr != NULL) && (curr->Jobnumber !=x)) { prev = curr; curr = curr->next; } if(curr == NULL) success = false; else { success = true; numJobs--; if(curr->next == NULL) { delete curr; rear = prev; rear->next = NULL; } else { prev->next = curr->next; curr->next->previous = prev; delete curr; } } } return success; } //////////////////////////////////////////////////////////////////////////////// ////////////////////// COMP 125 ///////////////////////// ////////////////////// ///////// ////////////////////// ASSIGNMENT 3 Simula.cpp //////////// ///////////////////////////////////////////////////////////////////////////////// typedef int number; class Simula { public: //Constructors to be used Simula(); Simula (int sim, int time, int comj, int comsj, int wait, int totime); void run(); int arrival(const number& x); bool CPUempty(); bool CPUfull(); bool timeout(number x); bool departure(number& x); void job(); void event(); private: struct wait { number Jobnumber; wait* next; wait* previous; }; wait* front; //front of the queue wait* rear; //back of the queue int Jobnumber; //number of jobs int numJobs; //number of jobs in the queue double simulation; // maximum simulation time double timeSlice; // time slice double completeJobs; // total number of completed jobs double compShort; // total number of completed short jobs double waitingTime; // total waiting time for completed jobs double timeReq; // total time required for completed jobs double aveWait; // average waiting time };
|
|
|
|
Still Can't Find What Your Looking For? Then Try a Essay Search!
|