-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityScheduling-DifferentArrivalTime.cpp
165 lines (148 loc) · 5.63 KB
/
PriorityScheduling-DifferentArrivalTime.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
#include <iostream>
using namespace std;
/* Note
I have used a multiDimensional array to store the
process id, priority of each process and
burst time, arrival time of each process.
These values are stored in the following manner in the table:
column 1: Process ID
column 2: Priority
column 3: burst time
column 4: arrival time
*/
int arrivalTimeCol = 3;
int burstTimeCol = 2;
int pIDCol = 0;
int priorityCol = 1; //priority index
struct process {
int noOfProcess; //Total number of process
int** processList; //List to store process ID's,brust Time of each process
int* waitingTime; //List to sotre waiting Time of each process
int* completionTime; //List to sotre completion Time of each process
int* turnAroundTime; //List to store turnaround time for each process
process(int processCount){ //parameterized constructor to initilize arrays dynamically
//Setting total no of process
noOfProcess = processCount;
// Dynamically allocate memory
processList = new int* [processCount];
for (int i = 0; i < processCount; i++)
processList[i] = new int[4];
for(int i = 0; i < processCount; i++){
for(int j=0; j < 4; j++){
processList[i][j] =0;
}
}
waitingTime = new int [processCount];
turnAroundTime = new int [processCount];
completionTime = new int [processCount];
}
};
void userPrompt(process p); //Function prototype to ask user for input
void print(process p); //Function prototype to print all the outputs
void waitingTime(process p); //Function prototype to calclate waiting time for each process
void completionTime(process p); //Function prototype to calclate completion time for each process
void turnAroundTime(process p);//Function prototype to calclate turn around time for each process
void avgWaitingAndTurnAroundTime(process p); //Function prototype to calclate avg waiting time and avg turn around time
int main(){
//User prompt for number of process
cout<<"ENter the total number of process: ";
int noOfProcess;
cin >> noOfProcess;
process p(noOfProcess); //Declaring instance of struct process
userPrompt(p); //Calling function to take iput from user
completionTime(p); // calling function to calculate completion time of each process
turnAroundTime(p);// calling function to calclate turn around time for each process
waitingTime(p); //calling function to calclate waiting time for each process
print(p); //Printing results
avgWaitingAndTurnAroundTime(p); //callinf function to calclate avg waiting time and turn around time
}
void userPrompt(process p){
int burstTime;
int pID;
int priority;
int arrivalTime;
for(int i = 0; i < p.noOfProcess; ++i){
pID=i+1;
cout<<"Enter Brust time for process-"<< pID <<" "; //User prompt to enter burst time
cin>>burstTime; //Taking input from user
cout<< "ENter the priority for process-"<< pID <<" ";
cin>>priority;
cout<< "Enter the arrival time for process-"<< pID <<" ";
cin>> arrivalTime;
p.processList[i][pIDCol] = pID;
p.processList[i][priorityCol] = priority;
p.processList[i][burstTimeCol] = burstTime;
p.processList[i][arrivalTimeCol]= arrivalTime;
}
for (int i = 0; i < p.noOfProcess; i++) {
int min = i;
for (int j = i + 1; j < p.noOfProcess; j++) {
if (p.processList[j][arrivalTimeCol] <= p.processList[min][arrivalTimeCol]){
if (p.processList[j][arrivalTimeCol] == p.processList[min][arrivalTimeCol]){
if (p.processList[j][priorityCol] > p.processList[min][priorityCol]){
min = j;
}
}
else
{
min = j;
}
}
}
int tempID = p.processList[i][0];
int tempPriority = p.processList[i][1];
int tempBurstTime = p.processList[i][2];
int tempArrivalTime = p.processList[i][3];
p.processList[i][0] = p.processList[min][0];
p.processList[i][1] = p.processList[min][1];
p.processList[i][2] = p.processList[min][2];
p.processList[i][3] = p.processList[min][3];
p.processList[min][0] = tempID;
p.processList[min][1] = tempPriority;
p.processList[min][2] = tempBurstTime;
p.processList[min][3] = tempArrivalTime;
}
}
//Print all the process
void print(process p){
for (int i = 0; i < p.noOfProcess; ++i) {
cout << "\nProcess ID: " << p.processList[i][0]
<< " Burst Time: " << p.processList[i][2]
<< " Priority: "<<p.processList[i][1]
<< " Waiting Time: " << p.waitingTime[i]
<< " turn around time: "<< p.turnAroundTime[i]
<< " completion time: "<< p.completionTime[i];
}
}
////Functionto calclate waiting time for each process
void waitingTime(process p){
p.waitingTime[0] = 0;
for (int i = 1; i < p.noOfProcess; i++ ) {
p.waitingTime[i] = p.turnAroundTime[i] - p.processList[i][2];
}
}
//COmpletion time of each process
void completionTime(process p){
p.completionTime[0] = p.processList[0][2];
for(int i = 1; i < p.noOfProcess; i++){
p.completionTime[i] = p.completionTime[i - 1] + p.processList[i][2];
}
}
//Turn around time for each process
void turnAroundTime(struct process p) {
for (int i = 0; i < p.noOfProcess; i++) {
p.turnAroundTime[i] = p.completionTime[i] - p.processList[i][3];}
}
//Avg waiting and Turn around Time
void avgWaitingAndTurnAroundTime(process p){
int totalWaitingTime=0;
int totalTurnAroundTime=0;
for(int i = 0; i<p.noOfProcess; i++){
totalWaitingTime += p.waitingTime[i];
totalTurnAroundTime += p.turnAroundTime[i];
}
int avgWaiting = (float)totalWaitingTime/(float)p.noOfProcess;
int avgTurnAround = (float)totalTurnAroundTime / (float)p.noOfProcess;
cout << "\n\nAverage waiting time = "<< avgWaiting <<endl;
cout<<"Average turn around time = "<< avgTurnAround <<endl;
}