-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiedrr.cpp
70 lines (62 loc) · 1.32 KB
/
modifiedrr.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
#include<bits/stdc++.h>
using namespace std;
int min(int a, int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
// Declaring vector of pairs
vector< pair <int,pair <int,int> > > vect;
map<int,int> visit;
int n,tq;
cout<<"Enter number of process\n";
cin>>n;
cout<<"Give time quantum\n";
cin>>tq;
cout<<"Give arrival and burst time\n";
for (int i=0; i<n; i++)
{
int a,b;
cin>>a>>b;
vect.push_back( make_pair(a, make_pair(i,b)));
visit[i]=tq;
}
for (int i=0; i<n; i++)
{
// "first" and "second" are used to access
// 1st and 2nd element of pair respectively
cout << vect[i].first << " "
<< vect[i].second.first <<" "<<vect[i].second.second << endl;
}
// using modified sort()
sort(vect.begin(), vect.end());
cout<<vect.size()<<endl;
int time=0;
bool flag = true;
while(flag)
{
int count=0;
for(int i=0; i<n; i++)
{
if(vect[i].second.second != 0)
{
int temp = visit[i];
visit[i] = visit[i] *2;
time = time + min(temp, vect[i].second.second);
cout<<"p"<<vect[i].second.first<<" ";
if(temp<= vect[i].second.second)
vect[i].second.second = vect[i].second.second-temp;
else
vect[i].second.second = 0;
count++;
}
}
if(count == 0)
flag=false;
}
return 0;
}