In: Computer Science
Round Robin Simulation Description: Write a program in c++ that utilizes STL queue (or your own queue implementation) that simulates the round robin process scheduling algorithm. WITH COMMENTS Requirement: - write a class Process that holds following integer information: id, arrival_time, time_needed, finished_time. - read and initiate 5 Process objects from associated file (robin. txt) - file format: id, arrival_time, time_needed - once a process is finished, mark its finished_time accordingly. - CPU time frame: 4. - utilize a queue for process scheduling. - store finished processes into a vector (first finished, first stored) - output: print information about finished processes from the vector, in the finished order.
robin.txt:
1 0 10
2 1 12
3 5 6
4 6 10
5 7 4
Here we are using round robin process scheduling algorithm.Its basically preemptive process scheduling algorithm.
A preemptive process enables the job scheduler to pause a process under execution and move to the next process in the job queue.The job scheduler saves current state of the job and moves to the another job in the queue.Here every process gets an equal time of execution here given as CPU time frame.Therefore, no process will be able to hold the CPU for a longer time period.
The round-robin job scheduling algorithm is, therefore, used in a multi-user, time-sharing or multi-tasking operating systems.
Parameters we have:
Read Processes: 5
CPU time frame(Quantum): 4.
Output:Print the information about finished processes.
at = Arrival time,
bt = Burst time(time_needed),
time_quantum= Quantum time(Cpu time frame)
tat = Turn around time,
wt = Waiting time
Code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
int i,n,time,remain,temps=0,time_quantum;
int wt=0,tat=0;
cout<<"Enter the total number of
process:"<<endl;
cin>>n;
remain=n;
vector<int>at(n);
vector<int>bt(n);
vector<int>rt(n);
cout<<"Enter the Arrival time, time needed
/burst time for All the processes"<<endl;
for(i=0;i<n;i++)
{
cin>>at[i];
cin>>bt[i];
rt[i]=bt[i];
}
cout<<"Enter the value of time QUANTUM/Cpu
time frame:"<<endl;
cin>>time_quantum;
cout<<"\n\nProcess\t:Turnaround Time:Waiting
Time\n\n";
for(time=0,i=0;remain!=0;)
{
if(rt[i]<=time_quantum
&& rt[i]>0)
{
time +=
rt[i];
rt[i]=0;
temps=1;
}
else if(rt[i]>0)
{
rt[i] -=
time_quantum;
time +=
time_quantum;
}
if(rt[i]==0 &&
temps==1)
{
remain--;
printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
cout<<endl;
wt +=
time-at[i]-bt[i];
tat +=
time-at[i];
temps=0;
}
if(i == n-1)
i=0;
else if(at[i+1] <= time)
i++;
else
i=0;
}
cout<<"Average waiting time
"<<wt*1.0/n<<endl;
cout<<"Average turn around time
"<<tat*1.0/n<<endl;;
return 0;
}
Output:
we ask the user to enter the number of processes and arrival time and burst time for each process. We then calculate the waiting time and the turn around time using the round-robin algorithm.