-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_priority_scheduling_wo_arrivals.c
121 lines (103 loc) · 2.31 KB
/
03_priority_scheduling_wo_arrivals.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
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define max(one, two) ((one < two) ? two: one)
typedef struct Process
{
int process_number;
float burst;
float priority;
float waiting;
float turnaround;
} Process;
void print_process(Process* proc)
{
printf("(%d, %f, %f, %f, %f)\n",
proc->process_number,
proc->burst,
proc->priority,
proc->waiting,
proc->turnaround);
}
void print_process_array(int nf, Process* arr)
{
for (int i = 0; i < nf; ++i)
{
print_process(&arr[i]);
}
printf("\n");
}
void read_process(Process* proc)
{
scanf("%f %f",
&proc->burst,
&proc->priority);
}
void read_process_array(int nf, Process* arr)
{
for (int i = 0; i < nf; ++i)
{
arr[i].process_number = i + 1;
read_process(&arr[i]);
}
}
bool cmp_by_priority_then_burst(Process* proc_one, Process* proc_two)
{
return proc_one->priority < proc_two->priority ||
(proc_one->priority == proc_two->priority &&
proc_one->burst < proc_two->burst);
}
bool cmp_by_process_no(Process* proc_one, Process* proc_two)
{
return proc_one->process_number < proc_two->process_number;
}
void swap(Process *xp, Process *yp)
{
Process temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubble_sort(int nf, Process* arr, bool (*lt)(Process*, Process*))
{
int i, j;
for (i = 0; i < nf - 1; i++)
{
for (j = 0; j < nf - i - 1; j++)
{
if (lt(&arr[j + 1], &arr[j]))
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
void priority_scheduling(int nf, Process* arr, float* avg_waiting, float* avg_turnaround)
{
float time_elapsed = 0, sum_waiting, sum_turnaround;
float default_arrival_time = 0;
for (int i = 0; i < nf; ++i)
{
time_elapsed = max(time_elapsed, default_arrival_time);
arr[i].waiting = time_elapsed;
time_elapsed += arr[i].burst;
arr[i].turnaround = time_elapsed;
sum_waiting += arr[i].waiting;
sum_turnaround += arr[i].turnaround;
}
*avg_waiting = sum_waiting / nf;
*avg_turnaround = sum_turnaround / nf;
}
void main()
{
int nf;
scanf("%d", &nf);
Process arr[nf];
read_process_array(nf, arr);
bubble_sort(nf, arr, cmp_by_priority_then_burst);
float avg_waiting, avg_turnaround;
priority_scheduling(nf, arr, &avg_waiting, &avg_turnaround);
bubble_sort(nf, arr, cmp_by_process_no);
print_process_array(nf, arr);
printf("=> (%f, %f)",
avg_waiting, avg_turnaround);
}