Question

In: Computer Science

Round Robin Simulation Description: Write a program in c++ that utilizes STL queue (or your own...

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

Solutions

Expert Solution

 

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.


Related Solutions

In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling...
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling algorithm. It should implement threads for the algorithm plus the main thread using a linked list to represent the list of jobs available to run. Each node will represent a Job with the following information: int ProcessID int time time needed to finish executing The thread representing the scheduler algorithm should continue running until all jobs end. This is simulated using the time variable...
There are n processes in a queue. Each process has namei and timei. The round-robin scheduling...
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 with the quantum of 100ms. A(150) - B(80)...
There are n processes in a queue. Each process has namei and timei. The round-robin scheduling...
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 with the quantum of 100ms. A(150) - B(80)...
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT