You are on page 1of 7

PROCESS SCHEDULING -II

Ex. No : 9
Date :

PROBLEM STATEMENT:

To implement Priority and Round Robin scheduling algorithms using


C.

PRIORITY SCHEDULING:

ALGORITHM:
1: Get the number of processes, priority and burst time.
2: Sort the process based on the priority in ascending order
3: Calculate the waiting time and turn around time.
4: Display the gantt chart,avg waiting time and turn around time.

PROGRAM:
#include<stdio.h>
main()
{
int a[20],b[20],pn[20];
char x[20];
int sum=0,t=0;
int n,j,i,te=0,te1=0;
float avg;
printf("enter the number of process");
scanf("%d",&n);
printf("process name");
for(i=1;i<=n;i++)
{
scanf("%s",&x[i]);
}
printf("enter the burst time");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
printf("enter the priority number");
for(i=1;i<=n;i++)
{
scanf("%d",&pn[i]);
}
printf("\nprocess\tburst time\tprioritynumber\n");
for(i=1;i<=n;i++)
{
printf("%c\t%d\t%d\n",x[i],b[i],pn[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pn[i]>pn[j])
{
te=pn[i];
pn[i]=pn[j];
pn[j]=te;
te1=b[i];
b[i]=b[j];
b[j]=te1;
}
}
}
printf("priority\n");
for(i=1;i<=n;i++)
{
printf("%d",pn[i]);
}
printf("\nburst time");
for(i=1;i<=n;i++)
{
printf("%d\t",b[i]);
}
printf("\ngantt chart");
for(i=1;i<=n;i++)
{
printf("%c\t",x[i]);
}
printf("\n%d\t",sum);
for(i=1;i<=n;i++)
{
sum=sum+b[i];
t=t+sum;
printf("%d\t",sum);
}
printf("\nthe average waiting time \n");
t=t-sum;
avg=t/n;
printf("\n%f",avg);
}

ROUND ROBIN SCHEDULING:


ALGORITHM:
1: Initialize all the structure elements
2: Receive inputs from the user to fill process id,burst time and arrival time.
3: Calculate the waiting time for all the process id.
i) The waiting time for first instance of a process is calculated as:
a[i].waittime=count + a[i].arrivt
ii) The waiting time for the rest of the instances of the process is calculated as:
a) If the time quantum is greater than the remaining burst time then waiting time
is calculated as:
a[i].waittime=count + tq
b) Else if the time quantum is greater than the remaining burst time then waiting
time is calculated as:
a[i].waittime=count - remaining burst time
4: Calculate the average waiting time and average turnaround time
5: Print the results of the step 4.

PROGRAM:

#include<stdio.h>
#include<string.h>
main()
{
int a[50],b[50];
int r=0,time,s;
int p[50],z=0;
char x[50];
int sum=0,t=0,n,i,j;
int avg=0;
float avg1=0;
printf("enter the number of process");
scanf("%d",&n);
s=n;
printf("process name");
for(i=1;i<=n;i++)
{
scanf("%s",&x[i]);
}
printf("\nenter the burst time");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
printf("enter the quantum time");
scanf("%d",&time);
printf("process\tbursttime\n");
for(i=1;i<=n;i++)
{
printf("%c\t%d\n",x[i],b[i]);
}
for(i=1;i<=n;i++)
{
if(b[i]>time)
{
p[i]=r+time;
n=n+1;
b[n]=b[i]-time;
x[n]=x[i];
}
else
{
p[i]=r+b[i];

}
r=p[i];
}
printf("gantt chart");
for(i=1;i<=n;i++)
{
printf("%c\t",x[i]);
}
printf("\n%d\t",sum);
for(i=1;i<=n;i++)
{
printf("\t%d",p[i]);
}
printf("\n");
for(i=1;i<s;i++)
{
avg=avg+p[i];
}
avg1=avg/s;
printf("average is %f",avg1);
}

SAMPLE INPUT OUTPUT:

PRIORITY:

Enter the number of processes 5

Process name a b c d e

Enter the Burst Time


350
125
475
250
75

Enter the Priority number


2
4
1
3
5
Process Burst Time Priority
a 350 2
b 125 4
c 475 1
d 250 3
e 75 5

priority
12345

Burst time
475 350 250 125 75

GanttChart

a b c d e

0 475 825 1075 1200 1275

The average Waiting Time 715.000000

SAMPLE INPUT OUTPUT:

ROUND ROBIN:

enter the number of process


3

process name
a
b
c

enter the burst time


24
3
3

enter the quantum time


4

process bursttime
a 24
b 3
c 3

gantt chart

a b c a a a a a

0 4 7 10 14 18 22 26 30

average waiting time is 3.000000

You might also like