-
Notifications
You must be signed in to change notification settings - Fork 0
/
SJF.c
126 lines (118 loc) · 2.92 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<stdio.h>
#include<limits.h>
struct process
{
int id;
int at;
int bt;
int ct;
int tat;
int wt;
int key;
};
void completion_time(struct process p[], int size)
{
int i,flag=1, temp = 0, pos = 0;
while(flag)
{
int copy[2][20];
int j=0;
flag = 0;
for(i=0; i<size; i++)
if(p[i].key==0 && p[i].at<temp)
{
copy[0][j] = p[i].id;
copy[1][j++] = p[i].bt;
}
if(j==0)
{
int temp1=INT_MAX;
for(i=0; i<size; i++)
if(p[i].at <temp1 && p[i].key==0 )
{
temp1 = p[i].at;
pos = i;
}
temp = p[pos].ct = p[pos].at + p[pos].bt;
p[pos].key = 1;
}
else
{
sort(copy , j);
if(temp < p[copy[0][0]-1].at)
{
temp = p[copy[0][0]-1].ct = p[copy[0][0]-1].at; + p[copy[0][0]-1].bt;
}
else
{
temp = p[copy[0][0]-1].ct = temp + p[copy[0][0]-1].bt;
}
p[copy[0][0]-1].key = 1;
}
for(i=0; i<size; i++)
if(p[i].key == 0)
flag = 1;
}
}
void turn_around_time(struct process p[], int size)
{
int i;
float avg, temp=0.0;
for(i=0; i<size; i++)
{
p[i].tat = p[i].ct - p[i].at;
temp += p[i].tat;
}
avg = temp/size;
printf("\nAverage Turn around time = %f", avg);
}
void avg_waiting_time(struct process p[], int size)
{
int i;
float avg, temp = 0.0;
for(i=0; i<size; i++)
{
p[i].wt = p[i].tat - p[i].bt;
temp += p[i].wt;
}
avg = temp/size;
printf("\nAverage Waiting time = %f",avg);
}
void sort(int p[2][20], int size)
{
int i, j, pos, temp;
for(i=0; i<size-1; i++)
{
pos=i;
for(j=i; j<size; j++)
if(p[1][j] < p[1][pos])
pos = j;
temp = p[1][i];
p[1][i] = p[1][pos];
p[1][pos] = temp;
temp = p[0][i];
p[0][i] = p[0][pos];
p[0][pos] = temp;
}
}
int main()
{
int n,i;
printf("Enter total no. of process: ");
scanf("%d",&n);
struct process p[n];
for(i=0; i<n; i++)
{
p[i].id = i+1;
p[i].key = 0;
printf("\nEnter Arrival Time and Burst Time for process %d : ",i+1);
scanf("%d%d",&p[i].at, &p[i].bt );
}
completion_time(p,n);
turn_around_time(p,n);
avg_waiting_time(p,n);
printf("\n Process No. Arrival Time Burst Time Completion Time Turn Around Time Waiting Time\n");
for(i=0; i<n; i++ )
printf("\n\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i].id, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
return 0;
}