-
Notifications
You must be signed in to change notification settings - Fork 3
/
Preemptive_Schedulers.cs
163 lines (122 loc) · 4.73 KB
/
Preemptive_Schedulers.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JobsScheduler
{
class Preemptive_Schedulers
{
public static float avg_waiting = 0;
public static List<outputProcesses> output_processes = new List<outputProcesses>();
public static void Arrival_sort(List<Process> processes)
{
Process[] temp = new Process[1];
for (int i = 0; i < processes.Count; i++)
{
for (int n = i + 1; n < processes.Count - 1; n++)
{
if (processes[n].arrivalTime < processes[i].arrivalTime)
{
temp[0] = processes[i];
processes[i] = processes[n];
processes[n] = temp[0];
}
}
}
}
public static float getAvgWaiting(int iterator, float[] waiting_time)
{
float average_waiting = 0;
for (int i = 0; i < iterator; i++)
{
average_waiting += waiting_time[i];
}
return average_waiting = average_waiting / iterator;
}
public static void assignUsingTime(float[] start_time, string[] process_entry, List<outputProcesses> output_processes, int size)
{
for (int i = 0; i < size; i++)
{
output_processes.Add(new outputProcesses { processNumber = process_entry[i], usingTime = start_time[i + 1] - start_time[i], startTime = start_time[i] });
}
}
public static void Preemptive_Scheduler(List<Process> processes, string type)
{
float[] start_time = new float[40];
string[] Process_entry = new string[40];
float[] remainingT = new float[processes.Count];
float[] waiting_time = new float[processes.Count];
int counter = 0;
float time = 0;
int proc_index = 0;
float finish_time;
int st_iter = 0;
int p_it = 0;
float min = float.MaxValue;
bool job_flag = false;
bool enter_flag = false;
Arrival_sort(processes);
for (int i = 0; i < processes.Count; i++)
remainingT[i] = processes[i].burstTime;
if (processes[0].arrivalTime > time)
{
start_time[st_iter++] = 0;
Process_entry[p_it++] = "idle";
}
while (counter < processes.Count)
{
enter_flag = false;
for (int i = 0; i < processes.Count; i++)
{
var condition = (type == "SJF Preemptive") ? (remainingT[i] < min) : (processes[i].priority < min);
if ((processes[i].arrivalTime <= time) && condition && (remainingT[i] > 0))
{
min = (type == "SJF Preemptive") ? remainingT[i] : (float)processes[i].priority;
proc_index = i;
job_flag = true;
enter_flag = true;
}
}
if (job_flag == false)
{
time++;
continue;
}
if (enter_flag)
{
start_time[st_iter++] = time;
Process_entry[p_it++] = processes[proc_index].processNumber;
}
remainingT[proc_index]--;
if (type == "SJF Preemptive")
{
min = remainingT[proc_index];
if (min == 0)
min = int.MaxValue;
}
if (remainingT[proc_index] == 0)
{
if (type == "Priority Preemptive")
{
processes[proc_index].priority = int.MaxValue;
min = int.MaxValue;
}
counter++;
job_flag = false;
finish_time = time + 1;
waiting_time[proc_index] = finish_time -
processes[proc_index].arrivalTime -
processes[proc_index].burstTime;
if (waiting_time[proc_index] < 0)
waiting_time[proc_index] = 0;
}
time++;
}
start_time[st_iter] = time;
assignUsingTime(start_time, Process_entry, output_processes, p_it);
avg_waiting = getAvgWaiting(processes.Count, waiting_time);
}
}
}