-
Notifications
You must be signed in to change notification settings - Fork 1
/
SJF.cpp
98 lines (85 loc) · 2.7 KB
/
SJF.cpp
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 <iostream>
using namespace std;
struct Process
{
int num, arrive, burst, priority;
};
void sort(Process array[], int size)
{
for (int i = 0; i < size; i++)
for (int j = i + 1; j < size; j++)
if (array[i].arrive > array[j].arrive)
{
Process temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int ShortestJobFirst(Process processes[], int size, bool preemptive)
{
int totalWaitTime = 0;
// Order Received Processes List By Arrive Time
// We Need To Order By Arrive Time Because Many Processes May Have The Same Burst Time So We Choose What Came First
sort(processes, size);
// Copy Received Processes List To New List
Process list[size];
for (int i = 0; i < size; i++)
list[i] = processes[i];
// Time Started... Let's Go Throw Processes
int time = 0;
while (true)
{
// 1) Find Shortest Job
int minIndex = -1;
int minBurst = 1000;
for (int i = 0; i < size; i++)
{
if (time >= list[i].arrive // If Process Has Arrived
&& list[i].burst > 0 // And Not Used Before
&& list[i].burst < minBurst)
{
minIndex = i;
minBurst = list[i].burst;
}
}
// 2) If The Process Was Ready (Arrived)
if (minIndex != -1)
{
// Don't Leave The Process Until It Is Finished (If Not Preemptive)
do
{
cout << time << " ~ " << time + 1 << "\t\t" << list[minIndex].num << endl;
list[minIndex].burst--;
time++;
} while (!preemptive && list[minIndex].burst > 0);
if (list[minIndex].burst == 0)
totalWaitTime += time - processes[minIndex].arrive - processes[minIndex].burst;
}
// 3) If No Process Is Ready
else
{
// Check If There Is Any UnFinished Process
bool allFinished = true;
for (int i = 0; i < size; i++)
if (list[i].burst > 0)
{
allFinished = false;
cout << time << " ~ " << time + 1 << endl;
time++;
break;
}
if (allFinished)
break;
}
}
return totalWaitTime;
}
int main(int argc, char const *argv[])
{
Process processes[] = {{1, 0, 1, 3}, {2, 3, 8, 4}, {3, 4, 5, 2}, {4, 5, 2, 2}, {5, 8, 4, 1}};
int totalWaitTime = 0;
totalWaitTime = ShortestJobFirst(processes, 5, false);
cout << "Total Waiting Time = " << totalWaitTime;
getchar();
return 0;
}