-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_sa_pm_vp.cpp
169 lines (145 loc) · 4.39 KB
/
priority_sa_pm_vp.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
// C++ Program to demonstrate the working of Priority Based Scheduling Algorithm - Preemptive
// it consider the arrival time in and out order
// by Vijay Purohit
// working and tested on Dev-C++ 5.11
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
typedef struct process
{
int id; // process id
int at; // process arrival time
int bt; // process burst time
int rt; // process remaining time after completing time quantum
int st; // process starting time
int ft; // process finish time
int pr; // process priority
float wt,tat; // process waiting time and time quantum
}process;
process p[20],ps[20],temp; // main process and ps= process sorted.
int priority_sel;
int accept() //functions for accepting inputs from the user
{
int i,n;
cout<<"Enter total number of processes(maximum 20):";
cin>>n;
if(n==0){
cout<<"\nInvalid no. of process"; exit(1);
}
for(i=1;i<=n;i++)
{
cout<<"\nEnter Arrival Time and Burst Time for Process P["<<i<<"] : Ar :";
p[i].id=i; // storing process id or no
cin>>p[i].at;
cout<<"\t\t\t\t\t\t Ex: ";
cin>>p[i].bt;
p[i].rt=p[i].bt; // copying burst time to remaining time
cout<<"\t\t\t\t\t\t Priority: ";
cin>>p[i].pr;
ps[i]=p[i]; // copying of one process into another process(dummy process)
}
cout<<"\n1. Large No. -> High Priority";
cout<<"\n2. Small No. -> High Priority\n : ";
cin>>priority_sel ;
return n;
}
void sort_arrival(int n)
{
int i,j;
for(i=2;i<=n;i++)
for(j=1;j<i;j++)
if(ps[j].at>ps[i].at)
{
temp=ps[i];
ps[i]=ps[j];
ps[j]=temp;
}
else if(ps[j].at==ps[i].at) //if both the arrival time are equal then sort on the basis of process priority number
if(ps[j].pr > ps[i].pr && priority_sel==2)
{
temp=ps[i];
ps[i]=ps[j];
ps[j]=temp;
}
else if(ps[j].pr < ps[i].pr && priority_sel==1)
{
temp=ps[j];
ps[j]=ps[i];
ps[i]=temp;
}
}
main()
{
int i, j=1, k, l, n, m, count=0, max, min, time=0,tq=1;
float sum_waittime=0, sum_turnaround=0;
int ganttP[50],ganttStartTime[50], ganttStopTime[50];
cout << fixed;
cout.precision(2);
n = accept();
sort_arrival(n);
time = ps[1].at; // starting time is equal to arrival time for first process
// m = ps[1].id;
while(count!=n)
{
min=9999;
max=0;
for(k=1;k<=n;k++)
{
if(ps[k].at <= time && ps[k].pr < min && ps[k].rt>0 && priority_sel==2)
{
min = ps[k].pr;
m=ps[k].id;
l=k;
}
else if(ps[k].at <= time && ps[k].pr > max && ps[k].rt>0 && priority_sel==1)
{
max = ps[k].pr;
m=ps[k].id;
l=k;
}
}
if(time < ps[n].at) // if all process are not arrived
{
ganttP[j] = m ; // process no
ganttStartTime[j] = time;
p[m].rt -= tq; // remaining time is decrease to one unit
time += tq;
ganttStopTime[j++] = p[m].ft = time;
if( ps[k].at > time ) // if next process arrival time is greater than current process finish time
time = ps[k].at;
}
else // if all the process are arrived
{
ganttP[j] = m ;
ganttStartTime[j] = time;
time += p[m].rt; // it will execute till its burst time
p[m].rt = 0;
ganttStopTime[j++] = p[m].ft = time;
}
ps[l].rt = p[m].rt; // updating sorted process remaining time
//calculating waiting time
if(p[m].rt == 0)
{
count++;
p[m].tat = p[m].ft - p[m].at;
p[m].wt = p[m].tat - p[m].bt;
sum_turnaround += p[m].tat;
sum_waittime += p[m].wt;
}
}
cout<<"\n Gantt Chart is = P[NO](start time, stop time) : \n";
for(i=1;i<j;i++)
{
printf("p[%d](%d,%d) || ", ganttP[i], ganttStartTime[i], ganttStopTime[i]);
}
printf("\n===========================/=================================");
printf("\n\nProcess\t | Arrival Time | Waiting Time | Completion T | Turnaround T | \n\n");
for(i=1;i<=n;i++)
{
printf(" P[%d] \t|\t %d\t|\t %.2f\t|\t %d \t|\t %.2f |\n",p[i].id, p[i].at, p[i].wt, p[i].ft, p[i].tat);
}
cout<<"\nAverage Waiting Time = "<<sum_waittime/n;
cout<<"\nAvg TurnAround Time = "<<sum_turnaround/n;
}