Monday, December 26, 2011

Exercise Program - 5.b IMPLEMENTATION OF ROUND ROBIN ALGORITHM

AIM:

To write a C program for implementing the Round Robin algorithm in Linux operating systems.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the variables as required.

Step 3: Get the number of process.

Step 4: Get the burst time for each process.

Step 5: get the quantum value.

Step 6: Get the arrival time for each process.

Step 7: Using the read data find the waiting time and turnaround time and average turn around

time and print the result.

Step 8: Stop the program.

PROGRAM:

#include

#include

typedef struct pcb

{

int pid, bt, bt_bal, tat, wt;

} PCB;

int main()

{

PCB pr[10];

int i,j,k,n,tq;

int sum_bt=0,sum_tat=0,sum_wt=0,tq_used=0;

int gantt[2][50];

printf("Enter the no. of process : ");

scanf("%d",&n);

for(i=0;i

{

printf("Enter burst time of the process %d: ",i+1);

pr[i].pid = i+1;

scanf("%d",&pr[i].bt);

pr[i].bt_bal = pr[i].bt;

}

printf("Enter the time quantum no. : ");

scanf("%d",&tq);

for( i=0;i

sum_bt += pr[i].bt;

printf("\nsum of bt = %d\n",sum_bt);

k=0;

do

{

for( i=0;i

if( pr[i].bt_bal > 0 && pr[i].bt_bal <= tq )

{

tq_used += pr[i].bt_bal;

pr[i].tat = tq_used;

pr[i].wt = pr[i].tat - pr[i].bt;

pr[i].bt_bal = 0;

gantt[0][k] = pr[i].pid;

gantt[1][k] = tq_used;

k++;

}

else if( pr[i].bt_bal >0 )

{

tq_used += tq;

pr[i].bt_bal -= tq;

gantt[0][k] = pr[i].pid;

gantt[1][k] = tq_used;

k++;

}

else if( pr[i].bt_bal < 0 )

{

printf("\nError: bt --ve\n");

exit(1);

}

} while( tq_used != sum_bt);

printf("\nROUND ROBBIN Scheduling GANTT Chart\n\n");

printf("PID: ");

for( i=0; i

printf("%4d",gantt[0][i]);

printf("\n\nTime: ");

for( i=0; i

printf("%4d",gantt[1][i]);

for( i=0;i

sum_wt += pr[i].wt;

for( i=0;i

sum_tat += pr[i].tat;

printf("\n\nPID: ");

for( i=0;i

printf("%4d",i+1);

printf("\nBtime:");

for( i=0;i

printf("%4d",pr[i].bt);

printf("\nTAT: ");

for( i=0;i

printf("%4d",pr[i].tat);

printf("\nWtime:");

for( i=0;i

printf("%4d",pr[i].wt);

printf("\n\nTotal waiting time = %d\n",sum_wt);

printf("Average waiting time = %.2f\n",(float)sum_wt/n);

printf("\nTotal turn around time = %d\n",sum_tat);

printf("Average turn around time = %.2f\n\n",(float)sum_tat/n);

return 0;

}

SAMPLE OUTPUT:

[it65@AntiViruS ~]$ cc roundrobin.c

[it65@AntiViruS ~]$ ./a.out

Enter the no. of process : 4

Enter burst time of the process 1: 53

Enter burst time of the process 2: 17

Enter burst time of the process 3: 68

Enter burst time of the process 4: 24

Enter the time quantum no. : 20

sum of bt = 162

ROUND ROBBIN Scheduling GANTT Chart

PID: 1 2 3 4 1 3 4 1 3 3

Time: 20 37 57 77 97 117 121 134 154 162

PID: 1 2 3 4

Btime: 53 17 68 24

TAT: 134 37 162 121

Wtime: 81 20 94 97

Total waiting time = 292

Average waiting time = 73.00

Total turn around time = 454

Average turn around time = 113.50


RESULT:

Thus the c-program for implementing Round Robin scheduling has been executed and the output is verified successfully.

No comments: