0% found this document useful (0 votes)
91 views

Round Robin Code

The document defines structures and functions to implement a round robin scheduling algorithm. It reads process details, sorts by arrival time, implements the RR algorithm to calculate completion times and wait times, displays the results, draws a Gantt chart, and calculates average wait and turnaround times.

Uploaded by

Atul Draws
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Round Robin Code

The document defines structures and functions to implement a round robin scheduling algorithm. It reads process details, sorts by arrival time, implements the RR algorithm to calculate completion times and wait times, displays the results, draws a Gantt chart, and calculates average wait and turnaround times.

Uploaded by

Atul Draws
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

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);

printf("Enter the process: ");


for(int i=0;i<n;i++){
//set ct,tat,wt,exe of process i as 0 and read process i
p[i].ct=0,p[i].ta=0,p[i].wt=0,p[i].exe=0;
scanf("%d",&p[i].id);
}

printf("Enter Arrival Time: ");


for(int i=0;i<n;i++){
scanf("%d",&p[i].at);
}

printf("Enter Burst Time: ");


for(int i=0;i<n;i++){
scanf("%d",&p[i].bt);
p[i].rt=p[i].bt;
}
}

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);
}
}

void enqueue(int item){


rear=(rear+1)%n;
q[rear]=item;
if(front==-1)
front=0;
}

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|");

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


{
for(j=0; j<g[i].bt - 1; j++) printf(" ");
printf("P%d", g[i].id);
for(j=0; j<g[i].bt - 1; j++) printf(" ");
printf("|");
}
printf("\n ");
for(i=0; i<n1; i++)
{
for(j=0; j<g[i].bt; j++) printf("--");
printf(" ");
}
printf("\n");
printf("0");
for(i=0; i<n1; i++)
{
for(j=0; j<g[i].bt; j++) printf(" ");
if(g[i].ct > 9) printf("\b");
printf("%d", g[i].ct);

}
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

You might also like