-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirst come first serve
More file actions
99 lines (96 loc) · 3.24 KB
/
First come first serve
File metadata and controls
99 lines (96 loc) · 3.24 KB
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
// ALGORITHM:
// 1. Start
// 2. Input the processes, their burst time and arrival time
// 3. Sort the processes according to arrival time
// 4. Find completion Time
// a)for first process , completionTime = bursttime + arrivaltime[1]
// b)for other processes , completionTime+=burstTime
// 5. Find turnaround time (tat) = completionTime-arrivalTime , for all processes
// 6. Find waiting time=turnaroundTime-burstTime , for all processes
// 7. if waiting time < 0 then
// a)completionTime += abs(waitingTime);
// b)turnaroundtime += abs(waitingTime);
// c)waitingTime = 0;
// 6. Find average waiting time = total_waiting_time / no_of_processes
// 7. Find average turnaround time = total_turnaround_time / no_of_processes
// 8. End
#include <stdio.h>
#include <stdlib.h>
int n, i, j, temp = 0, t, completionTime[10], tat[10], wt[10], burst[10] = {0}, arrival[10] = {0}, order[10] = {0};
float total_tat = 0, total_wt = 0;
void swap(int *xp, int *yp)
{
int t = *xp;
*xp = *yp;
*yp = t;
}
void Sort(int arr[], int n)
{
int i, j;
for (i = 1; i < n; i++)
for (j = i + 1; j <= n; j++)
if (arr[i] > arr[j])
{
swap(&arrival[i],&arrival[j]);
swap(&burst[i], &burst[j]);
swap(&order[i], &order[j]);
}
}
int main()
{
printf("\n-------------------------------");
printf("\nPROGRAM : FIRST COME FIRST SERVE \n\n");
printf("---------------------------------\n");
printf("Enter no of process : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("\nArrival time of process %d is :", i);
scanf("%d", &arrival[i]);
printf("\nBurst time of process %d is :", i);
scanf("%d", &burst[i]);
order[i] = i;
}
Sort(arrival, n);
for (i = 1; i <= n; i++)
{
if (i == 1)
{
temp = burst[i] + arrival[1];
completionTime[i] = temp;
tat[i] = completionTime[i] - arrival[i];
wt[i] = tat[i] - burst[i];
total_tat = total_tat + tat[i];
}
else
{
temp = temp + burst[i];
completionTime[i] = temp;
tat[i] = completionTime[i] - arrival[i];
wt[i] = tat[i] - burst[i];
if (wt[i] < 0)
{
completionTime[i] = completionTime[i] + abs(wt[i]);
temp = completionTime[i];
tat[i] += abs(wt[i]);
wt[i] = 0;
}
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
}
}
total_wt = total_wt / n;
total_tat = total_tat / n;
printf("\norder in which process get executed :\t");
for (i = 1; i <= n; i++)
{
printf("%d \t ", order[i]);
}
printf("\n\n arrival_time Burst_time Turn_around_time waiting_time completion time");
for (i = 1; i <= n; i++)
{
printf("\nprocess%d %d %d %d %d %d", order[i], arrival[i], burst[i], tat[i], wt[i], completionTime[i]);
}
printf("\n\n Average waiting time is %.2f", total_wt);
printf("\n Average turn around time is is %.2f\n", total_tat);
}