1 / 19

Computer System Simulation

Computer System Simulation. Problem: Write a general-purpose simulation program that determines how long items jobs (people, cars, …) must wait in line before being served and determine values of other criteria.

rhona
Télécharger la présentation

Computer System Simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer System Simulation Problem: Write a general-purpose simulation program that determineshow long itemsjobs (people, cars, …) must wait in linebefore being served and determine values of other criteria. • The simulation must vary the number of servers, the time between arrivals of the jobs and the job processing time • Include the admission and CPU schedulers. The admission scheduler purpose is to check whether a job arrives, if yes put in a FIFO queue. The CPU scheduler purpose is actually picking one of the ready job from the queue. • The program should calculate the performance parameters specified below.

  2. Case Study - Simulation Problem: Write a general-purpose simulation program to simulate a queuing system The input is as follows: • The length of the simulation • # of servers (CPUs) • The distribution of arrival times (average time between job arrivals) • The maximal processing time per job Assumptions:: No more than one job arrives per time unit

  3. Criteria • Average Processing Time APT = Σ PT for all jobs / n • Average Wait Time AWT = Σ WT for all jobs / n • Average Turnaround Time ATT = Σ (ET – AT) for all jobs / n • Average Completion Time ACT = Σ (ET) for all jobs / n • Average Response Time ART = Σ (ST - AT) for all jobs / n • Average Throughput ATH - number of completed Jobs (processed) during the simulation • where: WT – job Wait Time, ST – job Start Time, ET – job End Time, AT – job Arrival Time • n – number of Jobs reached servers during the simulation e.g. 1, 2, 5 or - number of Jobs number of completed Jobs (processed) during the simulation e.g. 3, 4

  4. Output The output table should include the following columns : • Simulation Length • Number of CPUs • Maximal Transaction Time • Time Between Arrivals • TJA - total jobs arrived • TJD - total jobs done (serviced) • APT - Average Processing Time • AWT - Average Wait Time • ATT - Average Turnaround Time • ACT - Average Completion Time • ART - Average Response Time • ATH – Average Throughput - number of completed job (processes) during the simulation • JLQ - Jobs Left unserviced in queue • JLS - Jobs Left unserviced in servers • Please bold the best result from 7 to 12 for each different input dataset :

  5. Case Study - Simulation • A queuing system consists of servers and queues of objects to be served. • The objective of a queuing system is to utilize the servers (CPUs, the tellers, checkers, operators, etc.) as fully possible, while keeping the wait time within reasonable limit. • The operating system is a queuing system • These goals usually require a compromise between cost and customer satisfaction

  6. Case Study - Simulation The technique that is used to make the model behave in the same way as the real object is called simulation. We can use similar technique to build computer models of objects and events rather than physical models.

  7. A big loop - framework • From the software point of view the simulation is a big loop that executes a set of rules for each value of the clock is e.g. 1 to 100 clock units • R1: Admission scheduler: If a job (customer) arrives, it gets in queue • R2. CPU scheduler: If the CPU (teller) is free and a job (someone) is waiting, the first job (customer) in queue leaves the queue and advances to the CPU (teller’s window). • The service time is set to e.g. 5 seconds • R3. If a job (customer) is at the CPU (teller’s window) the timeremaining is decremented by 1 • R4. If there are jobs (customers) in queue, the additional second that they have remained in the queue is recorded

  8. // File: Simulate.cpp // This file contains the coded classes class Timer // timer.h { public: Timer(); // Class constructor // Sets count to zero. void SetTimer(int value); // Sets count to value. void Increment(); // Increments count. void Decrement(); // Decrements count. int TimeIs() const; // Returns count. private: int count; }; class Job //job.h { public: Job (); // Class constructor void IncrementWaitTime(); // see Rule4 int WaitTimeIs() const; // Returns the value of waitTime. private: Timer waitTime; }; Extend the Job class attributes and include some of them for your needs pId /* process id priority /* integer state /* 0 for ready, 1 for run, 2 for blocked, 3 for terminated processingTime /* CPU processingTime in milliseconds CPUId /* CPU # waitTime /* in milliseconds turnaroundTtime /* in milliseconds completionTime /* in milliseconds responseTime /* in milliseconds arrivalTime /* in milliseconds deadline /* in milliseconds class Server // server.h { public: Server (); // Class constructor // Initializes status to FREE. bool IsFree() const; // Returns true if status is FREE; false otherwise. void SetBusy(); // Sets status to BUSY. void SetFree(); // Sets status to FREE. void SetTimeRemaining(int time); //see Rule3 // Sets timeRemaining to time. bool IsTimeZero(); // Calls: Timer.TimeIs. // Called by: ServerList.UpdateServers. // Function: Returns weather the time remaining is // zero or not. // Pre: The server is initialized. // Post: If the time remaining is zero returns true, // otherwise false. private: StatusType status; Timer timeRemaining; }; enum StatusType{BUSY, FREE}; // Both objects: Job and Server need a Timerobject

  9. class ServerList // serverlist.h - container of objects { public: ServerList();// class constructor - uses the new operator to allocate // the array of servers { numServers = 10; servers = new Server[ numServers ]; } ServerList (int number); // sets numServers to the input parameter number and // uses the new operator to allocate the array of servers { numServers = number; servers = new Server[ numServers ]; for( int count = 0; count < numServers; count++ ) { servers[ count ].SetFree(); } } ~ServerList();// class destructor void GetFreeServerId(int serverID, Boolean& found); // searches for the server id that is free void EngageServer(int serverId, int processingTime); // R2:sets the server’s timer to // processingTime and its status to BUSY void UpdateServers();// R3:: loops through the servers, // decrementing the timers of the active servers and changing a server’s status if the timer is zero private: int numServers; Server* servers; // ServerList(int number) uses the new operator to allocate // the array of servers };

  10. // Queue.h // Description: This class is a basic Queue type class, with an additional function // updateQueue. This Queue will simulate the line which will store // the jobs that are waiting to be served in the simulation. //////////////////////////////////////////////////////////////////////////////////////// #ifndef QUEUE_H #define QUEUE_H #include "Job.h" class FullQueue {}; class EmptyQueue {}; class Queue { public: Queue(); // Calls: None. // Called by: ExecuteSimulation. // Function: Class Constructor. // Pre: None. // Post: A Queue is created and initialized. Queue( int max ); // Calls: None. // Called by: None. // Function: Overloaded class constructor. // Pre: None. // Post: A Queue is created and initialized using max for the size. ~Queue(); // Calls: None. // Called by: Main. // Function: Class destructor. // Pre: None. // Post: Queue is deleted. void MakeEmpty(); // Calls: None. // Called by: None. // Function: Initializes the queue to an empty state. // Pre: Queue is initialized. // Post: Queue is empty. bool IsEmpty() const; // Calls: None. // Called by: ExecuteSimulation, CleanUp, // Function: Determines whether the queue is empty. // Pre: Queue is initialized. // Post: Function value = (queue is empty) bool IsFull() const; // Calls: None. // Called by: None. // Function: Determines whether the queue is full. // Pre: Queue is initialized. // Post: Function value = (queue is full) void Enqueue( Job newItem ); // Calls: // Called by: // Function: Adds newItem to the rear of the queue. // Pre: Queue is initialized, an item of the type of object in the queue // is passed. // Post: If (queue is full) FullQueue exception is thrown // else newItem is placed at the rear of the queue. void Dequeue( Job& item ); // Calls: None. // Called by: StartJob, CleanUp, Queue.UpdateQueue. // Function: Removes front item from the queue and returns it in item. // Pre: An item to hold the dequeued item is passed. // Post: If (queue is empty) EmptyQueue exception is thrown and // item is undefined else front element has been removed from queue // and item is a copy of removed element. void UpdateQueue( Queue& ); // see later // Calls: Queue.Enqueue, Queue.Dequeue, Job.WaitTimeIs, Job.IncrementWaitTime. // Called by: ExecuteSimulation. // Function: For every job in the queue it increments the time of the Timer object of that job. // Pre: Queue has been initialized. // Post: The timer of each job in the queue has been incremented. private: int front; int rear; int maxQueue; Job* items; }; #endif

  11. UpdateQue(waitQue) void Queue::UpdateQueue( Queue& waitQue ) { int number = -1; Job currentJob; Job dummyJob; dummyJob.waitTime = number; waitQue.Enqueue( dummyJob ); waitQue.Dequeue(currentJob ); while(currentJob.WaitTimeIs() != number ) { currentJob.IncrementWaitTime(); waitQue.Enqueue( currentJob ); waitQue.Dequeue(currentJob ); }

  12. Driver (driver.cpp) #include <iostream> #include <fstream> #include <cstdlib> #include "ServerList.h" #include "Queue.h" using namespace std; Open reportFile for writing Write title and headings to reportFile do ExecuteSimulation // the main work Write to screen “Enter an S to stop or another letter to continue; press return.” Read letter Set finished to (letter = “S” or “s”) whileNOT finished Write trailer to reportFile

  13. struct ParametersType struct ParametersType { int timeLimit; int numServers ; int processingTimeMax; // needed to generate processingTime int TimeBetween; }; You can extend this struct to keep some global criteria: float avgProcTime; // average Processing of completed jobs float avgWaitTime; // average wait of completed jobs float avgTurnarroundTime; // averageTurnarround of completed jobs float avgCompletionTime; // averageCompletion of completed jobs float avgResponseTime; // average Response of completed jobs int avgThroughput; // Throughput int totalJobsArrived; // total jobs arrived int totalJobsDone; // // total jobs serviced int jobsLeftInQue; // jobs left in queue int jobsLeftInSrv; ; // jobs left in servers

  14. ExecuteSimulation GetParameters ( ParametersType& parameters) {parameters.timeLimit = 100, parameters.numServers = 2, parameters.processingTimeMax = 5, parameters.TimeBetween = 3) } // a stub for testing your program // Initialize simulation variables – keep track oftotalWaitand numJobs: { Set totalWait for all jobs to 0 Set all criteria parameters for all jobs to 0 Set numJobs to 0 // the number of jobs served Set clock to 0 } while more time to run UpdateSimulationClock() // increment a clock UpdateServers() // see ServerList function – loops through the servers, // decrementing the timers of the active servers and // changing a server’s status if the timer is zero UpdateQue(waitQue) // for each job: dequeue one job at a time, increment the timer; // put the job back into the queue 1A: ifJobArrives() // 1.A -2 Admission Scheduler 1B: {SetProcessingTime(procTimeMax, job) 2: waitQue.Enqueue(job) } 3: servers.GetFreeServerId(serverId, found) // 3-5 CPU Scheduler 4: if ( found AND !waitQue.Empty() 5: StartJob // clock hits the timeLimit - end of while 6: Clean up wait queue 7: Calculate average wait time and other criteria 8: PrintResults

  15. 1A: JobArrives() #include <cstlib> // has rand() returns int in the range of [0, RAND_MAX], // to convert this # to a value between 0.0 and 1.0, cast it to // float(rand)/float(RAND_MAX) bool JobArrives() // if the random number is between 0.0 and // arrivalProb = 1/timeBetween // (inclusive), a job has arrived and JobArrives() return true; // otherwise no job arrives Set value to float(rand) / float(RAND_MAX) // or use srand( unsigned int) with an initial seed before the 1st call to rand - from C++ standard library, rand uses the same seed producing the same sequence of “random” numbers return (value <= arrivalProb) Where : arrivalProb = 1/timeBetween If a new job arrives every 5 sec, then the chance of a job arriving in any given sec is 0.2

  16. 1B: setProcessingTime setProcessingTime(int procTimeMax, Job& job) // generate processingTime based on // parameters.processingTimeMax Set value to float(rand) / float(RAND_MAX) // or use srand(unsigned int) Set job.processingTime according to value

  17. 5. StartJob 5: StartJob (servers, waitQue, serverId, parameters, numJobs, totalWait) waitQue.Dequeue(job) Increment numJobs Set totalWait to totalWait + job.WaitTimesIs() // for “dequeued” one // total parameters you may save in struct parametersType before calculating the average instead of keeping globals setProcessingTime(int procTimeMax, Job& job) Set the waitTime, responseTime etc. for this job servers.EngageServers (serverId, job.processingTime)

  18. 6: Clean up wait queue 7: Calculate average wait time This is an example. You need to extend it. 6: Clean up wait queue (waitQue, jobsLeftInQue) Set jobsLeftInQueue to 0 while NOT waitQue.IsEmpty() waitQue.Dequeue(job) Increment jobsLeftInQueue 7: Calculate average wait time and other criteria if numJobs > 0 Set averageWait to totalWait / numJobs else Set averageWait to 0 ………….

  19. 8: PrintResults // a stub void PrintResults(ofstream& reportFile, ParametersType parameters, float averageWait, int jobsLeftInQue) { reportFile << "Average wait time is " << averageWait << endl; …………. } void PrintReportHeader(ofstream& reportFile) { reportFile << "Starting Simulation" << endl; } void PrintReportTrailer(ofstream& reportFile) { reportFile << "Ending Simulation" << endl; }

More Related