-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjf.c
65 lines (65 loc) · 1.05 KB
/
sjf.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
#include<stdio.h>
int p[10],bt[10],tottat=0,wt[10],n,totwt=0,tat[10],sjfwt=0,sjftat=0;
int swap(int *a,int *b);
int sort();
int main()
{
int i;
printf("ente no of process\n");
scanf("%d",&n);
printf("enter burst time:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
p[i]=i;
}
sort();
for(i=0;i<n;i++)
{
if(i==0)
tat[i]=bt[i];
else
tat[i]=tat[i-1]+bt[i];
tottat=tottat+tat[i];
}
sjftat=tottat;
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
totwt=totwt+wt[i];
}
sjfwt=totwt;
printf("\nprocess\t\tbursttime\tat\twt");
for(i=0;i<n;i++)
printf("\np[%d]\t\t%d\t\t%d\t\t%d",p[i]+1,bt[i],tat[i],wt[i]);
printf("\n\ntotal turnaround time:%d",sjftat);
printf("\naverage turnaround time:%d",sjftat/n);
printf("\n\ntotal waiting time:%d",sjfwt);
printf("\naverage waiting time:%d",sjfwt/n);
return 0;
}
int sort()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
swap(&bt[i],&bt[j]);
swap(&p[i],&p[j]);
}
}
}
return 0;
}
int swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
return 0;
}