-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFirst Come First Served(FCFS).c
74 lines (61 loc) · 1.49 KB
/
First Come First Served(FCFS).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
#include<stdio.h>
#define max 30
typedef struct FCFS
{
int pid, at, bt, tat, wt, ct, st;
} ps;
int main()
{
ps p[max],temp;
int i, j, n, sum_tat = 0, sum_wt = 0;
float awt, atat;
printf("Enter the number of processors:\n");
scanf("%d", &n);
printf("Enter the Processor ID:\n");
for (i = 0; i < n; i++)
scanf("%d", &p[i].pid);
printf("Enter the Arrival time:\n");
for (i = 0; i < n; i++)
scanf("%d", &p[i].at);
printf("Enter the Burst time:\n");
for (i = 0; i < n; i++)
scanf("%d", &p[i].bt);
// Sort processes by arrival time
for (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 + 1];
p[j + 1] = p[j];
p[j] = temp;
}
}
}
// Calculate start time & completion time
p[0].st=p[0].at;
p[0].ct = p[0].st + p[0].bt;
for (i = 1; i < n; i++)
{
if(p[i].at>p[i-1].ct)
p[i].st=p[i].at;
else
p[i].st=p[i-1].ct;
p[i].ct=p[i].st+p[i].bt;
}
printf("Pid\tAT\tBT\tST\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++)
{
p[i].tat = p[i].ct - p[i].at; // Turnaround time
p[i].wt = p[i].tat - p[i].bt; // Waiting time
sum_tat += p[i].tat;
sum_wt += p[i].wt;
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt,p[i].st, p[i].ct, p[i].tat, p[i].wt);
}
awt = (float)sum_wt / n;
atat = (float)sum_tat / n;
printf("Average Waiting Time: %.2f\n", awt);
printf("Average Turnaround Time: %.2f\n", atat);
return 0;
}