Round Robin

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SOURCE CODE

#include<stdio.h>

main()

int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max; float awt=0,att=0,temp=0;

clrscr();

printf("Enter the no of processes -- ");

scanf("%d",&n);

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

printf("\nEnter Burst Time for process %d -- ", i+1);

scanf("%d",&bu[i]);

ct[i]=bu[i];

printf("\nEnter the size of time slice -- ");

scanf("%d",&t);

max=bu[0];

for(i=1;i<n;i++)

if(max<bu[i])

max=bu[i];

for(j=0;j<(max/t)+1;j++)

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

if(bu[i]!=0)

if(bu[i]<=t)

tat[i]=temp+bu[i];

temp=temp+bu[i];

bu[i]=0;

Else

bu[i]=bu[i]-t;

temp=temp+t;
}

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

wa[i]=tat[i]-ct[i];

att+=tat[i];

awt+=wa[i];}

printf("\nThe Average Turnaround time is -- %f",att/n);

printf("\nThe Average Waiting time is -- %f ",awt/n);

printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");

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

printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);

getch();

INPUT:

Enter the no of processes – 3

Enter Burst Time for process 1 – 24 Enter Burst Time for process 2 -- 3 Enter Burst Time for process 3 – 3 Enter
the size of time slice – 3

OUTPUT:

PROCESS BURST TIME WAITING TIME TURNAROUNDTIME

1 24 6 30

2 3 4 7

3 3 7 10

The Average Turnaround time is – 15.666667 The Average Waiting time is 5.666667

A). PRIORITY:

AIM: To write a c program to simulate the CPU scheduling priorityalgorithm.

DESCRIPTION:
To calculate the average waiting time in the priority algorithm, sort the burst times according to their priorities and
then calculate the average waiting time of the processes. The waiting time of each process is obtained by
summing up the burst times of all the previous processes.

ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time

Step 4: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as ‗0‘ and its burst time as its turnaround time Step 6: Arrange the
processes based on process priority

Step 7: For each process in the Ready Q calculate Step 8:

for each process in the Ready Q calculate

a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)

b) Turnaround time (n)= waiting time(n)+Burst time(n)

Step 9: Calculate

c) Average waiting time = Total waiting Time / Number of process

d) Average Turnaround time = Total Turnaround Time / Number of process Print the results in an
order.

Step10: Stop
SOURCE CODE:

#include<stdio.h>

main()

int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;

float wtavg, tatavg;

clrscr();

printf("Enter the number of processes --"); scanf("%d",&n);

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

p[i] = i;

printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d

%d",&bt[i], &pri[i]);

for(i=0;i<n;i++) for(k=i+1;k<n;k++)

if(pri[i] > pri[k])

temp=p[i];

p[i]=p[k];

p[k]=temp;

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

temp=pri[i];

pri[i]=pri[k];

pri[k]=temp;

wtavg =wt[0] = 0;

tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)

{
wt[i] = wt[i-1] + bt[i-1];

tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME");

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

printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverageTurnaround Time is ---


%f",tatavg/n);

getch();

INPUT

Enter the number of processes -- 5

Enter the Burst Time & Priority of Process 0 --- 10 3

Enter the Burst Time & Priority of Process 1 --- 1 1

Enter the Burst Time & Priority of Process 2 --- 2 4

Enter the Burst Time & Priority of Process 3 --- 1 5

Enter the Burst Time & Priority of Process 4 --- 5 2


OUTPUT

PROCESS PRIORITY BURST TIME WAITIN G TIME 0 TURNARO UND


TIME 1

1 1 1

4 2 5 1 6

0 3 10 6 16

2 4 2 16 18

3 5 1 18 19

Average Waiting Time is --- 8.200000

Average Turnaround Time is --- 12.000000

You might also like