-
Notifications
You must be signed in to change notification settings - Fork 3
/
sjf.c~
101 lines (55 loc) · 1.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
void main() {
int P[20],BT[20] ,WT[20] , TT[20];
int total_tt =0 , total_wt =0;
int i ,j, n,temp,pos ;
double avg_wt , avg_tt;
printf("Enter the number of processes ");
scanf("%d",&n);
for(i=0 ; i<n;++i) {
printf("Enter the burst time for process %d :" , i+1);
scanf("%d" , &BT[i]);
P[i]=i+1;
}
for(i=0;i<n;++i){
pos=i;
for(j=i+1;j<n;++j) {
if(BT[j]<BT[pos])
{
pos =j;
}
}
temp = BT[i];
BT[i]=BT[pos];
BT[pos]=temp;
temp = P[i];
P[i]=P[pos];
P[pos]=temp;
}
WT[0]=0;
for(i=0 ;i<n;++i)
{ WT[i]=0;
for (j=0;j<i;j++)
{
WT[i] = WT[i] +BT[j];
}
total_wt += WT[i];
}
for(i=0;i<n;++i)
{
TT[i]= BT[i] +WT[i];
total_tt +=TT[i];
}
printf(" \n\n\tSHORTEST JOB FIRST SCHEDULING \n");
printf("\tPROCESS INFORMATION \n");
printf("_________________________\n");
printf(" PID | BURST TIME | WAITING TIME | TURNAROUND TIME \n");
for(i=0;i<n;++i)
{
printf("%d\t %d\t\t %d\t\t %d\t \n" , P[i] ,BT[i] ,WT[i] ,TT[i]);
}
avg_wt =total_wt/n;
avg_tt =total_tt/n;
printf ("Average waiting time =%f \n " ,avg_wt );
printf ("Average turnaround time =%f \n " ,avg_tt );
}