-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.2 First_come_first_serve.c
98 lines (95 loc) · 1.99 KB
/
1.2 First_come_first_serve.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
#include<stdio.h>
struct process
{
int at,bt,order,rem,rt,wt,tt,terminated;
}pr[10];
int allprocessesTerminated(int n ,struct process pr[10])
{
int i;
for(i=0;i<n;i++)
{
if(!pr[i].terminated)
{
return 0;
}
}
return 1;
}
int nextprocessTorun(int n , struct process pr[10] , int ct)
{
int min_val=100;
int x=-1,i;
for(i=0;i<n;i++)
{
if(pr[i].at<=ct && !pr[i].terminated)
{
if(pr[i].order < min_val)
{
min_val = pr[i].order;
x=i;
}
}
}
return x;
}
int main()
{
int i,j,n;
printf("Enter the number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("PROCESS P(%d)\n",i);
printf("Enter the arrival time ( AT ) : ");
scanf("%d",&pr[i].at);
printf("Enter the burst time ( BT ) : ");
scanf("%d",&pr[i].bt);
printf("Enter the order : ");
scanf("%d",&pr[i].order);
pr[i].rem=pr[i].bt;
pr[i].wt=0;
pr[i].terminated=0;
}
int ct=0;
while(!allprocessesTerminated(n,pr))
{
int x=nextprocessTorun(n,pr,ct);
if(x==-1)
{
printf("t=%d IDLE\n",ct);
++ct;
continue;
}
if(pr[x].rem == pr[x].bt)
{
pr[x].rt = ct - pr[x].at;
}
--pr[x].rem;
if(pr[x].rem == 0)
{
pr[x].tt = (ct+1) - pr[x].at;
pr[x].terminated = 1;
}
for(i=0;i<n;i++)
{
if(x == i)
{
continue;
}
if(pr[i].at<=ct && !pr[i].terminated)
{
++pr[i].wt;
}
}
printf("t=%d p%d\n",ct,x);
++ct;
}
for(i=0;i<n;i++)
{
printf("PROCESS p%d\n",i+1);
printf("RT : %d\n",pr[i].rt);
printf("WT : %d\n",pr[i].wt);
printf("TT : %d\n\n",pr[i].tt);
}
return 0;
}