-
Notifications
You must be signed in to change notification settings - Fork 0
/
os project.cpp
308 lines (289 loc) · 8.73 KB
/
os project.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <iostream>
#include <vector>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <iomanip>
using namespace std;
int sleepcount = 0;
static int index=0;
int n;
int x = 0;
float total_waiting_time = 0;
float total_turnaround_time = 0;
float avg_waiting_time = 0;
float avg_turnaround_time = 0;
int GANTTindex[100], k=0, l=0, GANTTpid[100];
int current_time = 0;
bool check = false;
int anothervar = -1;
int PID[100], PAT[100], PBT[100], PFT[100];
int TAT[100], WT[100];
int RT[100];
sem_t mutex;
sem_t completed;
struct Process{
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
int completion_time;
int remaining_time;
};
vector<Process> processes;
bool checkshortest(Process *p)
{
bool shortest_remaining_time = true;
for (int i = 0; i < processes.size(); i++) {
if (processes[i].remaining_time > 0 && processes[i].arrival_time <= current_time && i != p->pid - 1) {
if (processes[i].remaining_time < p->remaining_time)
{
shortest_remaining_time = false;
break;
}
else if (processes[i].remaining_time == p->remaining_time && processes[i].arrival_time < p->arrival_time)
{
shortest_remaining_time = false;
break;
}
}
}
return shortest_remaining_time;
}
bool check_process_arrival()
{
bool arrived = false;
for (int i = 0; i < processes.size(); i++)
{
if (processes[i].remaining_time > 0 && processes[i].arrival_time <= current_time)
{
arrived = true;
break;
}
}
return arrived;
}
void* srtf_algorithm(void* arg)
{
Process* process = (Process*) arg;
cout << "Process: P" << process->pid << " has arrived! thread created for P" << process->pid << "\n";
process->remaining_time = process->burst_time;
sleepcount--;
sleep(sleepcount);
while (process->remaining_time > 0)
{
if(process->arrival_time <= current_time)
{
sem_wait(&mutex);
bool flag = checkshortest(process);
if(flag == true)
{
process->remaining_time--;
current_time++;
process->completion_time = current_time;
}
sem_post(&mutex);
}
else
{
if(!check_process_arrival())
{
sem_wait(&mutex);
current_time++;
sem_post(&mutex);
}
}
}
process->turnaround_time = process->completion_time - process->arrival_time;
process->waiting_time = process->turnaround_time - process->burst_time;
sem_post(&completed);
return NULL;
}
void SRTFGanttChart(){
int Complete = 0, Current_Time = 0, minimum = 9999999;
int Smallest = 0;
bool Check = false;
int Anothervar = -1;
GANTTindex[k] = Current_Time;
k++;
while(Complete != n){
for(int j=0 ; j<n ; j++){
if((PAT[j]<=Current_Time) && (RT[j]<minimum) && (RT[j] > 0)){
minimum = RT[j];
Smallest = j;
Check = true;
}
}
if(Check == false){
Current_Time++;
if(GANTTpid[l-1] == -1){
GANTTindex[k-1] = Current_Time;
}
else{
GANTTindex[k] = Current_Time;
k++;
GANTTpid[l] = -1;
l++;
}
continue;
}
RT[Smallest]--;
minimum = RT[Smallest];
if(minimum == 0){
minimum = 9999999;
}
if(RT[Smallest] == 0){
Complete++;
Check = false;
PFT[Smallest] = Current_Time + 1;
GANTTindex[k] = PFT[Smallest];
k++;
GANTTpid[l] = PID[Smallest];
l++;
}
Current_Time++;
if(Anothervar != Smallest && RT[Anothervar] != 0){
if(Anothervar != -1){
GANTTindex[k] = Current_Time-1;
k++;
GANTTpid[l] = PID[Anothervar];
l++;
}
}
Anothervar = Smallest;
}
}
int main(){
cout << "\n---------------------------------------------------------\n";
cout << "|\t\tSELECT PREEMPTIVE SCHEDULING ALGORITHM\t|\n";
cout << "---------------------------------------------------------\n";
cout << "\nEnter the Number of Processes: ";
cin >> n;
sleepcount = n;
sem_init(&mutex, 0, 1);
sem_init(&completed, 0, 0);
for (int i = 0; i < n; i++)
{
Process p;
p.pid = i + 1;
PID[i] = p.pid;
cout << "Enter Arrival Time and Burst Time for Process " << i + 1 << ": ";
cin >> p.arrival_time >> p.burst_time;
p.remaining_time = p.burst_time;
PAT[i] = p.arrival_time;
PBT[i] = p.burst_time;
RT[i] = p.remaining_time;
processes.push_back(p);
}
cout<<endl;
cout<<"-----------------------------"<<endl;
cout<<"| Process | Arrival | Burst |"<<endl;
cout<<"| ID | Time | Time |"<<endl;
cout<<"-----------------------------"<<endl;
for(int i=0 ; i<n ; i++){
if(processes[i].pid >= 10){
cout<<"| P"<<processes[i].pid<<" |"<<setw(5)<<processes[i].arrival_time<<" |"<<setw(4)<<processes[i].burst_time<<" |"<<endl;
}
else{
cout<<"| P"<<processes[i].pid<<" |"<<setw(5)<<processes[i].arrival_time<<" |"<<setw(4)<<processes[i].burst_time<<" |"<<endl;
}
}
cout<<"-----------------------------"<<endl;
pthread_t thread_id[n];
cout << endl;
for (int i = 0; i < n; i++)
{
pthread_create(&thread_id[i], NULL, srtf_algorithm, (void*) &processes[i]);
}
int completed_processes = 0;
while (completed_processes < n)
{
sem_wait(&completed);
completed_processes++;
}
sem_destroy(&mutex);
sem_destroy(&completed);
cout<<endl;
cout<<"----------------------------------------------------------------"<<endl;
cout<<"| Process | Arrival | Burst | Finishing | Turnaround | Waiting |"<<endl;
cout<<"| ID | Time | Time | Time | Time | Time |"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
for (int i = 0; i < n; i++) {
if(processes[i].pid >= 10){
cout<<"| P"<<processes[i].pid<<" |"<<setw(5)<<processes[i].arrival_time<<" |"<<setw(4)<<processes[i].burst_time<<" |"<<setw(6)<<processes[i].completion_time<<" |"<<setw(7)<<processes[i].turnaround_time<<" |"<<setw(5)<<processes[i].waiting_time<<" |"<<endl;
}
else{
cout<<"| P"<<processes[i].pid<<" |"<<setw(5)<<processes[i].arrival_time<<" |"<<setw(4)<<processes[i].burst_time<<" |"<<setw(6)<<processes[i].completion_time<<" |"<<setw(7)<<processes[i].turnaround_time<<" |"<<setw(5)<<processes[i].waiting_time<<" |"<<endl;
}
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
cout<<"----------------------------------------------------------------"<<endl;
avg_waiting_time = total_waiting_time / n;
avg_turnaround_time = total_turnaround_time / n;
cout<<endl;
cout << "Average Waiting Time : " << avg_waiting_time << endl;
cout << "Average Turnaround Time : " << avg_turnaround_time << endl;
SRTFGanttChart();
for(int i=0 ; i<n-1; i++){
for(int j=i+1 ; j<n; j++){
if(processes[j].arrival_time < processes[i].arrival_time){
Process Temp = processes[i];
processes[i] = processes[j];
processes[j] = Temp;
}
else if(processes[j].arrival_time == processes[i].arrival_time){
if(processes[j].pid < processes[i].pid){
Process Temp = processes[i];
processes[i] = processes[j];
processes[j] = Temp;
}
}
}
}
cout<<endl;
cout<<"\t\t--> GANTT Chart <--"<<endl;
for(int i=0 ; i<l ; i++){
if(GANTTpid[i] >= 10 || i==l-1){
cout<<"------";
}
else{
cout<<"-----";
}
}
cout<<endl;
cout<<" |";
for(int i=0 ; i<l ; i++){
if(GANTTpid[i] == -1){
cout<<" ~~ |";
}
else{
cout<<" P"<<GANTTpid[i]<<" |";
}
}
cout<<endl;
cout<<" ";
for(int i=0 ; i<l ; i++){
if(GANTTpid[i] >= 10 || i==l-1){
cout<<"------";
}
else{
cout<<"-----";
}
}
cout<<endl;
for(int i=0 ; i<k ; i++){
if(i==0){
cout<<" "<<GANTTindex[i];
}
else if(GANTTpid[i-1] >= 10){
cout<<setw(6)<<GANTTindex[i];
}
else{
cout<<setw(5)<<GANTTindex[i];
}
}
cout<<endl;
return 0;
}