-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryalloc.cpp
More file actions
145 lines (135 loc) · 2.78 KB
/
Copy pathmemoryalloc.cpp
File metadata and controls
145 lines (135 loc) · 2.78 KB
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
#include<iostream>
#include<conio.h>
using namespace std;
int h, p;
int holes[10], jobs[10];
void firstfit()
{
int ho[10], jo[10], cnt[10] = { 0 }; //cnt keeps track if job is assigned/not to any hole
for (int i = 0; i < h; i++)
{
ho[i] = holes[i];
}
for (int i = 0; i < p; i++)
{
jo[i] = jobs[i];
}
int j= 0; // to keep track of holes
for (int i = 0; i < p; i++) //keeps track of processes
{
while (cnt[i] != h) //until job is equal to some hole
{
if (jo[i] <= ho[j])
{
cout << "Job " << i << "assigned to " << j << " hole" << endl;;
ho[j] = ho[j] - jo[i];
j++;
j = j % h;
break;
}
else
{
j++;
j = j % h;
cnt[i]++;
if (cnt[i] == h) //if count == number of hole its not assigned
{
cout << "Process " << i << "has to wait" << endl;
}
}
}
}
}
void bestfit()
{
int jo[10], ho[10], bfit[10], bfitcap[10];
for (int i = 0; i < h; i++)
{
ho[i] = holes[i];
}
for (int i = 0; i < p; i++)
{
jo[i] = jobs[i];
bfit[i] = -1; //keeps track ofindex of hole assigned
bfitcap[i] = 1000; //helps us to know if process is assigned/not to any hole
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < h; j++)
{
if (jo[i] <= ho[j] && ho[j] < bfitcap[i])
{
bfit[i] = j;
bfitcap[i] = ho[i];
}
}
if (bfitcap[i] == 1000)
{
cout << "Job " << i << " has to wait " << endl;
}
else
{
cout << "Job " << i << " allocated to " << bfit[i] << endl;
ho[bfit[i]] -= jo[i];
}
}
}
void worstfit() // same like bestfit but just oppsoite
{
int jo[10], ho[10], wfit[10], wfitcap[10];
for (int i = 0; i < h; i++)
{
ho[i] = holes[i];
}
for (int i = 0; i < p; i++)
{
jo[i] = jobs[i];
wfit[i] = -1; //keeps track ofindex of hole assigned
wfitcap[i] = 0; //helps us to know if process is assigned/not to any hole
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < h; j++)
{
if (jo[i] <= ho[j] && ho[j] > wfitcap[i]) //reverse condition of best fit
{
wfit[i] = j;
wfitcap[i] = ho[j];
}
}
if (wfitcap[i] == 0)
{
cout << "Process " << i << " has to wait";
}
else
{
cout << "Process " << i << " is assigned to " << wfit[i] << endl;
ho[wfit[i]] -= jo[i];
}
}
}
int main()
{
cout << "Enter the number of blocks : ";
cin >> h;
cout << "Enter the number of jobs/process : ";
cin >> p;
cout << "Enter block sizes"<<endl;
for (int i = 0; i < h; i++)
{
cin >> holes[i];
}
cout << "Enter job sizes" << endl;
for (int i = 0; i < p; i++)
{
cin >> jobs[i];
}
cout << "First fit" << endl;
firstfit();
cout << "Best fit" << endl;
bestfit();
cout << "Worst fit" << endl;
worstfit();
_getch();
return 0;
}