-
Notifications
You must be signed in to change notification settings - Fork 0
/
MVT.cpp
67 lines (64 loc) · 1.65 KB
/
MVT.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
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int>> &mm, int memsize, int total)
{
cout << "\n\tMAIN MEMEORY\n";
cout << "\t-----------------\n";
for (int i = 0; i < (int)mm.size(); i++)
{
if (mm[i][1] == -1)
cout << "Block" << i << "\t|\t\t| " << mm[i][0] << " MB\n";
else
cout << "Block" << i << "\t|\t"
<< "P" << mm[i][1] << "\t| " << mm[i][0] << " MB\n";
cout << "\t-----------------\n";
}
cout << "Block n"
<< "\t|\t\t| " << memsize - total << " MB\n";
cout << "\t-----------------\n";
}
void MVT(int memsize)
{
int t,extr_frag = 0,total = 0,p,id;
vector<vector<int>> mm;
cout<<"Enter number of process: ";
cin>>t;
while(t--){
cout<<"Enter process id and size: ";
cin>>id>>p;
total+=p;
if(total>memsize){
cout<<"Memory is full";
break;
}
else{
mm.push_back({p,id});
print(mm,memsize,total);
}
}
cout<<"\nEnter number or process to remove: ";
cin>>t;
while(t--){
cout<<"Enter process id: ";
cin>>id;
int i;
for(i=0;i< (int)mm.size();i++){
if(id == mm[i][1]){
mm[i][1] = -1;
extr_frag+=mm[i][0];
print(mm,memsize,total);
break;
}
}
if(i==(int)mm.size())
cout<<"Process not found";
}
cout<<"\nExternal fragmentation: "<<extr_frag;
}
int main()
{
int memsize;
cout << "Enter the total size of main memory(MB):";
cin >> memsize;
MVT(memsize);
}