-
Notifications
You must be signed in to change notification settings - Fork 10
/
SJF Scheduling Algorithm.c
68 lines (68 loc) · 1.73 KB
/
SJF Scheduling 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
61
62
63
64
65
66
67
68
#include<stdio.h>
struct proc
{
int no,at,bt,it,ct,tat,wt;
};
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);
return p;
}
int main()
{
int n,j,min=0;
float avgtat=0,avgwt=0;
struct proc p[10],temp;
printf("<--SJF 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(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;
}
for(j=1;j<n&&p[j].at==p[0].at;j++)
if(p[j].bt<p[min].bt)
min=j;
temp=p[0];
p[0]=p[min];
p[min]=temp;
p[0].it=p[0].at;
p[0].ct=p[0].it+p[0].bt;
for(int i=1;i<n;i++)
{
for(j=i+1,min=i;j<n&&p[j].at<=p[i-1].ct;j++)
if(p[j].bt<p[min].bt)
min=j;
temp=p[i];
p[i]=p[min];
p[min]=temp;
if(p[i].at<=p[i-1].ct)
p[i].it=p[i-1].ct;
else
p[i].it=p[i].at;
p[i].ct=p[i].it+p[i].bt;
}
printf("\nProcess\t\tAT\tBT\tCT\tTAT\tWT\tRT\n");
for(int i=0;i<n;i++)
{
p[i].tat=p[i].ct-p[i].at;
avgtat+=p[i].tat;
p[i].wt=p[i].tat-p[i].bt;
avgwt+=p[i].wt;
printf("P%d\t\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i].no,p[i].at,p[i].bt,p[i].ct,p[i].tat,p[i].wt,p[i].wt);
}
avgtat/=n,avgwt/=n;
printf("\nAverage TurnAroundTime=%f\nAverage WaitingTime=%f",avgtat,avgwt);
}