Question

C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read...

C++ -- Event processing simulation using a transaction queue

Hi! it is queue simulation

please read the instructions, write codes, and explain the code with comments. Thank you

Transactions enter the system and are stored in a queue. Each transaction represents some work that needs to be accomplished. Servers exist which process transactions. Servers take transactions off the queue and process them.

you’re building the simulation framework. The idea is that somebody would take your framework, and add the specifics for whatever type of system it was going to simulate. For example, somebody might be simulating a hospital: it has doctors (servers) processing patients (transactions) waiting in the waiting room (queue).

Some things are common to all types of servers and transactions and queues, no matter what types of systems they’re simulating.

  • Transactions have a unique identifier, and have some amount of work associated with them. The amount of work varies from transaction to transaction.

  • Servers are the entities capable of performing work. Some servers are faster than others (they can process more work in the same amount of time). Any given server is either performing work (busy) or is free, waiting for a transaction to arrive.

  • Queues are data structures in which new elements are added to the back, and removed from the front. A queue can grow and shrink in size, as elements can be added and removed at different rates.

    Simulations are driven by a clock. Each time the clock ticks, something happens. For example, on tick one, a transaction may enter the system. Let’s say that transaction requires 10 units of work to complete. The transaction is picked up by a free server. The server is capable of performing two units of work per clock tick. The server can therefore determine that the transaction will require 5 clock ticks to complete. It the server knows what the time is now, it can deduce when the transaction will finish. After five clock ticks, the server removes the transaction, and looks in the queue for the next one. One may not exist, in which case the server waits.

    Part 1 - Definition for a Transaction ADT

    Use a struct to define the data members of a Transaction. Transactions are stored in the queue. A transaction has a transaction ID and some number of work units needed to process it. The data members are:

    transaction ID - string
    work units - int

    Part 2 – Definition for a Server ADT

Use a struct to define the data members of a Server. A server has a flag telling whether it’s free or busy, and its speed in processing units per clock tick. When a transaction is started by a server, the time that transaction will end is recorded. The data members are:

free or busy - bool
transaction ID, if busy - string
the speed of the server, in work units per clock tick - int
time that the current transaction will end - int

Part 3 - Class definition for the Queue ADT

Create a class named Queue. A queue will contain some number of elements, each element representing one transaction. Your queue should be templated to allow for any data type.

Use Malik’s queue logic for implementing a queue using an array. See the summary sheet and chapter 8. The user will instantiate a queue object via:

Queue queueName (n);

where queueName is the name the user chose for the object, and n is the number of elements in the array used to hold the queue.

Part 4 – Test your queue

Code up your own main to test your queue functions before you go on to part 5. Use a data type of Transaction. Your main should test all possibilities. Add some elements, delete some elements, dump out the queue and make sure the data structure matches what it should be. Try filling the queue, then overfilling it. Try emptying it, and then try getting another element. Keep your testing main in case you run into problems later on. As I grade your code, I will run my own queue testing program with your queue, so make sure your testing is thorough!

Part 5 - The Simulation

Code an event simulator, using one server. There are countless ways to design an event processing simulator. For a system with one server, try the following:

read from user:

number of clock ticks
probability of a transaction arriving on a clock tick
low and high ranges for processing units per transaction
number of work units per clock tick for the server

clock = 0
server busy = false
while (clock < clock ticks)
  if we’re adding a transaction to queue
   generate transaction
  add to queue
     endif
if server not busy
   if available transaction
   put transaction into server
   put ending time into server
   mark server busy
     endif
   endif
   if server busy and ending time = now
   remove transaction, record statistics
   create transaction log record
mark server free
   endif
   clock ++
end while

display
number of completed transactions
number of processing units used
was there a transaction in progress when clock expired?
number of unprocessed transactions in queue
total processing time required by those unprocessed transactions

Your transaction log is a file that details the transactions that were fully processed by your simulation. Each time a transaction finishes, create a log record containing the following information:

transaction ID
time transaction started
time transaction ended
work units consumed

Name the file a6log.txt and store it someplace on your hard drive. You’ll find that the transaction log will provide valuable information for debugging purposes.

When I run your code, I will look in your transaction log to make sure that the log matches the simulation activity and statistics I see on the console.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

QueueADT.h

==============================

#ifndef QUEUE_ADT_H
#define QUEUE_ADT_H

template <typename T>
class Queue {
private:
   T* arr; // array of elements
   int capacity; // maximum capacity of queue
   int size; // number of element in queue
public:
   Queue(int capacity); // constructor
   ~Queue(); // destructor
   void add(T elem); // add given element to end of queue
   T remove(); // remove and returns first element from queue
   int length(); // returns total number of transactions in queue
};

template <typename T>
Queue<T>::Queue(int capacity) {
   this->capacity = capacity;
   arr = new T[capacity];
   size = 0;
}

template <typename T>
Queue<T>::~Queue() {
   size = 0;
   delete[] arr;
}

template <typename T>
void Queue<T>::add(T elem) {
   if (size < capacity) {
       arr[size] = elem;
       size++;
   }
}

template <typename T>
T Queue<T>::remove() {
   T elem;
   if (size > 0) {
       elem = arr[0];
       for (int i = 0; i < size - 1; i++) {
           arr[i] = arr[i + 1];
       }
       size--;
   }
   return elem;
}

template <typename T>
int Queue<T>::length() {
   return size;
}

#endif // !QUEUE_ADT_H

=============================

ServerADT.h

=============================

#ifndef SERVER_ADT_H
#define SERVER_ADT_H
#include <iostream>

struct Server {
   bool is_busy;
   std::string transaction_ID = "";
   int speed = 0;
   int remaining_time = 0;
};

#endif // !SERVER_ADT_H

=============================

TransactionADT.h

=============================

#ifndef TRANSACTION_ADT_H
#define TRANSACTION_ADT_H
#include <iostream>

struct Transaction {
   std::string transaction_ID = "";
   int work_units = 0;
};

#endif // !TRANSACTION_ADT_H

============================

main.cpp

============================

#include "QueueADT.h"
#include "ServerADT.h"
#include "TransactionADT.h"
#include <string>
#include <fstream>
using namespace std;

int main() {

   // get simulation data from user
   cout << "How many clock ticks to simulate? " << endl;
   int num_of_ticks;
   cin >> num_of_ticks;
   cout << "Enter probability of transaction arriving: ";
   double probability;
   cin >> probability;
   cout << "Minimum work unit for transaction: ";
   int min_work_unit;
   cin >> min_work_unit;
   cout << "Maximum work unit for transaction: ";
   int max_work_unit;
   cin >> max_work_unit;
   cout << "Server speed(units/clock tick): ";
   int server_speed;
   cin >> server_speed;

   // create a log file
   ofstream log;
   log.open("a6log.txt");
   // number of transaction completed
   int num_of_completed = 0;
   // create queue
   Queue<Transaction> queue(10); // queue of size 10
   // create server
   Server server;
   server.is_busy = false; // server is not busy at start
   server.remaining_time = 0;
   server.speed = server_speed;
   server.transaction_ID = "Server_12345";
   // create transaction ID in increasing order for transactions
   int ID = 1000;
   // start simulation
   int clock = 0;
   Transaction inProcess; // transaction in process
   while (clock < num_of_ticks) {
       // at start of clock tick a transaction have a chance to arrive at server
       int chance = rand() % 100; // random chance from 0 to 99
       // create transaction
       if (chance / 100.0 > (1 - probability)) {
           Transaction t;
           t.transaction_ID = "Trans_" +to_string(ID);
           ID++;
           t.work_units = (rand() % (max_work_unit - min_work_unit)) + min_work_unit;
           // add transaction to queue
           queue.add(t);
       }
       // if server is free it will process transaction
       if (!server.is_busy) {
           inProcess = queue.remove();
           if (inProcess.transaction_ID != "") {
               server.is_busy = true;
               // create log
               log << "Transaction: " << inProcess.transaction_ID << endl;
               log << "start: " << clock << endl;
               server.remaining_time = inProcess.work_units;
           }
       }
       // if server is busy it keep processing transactions
       if (server.is_busy) {
           if (server.remaining_time > server.speed) {
               server.remaining_time -= server.speed;
           }
           // if server finish processing it will call for next transaction in queue
           else {
               // create log
               log << "end: " << clock << endl;
               log << "work unit consumed: " << inProcess.work_units << endl;
               int unconsumed_time = server.speed - server.remaining_time;
               num_of_completed++;
               server.remaining_time = 0;
               server.is_busy = false;
               inProcess = queue.remove();
               if (inProcess.transaction_ID != "") {
                   server.is_busy = true;
                   // create log
                   log << "Transaction: " << inProcess.transaction_ID << endl;
                   log << "start: " << clock << endl;
                   server.remaining_time = inProcess.work_units;
                   server.remaining_time -= unconsumed_time;
               }
           }
       }
       clock++; // end of a tick
   }// finish simulation
  
   // display statics on console
   cout << endl << "Total transaction completed: " << num_of_completed << endl;
   cout << "Number of processing unit used: 1" << endl;

   // check if there is unfinished transaction
   if (server.is_busy) {
       log << "Unfinished transaction: " << inProcess.transaction_ID << endl << endl;
       cout << "Unfinished transaction: " << inProcess.transaction_ID << endl;
   }
   log << "Total transaction completed: " << num_of_completed << endl;
   // close the log file
   log.close();

   cout << "Transactions in queue: " << queue.length() << endl;
   cout << "Total processing time required for unprocessed transaction: ";

   int total_unit = 0;
   if (server.is_busy) {
       total_unit += server.remaining_time;
   }
   else {
       inProcess = queue.remove();
   }
   while (inProcess.transaction_ID != "") {
       total_unit += inProcess.work_units;
       inProcess = queue.remove();
   }
   int time = total_unit / server.speed;
   if (total_unit % server.speed != 0) {
       time++;
   }
   cout << time << endl;

   return 0;
}

let me know if you have any problem or want any modification in code. thank you.

Add a comment
Know the answer?
Add Answer to:
C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi! it is c++ queue simulation please read the instructions, write codes, and explain the code...

    Hi! it is c++ queue simulation please read the instructions, write codes, and explain the code with comments. Thank you Transactions enter the system and are stored in a queue. Each transaction represents some work that needs to be accomplished. Servers exist which process transactions. Servers take transactions off the queue and process them. you’re building the simulation framework. The idea is that somebody would take your framework, and add the specifics for whatever type of system it was going...

  • Implement the event-driven simulation of a bank that this chapter described. A queue of arrival events...

    Implement the event-driven simulation of a bank that this chapter described. A queue of arrival events will represent the line of customers in the bank. Maintain the arrival events and departure events in an ADT event list, sorted by the time of the event. Use a reference-based implementation for the ADT event list The input is a text file of arrival and transaction times. Each line of the file contains tiie arrival time and required transaction time for a customer....

  • Consider a single-server queueing system with arrival and service details as: Interarrival times: 3, 2, 6,...

    Consider a single-server queueing system with arrival and service details as: Interarrival times: 3, 2, 6, 2, 4, 5 Service times: 2, 5, 5, 8, 4, 5 Prepare a table show below for the given data. Stop simulation when the clock reaches 20. Write a Java program, to implement this single-server queueing system, print out the table shown below: You should create a future event list in your Java code, and print out the contents of FE list in each...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

  • C++ Lists, and a little queuing theory The MegaMicroMart (a subsidiary of Fly-By-Night Industries) is planning...

    C++ Lists, and a little queuing theory The MegaMicroMart (a subsidiary of Fly-By-Night Industries) is planning their next Big Small-Box Store. They want to ensure good customer service, and you've been hired to do some simulations to advise them on how to set up their checkout lines. This is a discrete-time simulation. This means that for our purposes, time advances in discrete 'ticks'. Each tick, everyone who's going to arrive arrives all at once, while at the same time everyone...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bot...

    C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bottom: Implementing Load Balancing Summary: In this homework, you will be implementing the main muti-threaded logic for doing batch based server load balancing using mutexes Background In this assignment you will write a batch-based load balancer. Consider a server which handles data proces- sing based on user requests. In general, a server has only a fixed set of hardware resources that it can...

  • Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I...

    Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I have so far just basically adds customers to the queue and prints when they arrive. I am struggling with how to implement a way of keeping track of when a given customer finishes(I will attach what I have so far). I had already created queue and node classes (with headers and cpp files) that I modified in my attempt. I would be very grateful...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • TASK Read the Regional gardens case study document before attempting this assignment. Background: You have been...

    TASK Read the Regional gardens case study document before attempting this assignment. Background: You have been employed by Regional Gardens as their first Chief Information Officer (CIO). You have been tasked by the Board to conduct a review of the company’s risks and start to deploy security policies to protect their data and resources. You are concerned that the company has no existing contingency plans in case of a disaster. The Board indicated that some of their basic requirements for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT