-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_fit_algo.cpp
56 lines (48 loc) · 1.38 KB
/
best_fit_algo.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
#include <iostream>
using namespace std;
int main()
{
int fragment[20], b[20], p[20], i, j, nb, np, temp, lowest = 9999;
static int barray[20], parray[20];
cout << "\n\t\t\tMemory Management Scheme - Best Fit";
cout << "\nEnter the number of blocks: ";
cin >> nb;
cout << "Enter the number of processes: ";
cin >> np;
cout << "\nEnter the size of the blocks:-\n";
for (i = 1; i <= nb; i++)
{
cout << "Block no." << i << ": ";
cin >> b[i];
}
cout << "\nEnter the size of the processes :-\n";
for (i = 1; i <= np; i++)
{
cout << "Process no. " << i << ": ";
cin >> p[i];
}
for (i = 1; i <= np; i++)
{
for (j = 1; j <= nb; j++)
{
if (barray[j] != 1)
{
temp = b[j] - p[i];
if (temp >= 0)
if (lowest > temp)
{
parray[i] = j;
lowest = temp;
}
}
}
fragment[i] = lowest;
barray[parray[i]] = 1;
lowest = 10000;
}
cout << "\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment";
for (i = 1; i <= np && parray[i] != 0; i++)
cout << "\n"
<< i << "\t\t" << p[i] << "\t\t" << parray[i] << "\t\t" << b[parray[i]] << "\t\t" << fragment[i];
return 0;
}