-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworst_fit_MVT.cpp
162 lines (137 loc) · 5.6 KB
/
worst_fit_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
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
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
bool sortbyfirst(const pair<int,int> &a,const pair<int,int> &b){
return (a.first > b.first);
}
int main(){
int MAX_SIZE;
vector<int>int_frag;
int int_frag_count=0,sum=0;
vector<int>v;
vector<int>fchunk;
vector<pair<int,int> > tars;
cout<<"\nEnter the total mem_size for allocation: ";
cin>>MAX_SIZE;
int* arr=new int[MAX_SIZE];
for(int i=0;i<MAX_SIZE;i++)
arr[i]=0;
int size,z,ext_frag=0,v_loc;
int p_num=1,fchunk_count=0;;
char ch='y',ch2='n';
while(ch!='n'){
int count=0,temp=0;
bool status=false;
bool mem_status=false;
cout<<"\nenter the process "<<p_num<<" size: ";
v.push_back(p_num);
cin>>size;
for(int i=0 ; i<MAX_SIZE ; i++){
if(arr[i]==0){
temp=i;
count++;
if(count>2 && i<MAX_SIZE-1 && arr[i+1]!=0){
tars.push_back(make_pair(count,temp));
}
else if(count>2 && i==MAX_SIZE-1){
tars.push_back(make_pair(count,temp));
}
}
else{
count=0;
}
}
stable_sort(tars.begin(),tars.end(),sortbyfirst);
for(vector<pair<int,int> >::iterator it=tars.begin() ; it!=tars.end() && mem_status!=true ; it++){
if(it->first >= size){
for(int i=0;i<size;i++){
arr[it->second + 1 - it->first + i]=p_num;
}
mem_status=true;
}
}
tars.erase(tars.begin(),tars.end());
if(mem_status!=true){
cout<<"\nNo memory availble...Need to terminate some process...";
cout<<"\nDo you want to terminate any process('y'/'n'): ";
cin>>ch2;
bool v_status=false;
if(ch2=='y'){
cout<<"\nwhich process do you want to terminate:(int) ";
cin>>z;
for(int x=0 ; x<v.size() && v_status==false ; x++){
if(v[x]==z){
v_status=true;
v_loc=x;
}
}
if(v_status==true){
cout<<"\nTerminate process"<<v[v_loc];
for(int i=0 ; i<MAX_SIZE && status!=true ; i++){
if(arr[i]==v[v_loc])
status=true;
else
status=false;
}
if(status==true){
for(int i=0;i<MAX_SIZE;i++){
if(arr[i]==v[v_loc])
arr[i]=0;
}
}
}
else
cout<<"\nprocess"<<z<<" doesn't exist in memory";
}
}
else{
p_num++;
}
cout<<"\n";
for(int i=0;i<MAX_SIZE;i++)
cout<<arr[i]<<" ";
cout<<"\nDo you want to continue('y'/'n'): ";
cin>>ch;
}
for(int i=0;i<MAX_SIZE;i++){
if(arr[i]==0){
int_frag_count++;
if(int_frag_count==2 || int_frag_count==1){
if(i<(MAX_SIZE-1) && arr[i+1]!=0)
int_frag.push_back(int_frag_count);
else if(i==(MAX_SIZE-1) && arr[i]==0)
int_frag.push_back(int_frag_count);
}
}
else
int_frag_count=0;
}
cout<<"\nFree chunk list: ";
for(int i=0;i<MAX_SIZE;i++){
if(arr[i]==0){
fchunk_count++;
if(i<(MAX_SIZE-1) && arr[i+1]!=0)
fchunk.push_back(fchunk_count);
else if(i==MAX_SIZE-1)
fchunk.push_back(fchunk_count);
}
else
fchunk_count=0;
}
for(int i=0;i<fchunk.size();i++)
cout<<fchunk[i]<<" ";
cout<<"\n";
cout<<"\nTotal internal fragmentation is: ";
for(int i=0;i<int_frag.size();i++)
sum=sum+int_frag[i];
cout<<sum;
cout<<"\nexternal fragmentation is: ";
for(int i=0;i<MAX_SIZE;i++){
if(arr[i]==0)
ext_frag=ext_frag+1;
}
cout<<(ext_frag-sum)<<"\n\n";
return 0;
}