//--------------------------------------------------------- // Hmwk09 // // Simulate a client-server situation with one waiting line. // // D. Searls // REPLACE THIS LINE WITH YOUR NAME!! // Asbury University //--------------------------------------------------------- #include #include #include using namespace std; //********************************************************* // A clientType structure represents a client of some sort // that needs service. Its properties are its id (an // integer identifer), the time it entered the waiting line, // the time it exited the waiting line (after it has // actually done so, of course), and the time it will take // to be serviced. // // In a general waiting line simulation, a newly arriving // client is put into a waiting line where it waits until // it reaches the head of the line and a server is // available to serve it. At that time, the client is // removed from the line and sent to an available server. //********************************************************* struct clientType { int id; // Client identifier int enterTime; // Time client entered waiting line int exitTime; // Time client exited waiting line int serviceTime; // Time required to serve client }; //********************************************************* // A serverType object represents an entity that serves // clients. When a server's client is set, it becomes busy // (serving the client). A busy server can tell you who its // client is, when it will be done serving the current // client (if it is busy) and whether it is busy or not. // // When a server has finished serving a client, it should // be cleared; indicating that it is not busy and is ready // for a new client. //********************************************************* class serverType { private: clientType myClient; // The client being served int finishTime; // The time service will be finished bool busy; // True if server is busy else false public: //----------------------------------------------------- // Construct a default server. A default server is not // busy and has a finish time of 0. //----------------------------------------------------- serverType() { finishTime = 0; busy = false; } //----------------------------------------------------- // Set the client that is being served by this server. // When the client is set, the server finish time is // calculated (client exit time plus service time). The // server is also marked as busy. // // In parameter: newClient //----------------------------------------------------- void setClient(clientType newClient) { myClient = newClient; finishTime = myClient.exitTime + myClient.serviceTime; busy = true; } //----------------------------------------------------- // Get the client that is being served by this server //----------------------------------------------------- clientType getClient() { return myClient; } //----------------------------------------------------- // Reset this server to its default state. //----------------------------------------------------- void reset() { finishTime = 0; busy = false; } //----------------------------------------------------- // Return true if this server is busy otherwise return // false. A default server is not busy. This server // becomes busy when its client is set. Once the client // is set, the server will be busy until the server is // cleared. //----------------------------------------------------- bool isBusy() { return busy; } //----------------------------------------------------- // Get the finish time for the current client. You // should check to see if the server is busy before // getting the finish time. If the server is not busy, // this function will return a finish time of zero. //----------------------------------------------------- int getFinishTime() { return finishTime; } }; //--------------------------------------------------------- // Get the simulation parameters from the user. // // Out parameters: timeUnit, duration, arrivalProbability, // serviceTimeLow, serviceTimeHigh //--------------------------------------------------------- void getSimulationParameters(string& timeUnit, int& duration, double& arrivalProbability, int& serviceTimeLow, int& serviceTimeHigh) { cout << "You will be asked to enter the unit of time (e.g., second, minute, etc.), the\n"; cout << "duration of the simulation (in the specified units of time), the probability\n"; cout << "that a new client will arrive during any given unit of time, the minimum time\n"; cout << "needed to serve a client and the maximum time needed to serve a client (both in\n"; cout << "the specified time units).\n\n"; cout << "Enter the unit of time: "; cin >> timeUnit; cout << endl; cout << "Enter the number of "; cout << timeUnit << "s (an integer): "; cin >> duration; cout << endl; cout << "Enter the arrival probability: "; cin >> arrivalProbability; cout << endl; cout << "Enter the minimum service time in "; cout << timeUnit << "s (an integer): "; cin >> serviceTimeLow; cout << endl; cout << "Enter the maximum service time in "; cout << timeUnit << "s (an integer): "; cin >> serviceTimeHigh; cout << endl; } //--------------------------------------------------------- // Run a simulation of a waiting line using the specified // number of time units, service time generator, and client // arrival indicator. // // Return the number of clients served, the averge waiting // time, the maximum waiting time, and the total time // required to service all clients entering the waiting // line prior to the expiration of the specified number of // time units. // // In Parameters: duration, serviceTime, newClient // // Out Parameters: numClients, avgWait, maxWaitTime, // totalTime //--------------------------------------------------------- void runSimulation(int numTimeUnits, int serviceTimeLow, int serviceTimeHigh, double arrivalProbability, int& numClients, double& avgTime, int& maxWaitTime, int& totalTime) { } //--------------------------------------------------------- // Display the simulation parameters, the time required to // serve all clients, the number of clients, the average // waiting time and the maximum waiting time. // // In Parameters: timeUnit, targetDuration, actualDuration, // arrivalProb, minTime, maxTime, // numClients, avgWaitTime, maxWaitTime //--------------------------------------------------------- void displaySimulationResults (string timeUnit, int targetDuration, int actualDuration, double arrivalProb, int minTime, int maxTime, int numClients, double avgWaitTime, int maxWaitTime) { cout << fixed << setprecision(3); cout << endl << endl; cout << "-------------------------------------------------\n"; cout << " S I M U L A T I O N R E S U L T S\n"; cout << "-------------------------------------------------\n\n"; cout << "Scheduled duration of simulation: " << targetDuration << ' ' << timeUnit << 's' << endl; cout << " Actual duration of simulation: " << actualDuration << ' ' << timeUnit << 's' << endl << endl; cout << "Probability new client would arrive at any given " << timeUnit << ": " << arrivalProb << endl << endl; cout << "Service times: " << minTime << " to " << maxTime << ' ' << timeUnit << "s (inclusive)" << endl << endl; cout << "Number of clients served: " << numClients << endl << endl; cout << "Average wait time: " << avgWaitTime << ' ' << timeUnit << 's' << endl; cout << "Maximum wait time: " << maxWaitTime << ' ' << timeUnit << 's' << endl; } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { string unitOfTime; // Unit of time int duration; // Desired duration in time units int actualDuration; // Actual duration in time units double arrivalProb; // Probability new client will arrive // at any given unit of time int serviceMin; // Minimum possible service time int serviceMax; // Maximum possible service time int numClients; // Number of clients served double avgWaitTime; // Average wait time int maxWaitTime; // Maximum wait time getSimulationParameters(unitOfTime, duration, arrivalProb, serviceMin, serviceMax); srand(time(NULL)); runSimulation(duration, serviceMin, serviceMax, arrivalProb, numClients, avgWaitTime, maxWaitTime, actualDuration); displaySimulationResults(unitOfTime, duration, actualDuration, arrivalProb, serviceMin, serviceMax, numClients, avgWaitTime, maxWaitTime); return 0; }