-
Notifications
You must be signed in to change notification settings - Fork 4
/
Priority.cpp
executable file
·189 lines (154 loc) · 6.01 KB
/
Priority.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
//Preemptive Priority Scheduling
//July 30, 2018
//Authors: James Lopez
// Chantal Murdock
//Purpose: Make a program about four kinds of CPU scheduling:
// FCFS, Preemptive SJF and Priority, and Round Robin
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm> //Use for the sort function
#include <bits/stdc++.h>
#include <chrono>
#include <fstream>
using namespace std;
int stacks = 10000;
//Struct
struct Process {
int pid; //Process ID
int bt; //Burst Time
int art; //Arrival time
int ct; //Completion time
int wat; //Waiting time
int btt; //Burst time
int ta; //Turnaround time
int pr; //Priority
};
//Function to compare arrival times
bool compare(Process a, Process b) {
return a.art < b.art;
}
//Function to compare priority
bool compare2(Process a, Process b) {
return a.pr < b.pr;
}
int main(){
//Local variable
auto begin = chrono::high_resolution_clock::now();
double tstp;
int total_wt;
int total_tat;
int tat;
int wt[stacks];
double tst;
int sum;
double cpuUt;
//Object
Process process[stacks];
//File stream, opens the files
ifstream myfile;
ofstream schedFile;
myfile.open ("datagen.txt");
schedFile.open ("Prio_output.txt");
//Local variable
int i, j, pcom;
cout << "Processing... Please Wait..." << endl;
//Getting all the values from the output text file
for (i = 1; i <= stacks; i++) {
myfile >> process[i].pid;
myfile >> process[i].art;
myfile >> process[i].bt;
process[i].btt = process[i].bt; //It will be use for the algorithm
myfile >> process[i].pr;
}
//This function is from the algorithm.h header file which sort the process
//according to arrival time.
sort(process,process+stacks,compare);
i = 0;
pcom = 0;
//Processing each process until it completes
while(pcom < stacks){
for(j = 1; j <= stacks; j++){
if(process[j].art > i)
break;
}
//This function is from the algorithm.h header file which sort the process
//according to priority.
sort(process, process+j, compare2);
//Processing each process according to its priority
if(j > 0) {
for(j = 1; j <= stacks; j++){
if(process[j].bt != 0)
break;
}
if(process[j].art > i) {
i = process[j].art;
}
process[j].ct = i + 1; //Calculations for the completion time
process[j].bt--; //Decrement the burst time
}
pcom = 0;
i++;
//If the burst time is 0 proceed to the next process
for(j = 1; j <= stacks; j++) {
if(process[j].bt == 0)
pcom++;
}
}
//Outputs them into a text file
schedFile << "Process " << "\t" << "Arrival Time" << "\t" << "Burst Time" << "\t" << "Priority"
<< "\t" << "Completion Time " << "\t" << "Waiting Time " << "\t" << "Turnaround Time" << endl;
schedFile<<endl;
//Processing the each process
for(i = 1; i <= stacks; i++){
total_wt = 0;
total_tat = 0;
process[i].wat = process[i].ct - process[i].art; //Calculating the waiting time
process[i].ta = process[i].wat + process[i].btt; //Calculating the turnaround time
total_wt = total_wt+ process[i].wat; //Calculating the total waiting time
total_tat = total_tat + process[i].ta; //Calculating the total turnaround time
tst = (double)tst + process[i].ct; //Calculating the total completion time/service time
tstp = (double)tstp + process[i].pid; //Calculating the total number of process
//Output the result into a text file
schedFile << process[i].pid << "\t\t" << process[i].art << "\t\t" << process[i].btt << "\t\t" << process[i].pr
<< "\t\t" << process[i].ct << "\t\t" << "\t\t" << process[i].wat << "\t\t" << process[i].ta;
schedFile << endl;
}
//Calculations for the elapsed time
auto end = chrono::high_resolution_clock::now();
auto dur = end - begin;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
//Calculations for CPU UTILIZATION and CPU IDLE
double CPUUTL = 0;
double CPUIDL = 0;
CPUUTL = ((tstp/tst))*100;
CPUIDL = 100 -CPUUTL;
schedFile << "\n_________________________________________________________" << endl;
schedFile << "\n Statistics for the RUN" << endl;
schedFile << "\n Number of processes: " << stacks;
schedFile << "\n Total Elapsed time: " << ms << " ms ";
schedFile << "\n Average Waiting Time = "
<< total_wt;
schedFile << "\n Average Turn Around time = "
<< total_tat;
schedFile << " Throughput = "
<< (double)stacks/ms << endl; //Calculations for throughput
schedFile << " CPU utilization = Usage: "
<< CPUUTL << "%";
schedFile << " Idle: " << CPUIDL << "%";
schedFile << "\n Average Waiting Time = "
<< (double)total_wt/stacks; //Calculations for the average waiting time
schedFile << " ms";
schedFile << "\n Average Turnaround Time = "
<< (double)total_tat/stacks; //Calculations for the average turnaround time
schedFile << " ms" << endl;
schedFile << " Average response time = "
<< (tst-process[stacks].ct)/stacks << "ms" << endl; //Calculations for the average response time
schedFile << "_________________________________________________________" << endl;
cout << "\nThe Preemptive Priority Scheduling Has Been Successfully Processed!" << endl
<< "\nPlease Look the Output in the File Named Prio_output.txt" << endl;
//Closing the files
myfile.close();
schedFile.close();
return 0;
}