Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include #include #include #include #include #include #include #include using na

ID: 3843078 • Letter: #

Question

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;


//Create a struct or class to represent a customer.
//Include these data members: arrival time, service time, and completion time, all as whole numbers.
struct customer
{
   int arrival_time;
   int service_time;
   int completion_time;
   char ID;
   customer() : service_time(0), completion_time(0), ID(' '){}
};

struct serviceEvent
{
   int clock_time;
   int service_number;
   bool operator <(const serviceEvent& a) const { return (clock_time > a.clock_time); }
};

int getRam(double average)
{
   int req = 0;
   double num_of_requests = exp(-average);
   for (double random = (double)rand() / RAND_MAX; (random -= num_of_requests) > 0.0; num_of_requests *= average / (double)++req);
   return req;
}
int main()
{

   srand((float)time(0));
   rand();

   // read the input values from a text file, one per line, in the order specified above.
   ifstream fin;
   fin.open("test.txt");
   if (!fin.good())
       throw "Failed to read file";
  
   int num_servers;
   float averageArr;
   int max_wtime;
   int min_sInterval;
   int max_sInterval;
   int clock_time;

   fin >> num_servers;
   fin.ignore(1000, 10);
   fin >> averageArr;
   fin.ignore(1000, 10);
   fin >> max_wtime;
   fin.ignore(1000, 10);
   fin >> min_sInterval;
   fin.ignore(1000, 10);
   fin >> max_sInterval;
   fin.ignore(1000, 10);
   fin >> clock_time;
   fin.ignore(1000, 10);
  
   //output read results
   cout << "number of servers: " << num_servers << endl;
   cout << "customer arrival rate: " << averageArr << " per minute, for 50 minutes" << endl;
   cout << "maximum queue length: " << max_wtime << endl;
   cout << "minimum service time: " << min_sInterval << endl;
   cout << "maximum service time: " << max_sInterval << endl;

   // declare and create and assign arrays and queues to be used in the solution
   queue cQueue;
   queue sDone;
   priority_queue sEvent;
   customer* servers = new customer[num_servers];
   bool* status = new bool[num_servers];
   string buf;
   int n = 0;
   int turn_away_c;

   //initiallize bool status
   for (int i = 0; i < num_servers; i++)
       status[i] = false;

   // the clock time loop
   for (int time = 0;; time++) // the clock time
   {
       // handle all service events scheduled to complete at this clock time
       //while (event queue is not empty AND clock time of the top service event equals the current clock time)
       while (!sEvent.empty() && sEvent.top().clock_time == time)
       {
  
      
       //remove the top service event from the queue//determine the server associated with the removed service event
           serviceEvent temp = sEvent.top();
           sEvent.pop();
           int sNumber = temp.service_number;
           sDone.push(servers[num_servers]);
           //set this server's current customer's completion time to the current clock time
           //copy this server's current customer to the served customers queue
           //set this server to idle
           servers[num_servers].ID = ' ';
           status[num_servers] = false;

       }
          
          

           // handle new arrivals -- can be turned away if wait queue is at maximum length!
           //if time is less than the time at which new arrivals stop
       if (time < max_wtime)
       {
           int random = getRam(averageArr);    //get the #of of arrivals from the Poisson process(a whole number >= 0) --see the lecture topic notes!
           for (int i = 0; i < random; i++)    //for each new arrival
           {//   if the wait queue is full
               if (cQueue.size() >= max_wtime)
                   turn_away_c++; //   "turn away the customer" by adding one to the count of turned away customers
               else   //else
               {
                   customer newc; //create a new customer object, setting its arrival time equal to the current clock time
                   newc.arrival_time = time;
                   newc.ID = (char)('A' + (n++ % 26));//copy the new customer to the wait queue
                   cQueue.push(newc); // for idle servers, move customer from wait queue and begin service
               }
           }
       }
          
       for (int i = 0; i < num_servers;i++)   //for each server
       {
           //if (server is idle AND the wait queue is not empty)
           if (!status[i] && !cQueue.empty())
           {
               customer temp = cQueue.front();
               cQueue.pop(); //remove top customer from wait queue
               servers[i] = temp; //copy it to the server array
               temp.service_time = time; //set the copied customer's service time to the current clock time
               status[i] = false;
               int ran = rand() % 6; //use random number generation to determing the service time interval for the customer
               serviceEvent sTemp; //create a new service event for the server, for the current clock time PLUS the service time interval
               sTemp.clock_time = time + ran;
               sTemp.service_number = i;
               sEvent.push(sTemp); //add the service event to the event queue
           }
       }
      
       // output the summary
       cout << "Time: " << time << endl;   //output the current time
                                                                           //   output a heading for the visual prepresentation of the servers and the wait queue
       cout << "-------------------------------------------" << endl;
       cout << "line now serving wait queue" << endl;
       cout << "-------------------------------------------" << endl;
       //for each server
       for (int i = 0; i < num_servers; i++)
       {
           cout << setw(5) << i;
           cout << setw(10) << servers[i].ID; //output the server's index number (zero-based)
                                                                               //       show the ID of the customer being served by that server(blank if idle)
           if (i == 0)//       for server 0 only, show the IDs of customers in the wait queue
               cout << setw(15) << servers[i].ID;
           cout << endl;
       }
       cout << "-------------------------------------------" << endl;
  
       // summarize performance
       float sum = 0;
       float sum_Count = 0;
       float sum_Time = 0;

       //if time is greater than zero
       if (time != 0)
       {
           //iterate over the served customers to get the average time between arrival and completion
           queue average = sDone;
           sum_Count = average.size();
           //and if any, output the average of their "completion time" minus their "arrival time"
           while (!average.empty())
           {
               sum_Time += (average.front().completion_time - average.front().arrival_time);
               average.pop();
           }
           sum = sum_Time / sum_Count;
       }

           if (sum_Count != 0)
               cout << "Avg time: " << sum;
           else
               cout << "Average Time: 0" << endl;
           if (turn_away_c != 0)
               //output the rate of customers turned away as total turned away divided by the current time
               cout << "Turned away per minute: " << (float)turn_away_c / time << endl;
           else
               cout << "Turned away permitue: 0" << endl;
          
              
          
      
           // if the user wants to quit, break out of this loop
           cout << "Press ENTER to continue, X-ENTER to exit..." << endl;
           getline(cin, buf);
           if (buf == "X" || buf == "x")
               break;
           else
               continue;
   }
   //clear memory
   delete[] servers;
   delete[] status;
}

This program now has runtime error: terminating with uncaught exception of type char const* abort trap:6

test.txt should be like this:

4

2.5

8

3

10

Explanation / Answer

#include<bits/stdc++.h>
#include<time.h>
using namespace std;
int number,id=0,quantum;
struct st
{
int id,start,finish,arrival_time,service_time,service_time2,turnaround_time,waiting_time,last_run,priority;
}job[50];
vector<int>v,v2;
void sort_by_arrival_time();
void sort_by_service_time();
void sort_by_priority();
void drawline(int,int);
void FCFS(void);
void SJF(void);
void SRTF(void);
void Priority_scheduling(void);
void Round_Robin(void);
void show_diagram();
int show_output();
void gotoxy(int x,int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
void sleep(unsigned int ms)
{
clock_t goal=ms+clock();
while(goal>clock());
}
void intput1(void)
{
int x=5,y=10,i;
gotoxy(x+3,y); cout<<"Process";
gotoxy(x+15,y); cout<<"Arrival time";
gotoxy(x+30,y); cout<<"Service time";
drawline(11,45);
x=8;y=11;
for(i=1;i<=number;i++)
{
gotoxy(x,y+i); cout<<"P"<<i;
gotoxy(x+15,y+i); job[i].arrival_time = rand()%11;cout<<job[i].arrival_time;
gotoxy(x+30,y+i); job[i].service_time = rand()%21;cout<<job[i].service_time;
job[i].id=i;
job[i].service_time2=job[i].service_time;
}
}
void intput2(void)
{
int x=5,y=10,i;
gotoxy(x+3,y); cout<<"Process";
gotoxy(x+15,y); cout<<"Service time";
gotoxy(x+30,y); cout<<"Priority";
drawline(11,45);
x=8;y=11;
for(i=1;i<=number;i++)
{
gotoxy(x,y+i); cout<<"P"<<i;
gotoxy(x+15,y+i); job[i].service_time = rand()%21;cout<<job[i].service_time;
gotoxy(x+30,y+i); job[i].priority = rand()%11;cout<<job[i].priority;
job[i].id=i;
job[i].service_time2=job[i].service_time;
}
}
void intput3(void)
{
int x=5,y=10,i;
gotoxy(x+3,y); cout<<"Process";
gotoxy(x+15,y); cout<<"Service time";
drawline(11,30);
x=8;y=11;
for(i=1;i<=number;i++)
{
gotoxy(x,y+i); cout<<"P"<<i;
gotoxy(x+15,y+i); job[i].service_time = rand()%21;cout<<job[i].service_time;
job[i].id=i;
job[i].service_time2=job[i].service_time;
}
cout<<"Enter time quantum: ";
quantum = 3;cout<<quantum; //given in the question to assume quantum or time slice to be 3 seconds
}
void intput4(void)
{
int x=5,y=10,i;
gotoxy(x+3,y); cout<<"Process";
gotoxy(x+15,y); cout<<"Service time";
drawline(11,30);
x=8;y=11;
for(i=1;i<=number;i++)
{
gotoxy(x,y+i); cout<<"P"<<i;
gotoxy(x+15,y+i); job[i].service_time = rand()%21;cout<<job[i].service_time;
job[i].id=i;
job[i].service_time2=job[i].service_time;
}
}
void ganttchart1(int y)
{
cout<<endl<<" Gantt Chart:"<<endl;
drawline(y+3,70);
int i;
cout<<" ";
for(i=1;i<=number;i++)
{
cout<<"| "<<job[i].start<<" | P"<<job[i].id<<" ";
}
cout<<"| "<<job[number].finish<<" |"<<endl;
drawline(y+5,70);
}
void ganttchart2(int y)
{
cout<<endl<<" Gantt Chart:"<<endl;
drawline(y+3,70);
int i,cnt;
cout<<" ";
cnt=0;
for(i=0;i<v.size();i++)
{
if(i==0||v[i]!=v[i-1])
{
cout<<"| "<<cnt<<" | P"<<job[v[i]].id<<" ";
}
cnt++;
}
cout<<"| "<<cnt<<" |";
drawline(y+5,70);
}
void ganttchart3(int y)
{
cout<<endl<<" Gantt Chart:"<<endl;
drawline(y+3,85);
int i,cnt,d;
cout<<" ";
cnt=0;d=0;
for(i=0;i<v2.size();i++)
{
if(i==0||v2[i]!=v2[i-1]||d>=quantum)
{
d=0;
cout<<"| "<<cnt<<" | P"<<job[v2[i]].id<<" ";
}
d++;
cnt++;
}
cout<<"| "<<cnt<<" |";
drawline(y+5,85);
}
int main()
{
int i,x=5,y=10,choice;


while(1)
{
system("clear"); //change made by reyan
v.clear();
v2.clear();
cout<<"Job Scheduling algorithm list:"<<endl;
cout<<" 1. First Come First Served (FCFS)."<<endl;
cout<<" 2. Shortest Job First (SJF)."<<endl;
cout<<" 3. SRTF"<<endl;
cout<<" 4. Priority Scheduling"<<endl;
cout<<" 5. Round Robin Scheduling (RR)."<<endl;
cout<<" 0. Exit."<<endl;
cout<<"Your Choice: ";
cin>>choice;
if(choice==0)
break;
else if(choice<0||choice>5)
{
cout<<" Sorry wrong choice. Please try again."<<endl;
sleep(1000000);
continue;
}
cout<<"Enter total number of service:";
cin>>number;
if(choice==1)
{
intput1(); sleep(5000);
FCFS();
system("clear");
x=20;y=2;
gotoxy(x,y);
cout<<"First Come First Served (FCFS) job scheduling."<<endl;
y=show_output();
ganttchart1(y);
}
else if(choice==2)
{
intput4();sleep(5000);
SJF();
system("clear");
x=20;y=2;
gotoxy(x,y);
cout<<"Shortest Job First (SJF) job scheduling."<<endl;
y=show_output();
ganttchart1(y);
}
else if(choice==3)
{
intput1();sleep(5000);
SRTF();
system("clear");
x=15;y=2;
gotoxy(x,y);
cout<<"Shortest Remaining Time First (SRTF) job scheduling."<<endl;
y=show_output();
ganttchart2(y);
}
else if(choice==4)
{
intput2();sleep(5000);
Priority_scheduling();
system("clear");
x=30;y=2;
gotoxy(x,y);
cout<<"Priority Scheduling."<<endl;
y=show_output();
ganttchart1(y);
}
else if(choice==5)
{
intput3();sleep(5000);
Round_Robin();
system("clear");
x=25;y=2;
gotoxy(x,y);
cout<<"Round Robin job scheduling."<<endl;
y=show_output();
ganttchart3(y);
}
cout<<endl<<"Press [Enter] to continue............";
char ch;
getchar();
scanf("%c",&ch);
}
}
void drawline(int y,int len)
{
int i,x=5;
gotoxy(x,y);
for(i=0;i<len;i++)
{
cout<<"-";
}
cout<<endl;
}
int show_output(void)
{
int x=5,y=3,n;
gotoxy(x+5,y); cout<<"Process";
gotoxy(x+15,y); cout<<"Waiting time";
gotoxy(x+30,y); cout<<"Turnaround time";
gotoxy(x+50,y); cout<<"Finish time";
drawline(4,70);
int i;y=4;x=7;
for(i=1;i<=number;i++)
{
gotoxy(x+5,y+i); cout<<"P"<<job[i].id;
gotoxy(x+15,y+i); cout<<job[i].waiting_time;
gotoxy(x+30,y+i); cout<<job[i].turnaround_time;
gotoxy(x+50,y+i); cout<<job[i].finish;
}
drawline(y+number+1,70);
return y+number+1;
}
void FCFS(void)
{
int i,j,cpu_free_at;
sort_by_arrival_time();
cpu_free_at=0;
for(i=1;i<=number;i++)
{
if(cpu_free_at<=job[i].arrival_time)
{
job[i].waiting_time=0;
job[i].start=job[i].arrival_time;
job[i].finish=job[i].start+job[i].service_time;
cpu_free_at=job[i].finish;
job[i].turnaround_time=job[i].service_time;
}
else
{
job[i].waiting_time=cpu_free_at-job[i].arrival_time;
job[i].start=cpu_free_at;
job[i].finish=job[i].start+job[i].service_time;
cpu_free_at=job[i].finish;
job[i].turnaround_time=job[i].waiting_time+job[i].service_time;
}
}
}
void SJF(void)
{
int i,j,cpu_free_at;
sort_by_service_time();
cpu_free_at=0;
for(i=1;i<=number;i++)
{
job[i].waiting_time=cpu_free_at;
job[i].start=cpu_free_at;
job[i].finish=job[i].start+job[i].service_time;
cpu_free_at=job[i].finish;
job[i].turnaround_time=job[i].waiting_time+job[i].service_time;
}
}
void SRTF(void)
{
int i,j;
for(i=1;i<=number;i++)
job[i].last_run=-1;
for(i=0;i<=1000;i++)
{
int idx=-1;
for(j=1;j<=number;j++)
{
if(job[j].arrival_time<=i&&job[j].service_time>0)
{
idx=j;
break;
}
}
if(idx!=-1)
{
for(j=1;j<=number;j++)
{
if(job[j].arrival_time<=i&&job[j].service_time>0&&job[j].service_time<job[idx].service_time)
{
idx=j;
}
}
if(job[idx].last_run==-1)
{
job[idx].waiting_time=i-job[idx].arrival_time;
job[idx].last_run=i;
job[idx].service_time--;
if(job[idx].service_time==0)
job[idx].finish=i+1;
}
else
{
job[idx].waiting_time+=i-job[idx].last_run-1;
job[idx].last_run=i;
job[idx].service_time--;
if(job[idx].service_time==0)
job[idx].finish=i+1;
}
v.push_back(idx);
}
}
for(i=1;i<=number;i++)
{
job[i].turnaround_time=job[i].waiting_time+job[i].service_time2;
}
}
void Priority_scheduling(void)
{
int i,j,cpu_free_at;
sort_by_priority();
cpu_free_at=0;
for(i=1;i<=number;i++)
{
if(cpu_free_at<=job[i].arrival_time)
{
job[i].waiting_time=0;
job[i].start=job[i].arrival_time;
job[i].finish=job[i].start+job[i].service_time;
cpu_free_at=job[i].finish;
job[i].turnaround_time=job[i].service_time;
}
else
{
job[i].waiting_time=cpu_free_at-job[i].arrival_time;
job[i].start=cpu_free_at;
job[i].finish=job[i].start+job[i].service_time;
cpu_free_at=job[i].finish;
job[i].turnaround_time=job[i].waiting_time+job[i].service_time;
}
}
}
void Round_Robin(void)
{
deque<int>Q;
int i,cnt;
for(i=1;i<=number;i++)
{
Q.push_back(i);
job[i].last_run=-1;
job[i].waiting_time=0;
}
cnt=0;
while(!Q.empty())
{
i=Q.front();
Q.pop_front();
if(job[i].last_run==-1)
{
job[i].waiting_time=cnt;
}
else
{
job[i].waiting_time+=cnt-job[i].last_run;
}
if(job[i].service_time>quantum)
{
job[i].service_time-=quantum;
job[i].last_run=quantum+cnt;
Q.push_back(i);
cnt+=quantum;
for(int j=0;j<quantum;j++)
v2.push_back(i);
}
else
{
job[i].finish=cnt+job[i].service_time;
cnt+=job[i].service_time;
for(int j=0;j<job[i].service_time;j++)
v2.push_back(i);
}
}
for(i=1;i<=number;i++)
{
job[i].turnaround_time=job[i].waiting_time+job[i].service_time2;
}
}
void sort_by_arrival_time(void)
{
int i,j;
st temp;
for(i=1;i<=number;i++)
{
for(j=i+1;j<=number;j++)
{
if(job[i].arrival_time>job[j].arrival_time)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
else if(job[i].arrival_time==job[j].arrival_time)
{
if(job[i].id>job[j].id)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
}
}
}
}
void sort_by_service_time(void)
{
int i,j;
st temp;
for(i=1;i<=number;i++)
{
for(j=i+1;j<=number;j++)
{
if(job[i].service_time>job[j].service_time)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
else if(job[i].service_time==job[j].service_time)
{
if(job[i].arrival_time<job[j].arrival_time)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
}
}
}
}
void sort_by_priority(void)
{
int i,j;
st temp;
for(i=1;i<=number;i++)
{
for(j=i+1;j<=number;j++)
{
if(job[i].priority>job[j].priority)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
else if(job[i].priority==job[j].priority)
{
if(job[i].id>job[j].id)
{
temp=job[i];
job[i]=job[j];
job[j]=temp;
}
}
}
}
}