-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstFit.cpp
67 lines (55 loc) · 1.7 KB
/
FirstFit.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;
//GLOBAL VARIABLE:
struct Process {
int Block_Size = 0, Process_Size = 0, Num_Block = -1, Real_Block_Size = 0, Ind = 0;
};
int Num_Process, Num_Block;
Process* Arr;
//INPUT:
void Get_Block_Size() {
cout << "Enter Size Of Blocks :\n";
for (int i = 0; i < Num_Block; i++) {
cout << "Block [" << i + 1 << "] = ";
cin >> Arr[i].Block_Size, Arr[i].Real_Block_Size = Arr[i].Block_Size;
}
cout << endl;
}
void Get_Process_Size() {
cout << "Enter Size of Processes :\n";
for (int i = 0; i < Num_Process; i++) {
cout << "Process [" << i + 1 << "] = ";
cin >> Arr[i].Process_Size;
}
}
//PROCESS:
void Calc_Process() {
for (int i = 0; i < Num_Process; i++)
for (int j = 0; j < Num_Block; j++)
if (Arr[i].Process_Size <= Arr[j].Block_Size) {
Arr[i].Num_Block = j + 1, Arr[j].Block_Size -= Arr[i].Process_Size, Arr[i].Ind = j;
break;
}
}
//OUTPUT:
void Print_Process() {
cout << "\nProcess No.\tProcess Size\tBlock No\tBlock_Size\tFragment.\n";
for (int i = 0; i < Num_Process; i++) {
cout << " " << i + 1 << "\t\t" << Arr[i].Process_Size << "\t\t";
if (Arr[i].Num_Block != -1)
cout << Arr[i].Num_Block << "\t\t" << Arr[Arr[i].Ind].Real_Block_Size << "\t\t" << Arr[Arr[i].Ind].Block_Size << '\n';
else
cout << "Not Allocated \n";
}
}
int main() {
cout << "Enter Number Of Blocks: ";
cin >> Num_Block;
cout << "Enter Number Of Processes: ";
cin >> Num_Process;
Arr = new Process[Num_Block];
Get_Block_Size();
Get_Process_Size();
Calc_Process();
Print_Process();
}