Skip to content

Commit 23005e8

Browse files
committed
Priority
1 parent 47b2229 commit 23005e8

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Priority.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
struct Process
6+
{
7+
int num, arrive, burst, priority;
8+
};
9+
10+
void sort(Process array[], int size)
11+
{
12+
for (int i = 0; i < size; i++)
13+
for (int j = i + 1; j < size; j++)
14+
if (array[i].arrive > array[j].arrive)
15+
{
16+
Process temp = array[i];
17+
array[i] = array[j];
18+
array[j] = temp;
19+
}
20+
}
21+
22+
int Priority(Process processes[], int size, bool primitive)
23+
{
24+
int totalWaitTime = 0;
25+
26+
// Order Received Processes List By Arrive Time
27+
// We Need To Order By Arrive Time Because Many Processes May Have The Same Priority So We Choose What Came First
28+
sort(processes, size);
29+
30+
// Copy Received Processes List To New List
31+
Process *list = new Process[size];
32+
for (int i = 0; i < size; i++)
33+
list[i] = processes[i];
34+
35+
// Time Started... Let's Go Throw Processes
36+
int time = 0;
37+
while (true)
38+
{
39+
// 1) Find Highest Priority Number
40+
int minIndex = -1;
41+
int minPriority = 1000;
42+
for (int i = 0; i < size; i++)
43+
{
44+
if (time >= list[i].arrive // If Process Has Arrived
45+
&& list[i].burst > 0 // And Not Used Before
46+
&& list[i].priority < minPriority)
47+
{
48+
minIndex = i;
49+
minPriority = list[i].priority;
50+
}
51+
}
52+
53+
// 2) If The Process Was Ready (Arrived)
54+
if (minIndex != -1)
55+
{
56+
// Don't Leave The Process Until It Is Finished
57+
do
58+
{
59+
cout << time << " ~ " << time + 1 << "\t\t" << list[minIndex].num << endl;
60+
list[minIndex].burst--;
61+
time++;
62+
} while (!primitive && list[minIndex].burst > 0);
63+
if (list[minIndex].burst == 0)
64+
totalWaitTime += time - processes[minIndex].arrive - processes[minIndex].burst;
65+
}
66+
// 3) If No Process Is Ready
67+
else
68+
{
69+
// Check If There Is Any UnFinished Process
70+
bool allFinished = true;
71+
for (int i = 0; i < size; i++)
72+
if (list[i].burst > 0)
73+
{
74+
allFinished = false;
75+
cout << time << " ~ " << time + 1 << endl;
76+
time++;
77+
break;
78+
}
79+
80+
if (allFinished)
81+
break;
82+
}
83+
}
84+
85+
return totalWaitTime;
86+
}
87+
88+
int main(int argc, char const *argv[])
89+
{
90+
Process processes[] = {{1, 0, 1, 3}, {2, 3, 8, 4}, {3, 4, 5, 2}, {4, 5, 2, 2}, {5, 8, 4, 1}};
91+
92+
int totalWaitTime = 0;
93+
totalWaitTime = Priority(processes, 5, false);
94+
cout << "Total Waiting Time = " << totalWaitTime;
95+
96+
getchar();
97+
return 0;
98+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
- First Come, First Served ( `FCFS` )
66
- Shortest Job First ( **Preemptive** / **Non-Preemptive** ) ( `SJF` )
7+
- Priority ( **Preemptive** / **Non-Preemptive** )

0 commit comments

Comments
 (0)