-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnPP_scheduling.cpp
192 lines (168 loc) · 4.38 KB
/
nPP_scheduling.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
#include<iostream>
#include<iomanip>
#include<conio.h>
// practical 9
using namespace std;
struct Process
{
int pid;
int burst_time;
int waiting_time;
int response_time;
int turnaround_time;
int arrival_time;
int completion_time;
int starting_time;
int priority;
};
class NPPS
{
int n, total_tat;
float avg_tat;
Process *p;
public:
NPPS();
void input();
void arrival_sort();
void sort(int, int);
void sort2();
void calc();
void GC();
void output();
};
NPPS :: NPPS()
{
total_tat = 0;
avg_tat = 0;
}
void NPPS :: input()
{
cout<<endl<<endl<<"Enter the total number of processes : ";
cin>>n;
p = new Process[n];
cout<<endl<<endl<<"Enter the arrival time of each process : "<<endl;
for(int i=0; i<n; i++)
{
p[i].pid = i+1;
cout<<"[P"<<p[i].pid<<"] : ";
cin>>p[i].arrival_time;
}
cout<<endl<<endl<<"Enter the burst time of each process : "<<endl;
for(int i=0; i<n; i++)
{
cout<<"[P"<<p[i].pid<<"] : ";
cin>>p[i].burst_time;
}
cout<<endl<<endl<<"Enter the priority of each process : "<<endl;
for(int i=0; i<n; i++)
{
cout<<"[P"<<p[i].pid<<"] : ";
cin>>p[i].priority;
}
}
void NPPS :: sort2()
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n-1-i; j++)
{
if(p[j].arrival_time > p[j+1].arrival_time)
swap(p[j], p[j+1]);
}
}
for(i=0; i<n; i++)
{
p[i].starting_time = (i==0)? p[i].arrival_time : max(p[i].arrival_time, p[i-1].completion_time);
p[i].completion_time = p[i].burst_time + p[i].starting_time;
for(j=i; j<n; j++)
{
if(p[i].completion_time < p[j+1].arrival_time)
break;
}
if(p[i].arrival_time == p[i+1].arrival_time)
sort(i,j);
else
sort(i+1, j);
}
}
void NPPS :: sort(int x, int y)
{
Process *temp = new Process[y-x+1];
int i, j;
for(j=0,i=x; i<=y; i++)
{
temp[j] = p[i];
j++;
}
for(i=0; i<y-x+1; i++)
{
for(j=0; j<y-x-i; j++)
{
if(temp[j].priority > temp[j+1].priority)
{
swap(temp[j], temp[j+1]);
}
}
}
for(j=0,i=x; i<=y; i++)
{
p[i] = temp[j];
j++;
}
delete temp;
}
void NPPS :: calc()
{
for(int i=0; i<n; i++)
{
p[i].starting_time = (i == 0) ? p[i].arrival_time : max(p[i].arrival_time, p[i-1].completion_time);
p[i].completion_time = p[i].starting_time + p[i].burst_time;
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
p[i].response_time = p[i].waiting_time;
total_tat += p[i].turnaround_time;
}
avg_tat = total_tat/n;
}
void NPPS :: GC()
{
int i;
cout<<setw(40)<<"\n\n ~~~~~~~~~~~~~~~~~~~~~~~~ GANTT CHART ~~~~~~~~~~~~~~~~~~~~~~~~~ \n\n";
cout<<endl;
cout<<"";
for(i=0; i<n; i++)
{
cout<<"\t P"<<p[i].pid<<"\t";
}
cout<<""<<endl<<endl;
cout<<p[0].arrival_time<<"\t\t";
for(i=0; i<n; i++)
{
cout<<p[i].completion_time<<"\t\t";
}
cout<<endl<<endl;
}
void NPPS :: output()
{
cout<<" Processes "<<" Arrival time "<<" Burst time "<<" Completion time "<<" Waiting time "<< " Response time "<<" Turnaround time "<<endl;
for (int i=0; i<n; i++)
{
cout<< " "<<p[i].pid << "\t\t"<<p[i].arrival_time <<" \t "<<p[i].burst_time<<" \t\t "<<p[i].completion_time<<" \t\t "<<p[i].waiting_time<<" \t "<<p[i].response_time <<"\t " << p[i].turnaround_time << endl;
}
cout<<endl;
cout<<"Total Turnaround time : "<<total_tat<<endl;
cout<<"Total Average time : "<<avg_tat<<endl;
}
int main()
{
NPPS ob;
cout<<"\n ~~~~~~~~ Non-preemptive priority based scheduling algorithm ~~~~~~~~~ \n";
ob.input();
ob.sort2();
ob.calc();
ob.GC();
ob.output();
getch();
return 0;
}