4. Round robin scheduling program in C

  1. Round robin scheduling program in c

#include<stdio.h>
#include<deque>
using namespace std;
deque<int>dq;
int n;
int TimeQuantam = 4;
int bt[20];
int total;
void input()
{
for( int i = 0 ; i < n ; i++)
{
scanf(“%d”,&bt[i]);
dq.push_back(i+1);
dq.push_back(bt[i]);
}
}
int process;
int BTshow;
int WT1[50],WT2[50];
struct WT
{
int WT1,WT2;
};
WT ar[20];
void cal(int pro , int BTs)
{
dq.push_back(pro);
dq.push_back(BTs);
printf(“—P%d—%d”,process,total);
}
int temp1 , temp2[20];
void calculation()
{
total = 0;
printf(“0”);
while(! dq.empty())
{
process = dq.front();
dq.pop_front();
ar[process].WT1 = ar[process].WT1+ total ;
BTshow = temp1= dq.front();
dq.pop_front();
BTshow = BTshow – TimeQuantam;
if(BTshow <= 0)
{
int remainder = temp1 % TimeQuantam;
if(remainder == 0)
{
total = total + TimeQuantam;
ar[process].WT2 = ar[process].WT2+ total ;
temp2[process] = total;
printf(“—P%d—%d”,process,total);
}
else
{
total = total + remainder ;
ar[process].WT2 = ar[process].WT2+ total ;
temp2[process] = total;
printf(“—P%d—%d”,process,total);
}
continue;
}
total = total + TimeQuantam;
ar[process].WT2 = ar[process].WT2+ total ;
temp2[process] = total;
cal(process , BTshow);

}

}
int main()
{
scanf(“%d”,&n);
input();
calculation();
// output();
for( int i = 0 ; i < n ; i++)
{
printf(“\nWTP%d—>%d\n”,i+1,ar[i+1].WT1-(ar[i+1].WT2-temp2[i+1]));
// printf(“2—>%d\n”,WT2[i+1]);
}
return 0;
}

Sample Input :

4
12
8
9
7

Sample Output :

1