## Friday, 5 February 2016

### CPU Scheduling Algorithm - Round Robin(Cpp)

 This is a Cpp program which implements one of the CPU Scheduling algorithm called Round Robin(RR). Round robin algorithm is mainly used in time sharing systems , it is also similar to First Come First Served(FCFS) algorithm but FCFS does not have that time slicing switch. All the jobs gets executed in this scheduling algorithm , so the advantage here is , its Starvation free.

Download Code

## PROGRAM :

```#include <iostream>

using namespace std;

int main()
{
int bt_c,bt,i,j,n,m,r,q,e=0;
float f,avg=0;
cout << "\nEnter how many jobs ?\t";
cin >> n;
for(i=1; i<=n; i++)
{
cout << "Enter burst time for job %d :\t" << i;
cin >> bt[i]);
bt_c[i]=bt[i]; //stores job has how much burst  time in array  i
}
cout << "\nEnter Quantum (time slice value) :\t";
cin >> q;
int max=0;
max=bt;
for(j=1; j<=n; j++)
if(max<=bt[j])
max=bt[j];

if((max%q)==0)
r=(max/q);
else
r=(max/q)+1;
for(i=1; i<=r; i++)
{
cout << "\n\nRound" << i;
for(j=1; j<=n; j++)
{
if(bt[j]>0)
{
bt[j]=bt[j]-q;

if(bt[j]<=0)
{
bt[j]=0;
cout << "\njob %d is completed" << j;
}
else
cout << "\njob" <<  j << " remaining time is " << bt[j];
}
}

}
for(i=1; i<=n; i++)
{
e=0;
for(j=1; j<=r; j++)
{
if(bt_c[i]!=0)
{
if(bt_c[i]>=q)
{
m[i+e]=q;
bt_c[i]-=q;
}
else
{
m[i+e]=bt_c[i];
bt_c[i]=0;
}
}
else
m[i+e]=0;
e=e+n;
}
}
for(i=2; i<=n; i++)
for(j=1; j<=i-1; j++)
avg=avg+m[j];
for(i=n+1; i<=r*n; i++)
{
if(m[i]!=0)
{
for(j=i-(n-1); j<=i-1; j++)
avg=m[j]+avg;
}
}
f=avg/n;
cout << "\n\n\nTOTAL WATING TIME:" << avg << "\t";
cout << "\n\nAVERAGE WAITING TIME:" << f << "\t\n";
return 0;
}
```
Download Code

## OUTPUT : Cpp Programming - CPU Scheduling Algorithm - Round Robin