Question

Round Robin Simulation in C++ using queue Description: Write a program that utilizes STL queue that...

Round Robin Simulation in C++ using queue

Description: 
Write a program that utilizes STL queue that simulates the round robin process scheduling algorithm. 

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 (round_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. 

round_robin.txt:

1 0 10
2 1 12
3 5 6
4 6 10
5 7 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main(){
int i,time,remain,temps=0;
int cpu_timeframe = 4;
int wt=0,tat=0;
ifstream file;
vector<int> at;
vector<int> bt;
vector<int> rt;
vector<int> fp; // finished processes
  
file.open("round_robin.txt");
int tmp;
while(! file.eof()) {
file >> tmp; // false read not , not used in code
file >> tmp; // arrival_time
at.push_back(tmp);
file >> tmp; // time needed
bt.push_back(tmp);
rt.push_back(tmp);
}

cout << "finished processes from the vector, in the finished order";
cout<<"\nProcess\t: Turnaround Time: Waiting Time\n";
remain=at.size();
// In round robin simulation,the job scheduler saves current state of the job
// and moves to the another job in the queue.
for(time=0,i=0;remain!=0;) {

if(rt[i]<=cpu_timeframe && rt[i]>0) {
time += rt[i];
rt[i] = 0;
temps = 1;
} else if(rt[i]>0) {
// Every process gets an equal time of execution the CPU timeframe.
rt[i] -= cpu_timeframe;
time += cpu_timeframe;
}
  
if(rt[i]==0 && temps==1) {
remain--;
printf("Process# %d:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
fp.push_back(i+1);
wt += time-at[i]-bt[i];
tat += time-at[i];
temps=0;
}
  
if(i == at.size()-1)
i=0;
else if(at[i+1] <= time)
i++;
else
i=0;
}

cout<<"Average waiting time "<<wt*1.0/at.size()<<endl;
cout<<"Average turn around time "<<tat*1.0/at.size()<<endl;;

return 0;
}

The content of round_robit.txt
1 0 10
2 1 12
3 5 6
4 6 10
5 7 4

The program output:

Add a comment
Know the answer?
Add Answer to:
Round Robin Simulation in C++ using queue Description: Write a program that utilizes STL queue that...
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
  • Java program The round-robin scheduling problem: using queue There are n processes in a queue. Each...

    Java program The round-robin scheduling problem: using queue There are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue. For example, we have the following queue...

  • Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS)...

    Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS) Round Robin (RR) Process information The process information will be read from an input file. The format is: pid arrival_time burst_time All of fields are integer type where: pid is a unique numeric process ID arrival_time is the time when the task arrives in the unit of milliseconds burst_time the is the CPU time requested by a task, in the unit of milliseconds The...

  • Assume that you have four different processes with the following attributes: Process   Arrival time.   CPU Burst....

    Assume that you have four different processes with the following attributes: Process   Arrival time.   CPU Burst.   I/O Burst Total CPU time A. 0 4 4 9 B 3 2 3 7 C 6 5 1 11 D 12 1 1 5 As we did in class, perform a scheduling simulation using these four processes according to the following algorithms: 1) First Come First Serve 2) Round Robin with time slice = 1 3) Round Robin with time slice = 3...

  • Assume a dynamic queue which is serviced by a Priority Based Round Robin algorithm such that ther...

    Assume a dynamic queue which is serviced by a Priority Based Round Robin algorithm such that there exists three priorities (1,2,3) which are used as multipliers of the basic time quantum value with the resulting number being the maximum service time the corresponding job will receive each time it gets the CPU. For a maximum amount of time equal to 1 basic time quantum , a priority 2 job gets the CPU for the maximum amount of time equal to...

  • 4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1...

    4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1 Priority P2 P3 P4 P5 For each of the following CPU scheduling algorithms, determine the turnaround and average waiting time for each of the process :- a. Shortest remaining Time First b. Shortest Job First C. Priority Scheduling (Both pre-emptive and non-preemptive) d. Round Robin (quantum is 1 ms)

  • This assignment requires you to create simulations for different scheduling algorithms commonly employed by operating systems...

    This assignment requires you to create simulations for different scheduling algorithms commonly employed by operating systems to achieve multiprogramming. All problems in this assignment assume the following: The simulations you will be creating are for a uniprocessor system (single CPU). Processes in these simulations will require CPU bursts of one or more time units followed by I/O bursts of one or more time units. For simplicity’s sake, when more than one process is executing its I/O burst at the same...

  • The following processes are being scheduled using a pre-emptive, priority-based, round-robin scheduling algorithm. Process Burst Time...

    The following processes are being scheduled using a pre-emptive, priority-based, round-robin scheduling algorithm. Process Burst Time Priority Arrival 20 20 0 20 25 45 55 5 5 5 15 Each process is assigned a numerical priority, with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is pre-empted by a higher-priority process,...

  • 1. True or False: Round-robin CPU scheduling is used to study the behavior of real-world systems....

    1. True or False: Round-robin CPU scheduling is used to study the behavior of real-world systems. Answer: 2. True or False: Because a priority queue closely resembles a queue, the two have the same interface or set of operations. Answer: 3. True or False: A computer's file system has three major components: a directory of files, tracks, and sectors. Answer 4. True or False: The inorder traversal algorithm traverses the left subtree, visits the root node, and traverses to the...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • 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...

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