-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathNon-Preemptive Priority Scheduling (Smallest Priority First) Algorithm.c
60 lines (59 loc) · 1.59 KB
/
Non-Preemptive Priority Scheduling (Smallest Priority First) Algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
#define MAX 9999;
struct proc
{
int no,at,bt,ct,wt,tat,pri,status;
};
struct proc read(int i)
{
struct proc p;
printf("\nProcess No: %d\n",i);
p.no=i;
printf("Enter Arrival Time: ");
scanf("%d",&p.at);
printf("Enter Burst Time: ");
scanf("%d",&p.bt);
printf("Enter Priority: ");
scanf("%d",&p.pri);
p.status=0;
return p;
}
void main()
{
int n,s,ct=0,remaining;
struct proc p[10],temp;
float avgtat=0,avgwt=0;
printf("<--Smallest Priority First Scheduling Algorithm (Non-Preemptive)-->\n");
printf("Enter Number of Processes: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
p[i]=read(i+1);
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
p[9].pri=MAX;
remaining=n;
printf("\nProcessNo\tAT\tBT\tPri\tCT\tTAT\tWT\tRT\n");
for(ct=p[0].at;remaining!=0;)
{
s=9;
for(int i=0;i<n;i++)
if(p[i].at<=ct && p[i].status!=1 && p[i].pri<p[s].pri)
s=i;
p[s].ct=ct=ct+p[s].bt;
p[s].tat=p[s].ct-p[s].at;
avgtat+=p[s].tat;
p[s].wt=p[s].tat-p[s].bt;
avgwt+=p[s].wt;
p[s].status=1;
remaining--;
printf("P%d\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",p[s].no,p[s].at,p[s].bt,p[s].pri,p[s].ct,p[s].tat,p[s].wt,p[s].wt);
}
avgtat/=n,avgwt/=n;
printf("\nAverage TurnAroundTime=%f\nAverage WaitingTime=%f",avgtat,avgwt);
}