-
Notifications
You must be signed in to change notification settings - Fork 1
/
RR.cpp
132 lines (115 loc) · 3.28 KB
/
RR.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
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
#include <iostream>
using namespace std;
class Queue
{
private:
int front, rear, counter, size;
int *array;
public:
int length() { return counter; }
bool isFull() { return counter == size; }
bool isEmpty() { return counter == 0; }
Queue(int init_size)
{
size = init_size;
array = new int[size];
front = rear = counter = 0;
}
void enqueue(int data)
{
array[rear] = data;
rear = (rear + 1) % size;
counter++;
}
int dequeue(void)
{
int temp = array[front];
front = (front + 1) % size;
counter--;
return temp;
}
};
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 RoundRobin(Process processes[], int size, int quantum)
{
int totalWaitTime = 0;
// Order Received Processes List By Arrive Time
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, enqueued = 0, current = 0;
// Enqueue All myQueue With Arrive = 0
Queue myQueue(size);
for (int i = 0; i < size; i++)
{
if (list[i].arrive == 0)
{
myQueue.enqueue(i);
enqueued++;
}
}
// While Queue Has Items Or List Has Processes Not Enqueued Yet
while (!myQueue.isEmpty() || enqueued < size)
{
// If Queue Has Items
if (!myQueue.isEmpty())
{
current = myQueue.dequeue();
// Hold The Dequeued Process For A quantum Period
for (int i = 0; i < quantum; i++)
// If The Process Not Finished
if (list[current].burst > 0)
{
cout << time << " ~ " << time + 1 << "\t\t" << list[current].num << endl;
list[current].burst--;
time++;
}
// If The Process Finished During quantum Period
if (list[current].burst == 0)
totalWaitTime += time - processes[current].arrive - processes[current].burst;
}
// If List Has Processes Not Enqueued Yet
else
{
cout << time << " ~ " << time + 1 << endl;
time++;
}
// Enqueue The Processes That Arrived During quantum Period
for (int i = enqueued; i < size; i++)
if (list[i].arrive <= time)
{
myQueue.enqueue(i);
enqueued++;
}
// If The Current Processes Not Finished
if (list[current].burst > 0)
myQueue.enqueue(current);
}
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 = RoundRobin(processes, 5, 3);
cout << "Total Waiting Time = " << totalWaitTime;
getchar();
return 0;
}