-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorpremp.hpp
154 lines (127 loc) · 2.71 KB
/
priorpremp.hpp
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
/*valid for different and unique arrival times*/
#include<iostream>
using namespace std;
void priority_premp()
{
struct process1
{
int prior1;
int pid;
int at;
int bt;
int rt;
}pro1[100];
int wt[100],tat[100],sum=0,sum1=0,n,ct[100];
int max=0,t=0,comp=0;
cout<<"enter the no of processes";
cin>>n;
for(int i=0;i<n;i++)
{
pro1[i].pid=i+1;
cout<<"enter the arrival time of "<<i+1<<"process";
cin>>pro1[i].at;
cout<<"enter the burst time of "<<i+1<<"process";
cin>>pro1[i].bt;
cout<<"enter the priority of "<<i+1<<"process";
cin>>pro1[i].prior1;
pro1[i].rt=pro1[i].bt;
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(pro1[i].at>pro1[j].at)
{
int temp=pro1[j].prior1;
pro1[j].prior1=pro1[i].prior1;
pro1[i].prior1=temp;
int mp=pro1[j].bt;
pro1[j].bt=pro1[i].bt;
pro1[i].bt=mp;
int kp=pro1[j].pid;
pro1[j].pid=pro1[i].pid;
pro1[i].pid=kp;
int tp=pro1[j].at;
pro1[j].at=pro1[i].at;
pro1[i].at=tp;
int mop=pro1[j].rt;
pro1[j].rt=pro1[i].rt;
pro1[i].rt=mop;
}
if(pro1[i].at==pro1[j].at)
{
if(pro1[i].prior1<pro1[j].prior1)
{
int ep=pro1[j].prior1;
pro1[j].prior1=pro1[i].prior1;
pro1[i].prior1=ep;
int mop=pro1[j].bt;
pro1[j].bt=pro1[i].bt;
pro1[i].bt=mop;
int kop=pro1[j].pid;
pro1[j].pid=pro1[i].pid;
pro1[i].pid=kop;
}
}
}
}
for(int j=0;j<n;j++)
{
while(t<pro1[j].at)
{
t++;
}
for(int i=0;i<n;i++)
{
if(t==pro1[i].at)
{
pro1[i].rt--;
ct[i]=t+1;
}
}
t++;
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(pro1[i].prior1<pro1[j].prior1)
{
int ep=pro1[j].prior1;
pro1[j].prior1=pro1[i].prior1;
pro1[i].prior1=ep;
int mop=pro1[j].bt;
pro1[j].bt=pro1[i].bt;
pro1[i].bt=mop;
int kop=pro1[j].pid;
pro1[j].pid=pro1[i].pid;
pro1[i].pid=kop;
int mp=pro1[j].rt;
pro1[j].rt=pro1[i].rt;
pro1[i].rt=mp;
int eop=pro1[j].at;
pro1[j].at=pro1[i].at;
pro1[i].at=eop;
}
}
}
ct[0]=t+pro1[0].rt;
for(int i=1;i<n;i++)
{
ct[i]=ct[i-1]+pro1[i].rt;
}
for(int i=0;i<n;i++)
{
tat[i]=ct[i]-pro1[i].at;
wt[i]=tat[i]-pro1[i].bt;
sum=sum+wt[i];
sum1=sum1+tat[i];
}
cout<<"process_id\t"<<"at\t"<<"prior\t"<<"bt\t"<<"ct\t"<<"tat\t"<<"wt\n";
for(int i=0;i<n;i++)
{
cout<<pro1[i].pid<<"\t\t"<<pro1[i].at<<"\t"<<pro1[i].prior1<<"\t"<<pro1[i].bt<<"\t"<<ct[i]<<"\t"<<tat[i]<<"\t"<<wt[i]<<"\n";
}
cout<<"average waiting tym is"<<(float)sum/n;
cout<<"\naverage turn around tym is "<<(float)sum1/n;
}