Practical 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Program-6

Aim : Write a program for Round Robin CPU scheduling.


Program :
#include<iostream>
#include<unistd.h>
using namespace std;

int main()
{
int nop;
int i;
int timeSlot;
int hover = 0;
cout<<"\nNUMBER OF PROCESSES: ";
cin>>nop;
int burstTime[10];

cout<<"TIME SLOT:";
cin>>timeSlot;

for(i=0; i<nop; i++)


{

cout<<"PROCESS " << i+1 << "\n";


cout<<"\tBURST TIME: ";
cin>>burstTime[i];
}
//display burst time
cout<<"\n================================\n";
cout<<" PROCESS\t BURST TIME\t ";
for(i=0;i<nop;i++)
{
cout << "\n P"<<i+1<<"\t\t"<<burstTime[i];
hver += burstTime[i];
}
cout<<"\n================================\n\n";

//start process execution


//int sec = timeSlot;
int sec=0;
while(hover != 0)
{
for(i = 0; i < nop; i++)
{
if(burstTime[i] > 0)
{
if(burstTime[i] > timeSlot)
{
sleep(timeSlot);
burstTime[i] -= timeSlot;
hover -= timeSlot;
cout << "at " << sec <<"s\tPROCESS P";
cout << i+1 << " is running ......\n";
sec += timeSlot;
}
else
{
sleep(burstTime[i]);
hover -= burstTime[i];
cout << "at " << sec <<"s\tPROCESS P";
cout << i+1 <<" has completed\n";
sec += burstTime[i];
burstTime[i] = 0;
}
}
}
}
return 0;
}

Output :
Number of processes: 5
Time slot:3
Process 1
burst time: 7
Process 2
burst time: 5
Process 3
burst time: 8
Process 4
burst time: 2
Process 5
burst time: 4

================================
process burst time
p1 7
p2 5
p3 8
p4 2
p5 4
================================

At 0s process p1 is running ......


At 3s process p2 is running ......
At 6s process p3 is running ......
At 9s process p4 has completed
At 11s process p5 is running ......
At 14s process p1 is running ......
At 17s process p2 has completed
At 19s process p3 is running ......
At 22s process p5 has completed
At 23s process p1 has completed
At 24s process p3 has completed

You might also like