Round Robin Code
Round Robin Code
h>
struct pro{
int id, at, bt, ct ,ta,wt,exe,rt; };
struct pro p[10],g[100],temp;
int n,n1,q[10],front=-1,rear=-1,tq;
void read(){
printf("Enter the number of process : ");
scanf("%d",&n);
printf("Enter the time quantum : ");
scanf("%d",&tq);
void sort(){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
//sort p wrt AT
if(p[j].at>p[j+1].at){
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void display(){
printf("\nPid\tAT\tBT\tCT\tTAT\tWT\n");
for(int i=0;i<n;i++){
printf("p%d\t%d\t%d\t%d\t%d\t%d\n",p[i].id,p[i].at,p[i].bt,p[i].ct,p[i].ta,p[i].wt);
}
}
int dequeue(){
int item=q[front];
if(front==rear){
front=-1;
rear=-1;
}
else{
front=(front+1)%n;
}
return item;
}
//after sorting
void rr(){
int remain=n,prorem=n;
enqueue(p[0].id); // put first process in ready Q
p[0].exe=1; // that process is now executing
prorem--; // one less process remaining to enter ready queue
int lt=0,i,k=0; // set LT = 0
while(remain!=0){ // while processes remain to be executed
int flag=0; // process has not finished executing
int qpid=dequeue(); // remove first process from RQ
for(i=0;i<n;i++){ // i = 0 to n for loop
if(p[i].id==qpid) // if pid = qid, break
break;
}
if(p[i].rt<=tq){ //if remaining time <= time quantum
int tempbt=p[i].rt; //store remaining time in temp BT variable
p[i].ct=lt+p[i].rt; //CT = LT(last reached time) + RT
lt=lt+p[i].rt; //LT = LT + RT
p[i].rt=0; //set RT = 0
g[k].id=p[i].id; //enter that process to gant chart list
g[k].bt=tempbt; //store RT of the process (tempBT) as gant chart BT of that
process
g[k].ct=lt; // CT of the process = LT
k++;
flag=1; // process is finished executing
remain--; // one less process remaining in ready queue
}
else{ //if remaining time > time quantum
p[i].rt=p[i].rt-tq; // RT = RT - TQ
lt=lt+tq; // LT = LT + TQ
g[k].id=p[i].id; //enter that process to gant chart list
g[k].bt=tq; //store TQ as gant chart BT of that process
g[k].ct=lt; // CT of the process = LT
k++;
}
if(prorem!=0){ // if there are processes remaining to enter the ready Q
for(int j=0;j<n;j++){ // loop through all the remaining processes
if(p[j].exe!=1 && p[j].at<=lt){ // if the process is not executing and has
AT < last reached time
enqueue(p[j].id); // put that process in ready Q
p[j].exe=1; // that process is now executing
prorem--; // one less process remaining to enter ready queue
}
}
}
if(flag!=1){ // if process has not finished executing
enqueue(p[i].id); // enter it back to RQ
}
}
for(int i=0;i<n;i++){
p[i].ta=p[i].ct-p[i].at;
p[i].wt=p[i].ta-p[i].bt;
}
n1=k;
}
void gc(){
int i, j;
printf(" ");
for(i=0; i<n1; i++)
{
for(j=0; j<g[i].bt; j++) printf("--");
printf(" ");
}
printf("\n|");
}
printf("\n");
}
void avg(){
float sumta=0,sumwt=0;
float avgta,avgwt;
for(int i=0;i<n;i++){
sumta=p[i].ta+sumta;
sumwt=p[i].wt+sumwt;
}
avgta=sumta/n;
avgwt=sumwt/n;
printf("\nAverage TurnAroundTime = %.2f",avgta);
printf("\nAverage WaitingTime = %.2f\n",avgwt);
}
int main(){
read();
sort();
rr();
display();
gc();
avg();
}
P0
1