-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryAllocationMethods.c
156 lines (150 loc) · 3.84 KB
/
MemoryAllocationMethods.c
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
#include <stdio.h>
void printAllocation(int allocation[100], int process[100], int np)
{
printf("\nProcess No.\tProcess size\tAllocation block no\n");
for (int i = 0; i < np; i++)
{
printf("%d\t\t", i);
printf("%d\t\t", process[i]);
if (allocation[i] == -1)
{
printf("Not allowed");
}
else
{
printf("%d", allocation[i]);
}
printf("\n");
}
}
void firstFitAllocation(int process[100], int mblocks[100], int np, int nb)
{
int allocation[100];
int visited[nb];
for(int i=0;i<nb;i++){
visited[i]=0;
}
int p = 0, flag = 0;
for (int p = 0; p < np; p++)
{
flag = 0;
for (int b = 0; b < nb; b++)
{
if (process[p] <= mblocks[b] && visited[b]==0)
{
allocation[p] = b;
visited[b]=1;
mblocks[b]-= process[p];
flag = 1;
break;
}
}
if (flag == 0)
{
allocation[p] = -1;
}
}
// printitng
printAllocation(allocation, process, np);
}
void bestFitAllocation(int process[100], int mblocks[100], int np, int nb)
{
int allocation[100];
int visited[nb];
for(int i=0;i<nb;i++){
visited[i]=0;
}
int fitLoc, flag;
for (int p = 0; p < np; p++)
{
fitLoc = -1;
flag = 0;
for (int b = 0; b < nb; b++)
{
if (process[p] <= mblocks[b] && visited[b]==0)
{
if (fitLoc == -1)
fitLoc = b;
else
{
if (mblocks[b] < mblocks[fitLoc])
fitLoc = b;
}
}
}
allocation[p] = fitLoc;
if (fitLoc != -1)
{
mblocks[fitLoc] -= process[p];
visited[fitLoc]=1;
}
}
printAllocation(allocation, process, np);
}
void worstFitAllocation(int process[100], int mblocks[100], int np, int nb)
{
int allocation[100],visited[nb];
for(int i=0;i<nb;i++){
visited[i]=0;
}
int fitLoc, flag;
for (int p = 0; p < np; p++)
{
fitLoc = -1;
flag = 0;
for (int b = 0; b < nb; b++)
{
if (process[p] <= mblocks[b] && visited[b]==0)
{
if (fitLoc == -1)
fitLoc = b;
else
{
if (mblocks[b] > mblocks[fitLoc])
fitLoc = b;
}
}
}
allocation[p] = fitLoc;
if (fitLoc != -1)
{
mblocks[fitLoc] -= process[p];
visited[fitLoc]=1;
}
}
printAllocation(allocation, process, np);
}
int main()
{
int np, nb, process[100], mblocks[100];
printf("Enter the number of memory blocks:");
scanf("%d", &nb);
for (int i = 0; i < nb; i++)
{
printf("Enter the size of the memory block %d:", i);
scanf("%d", &mblocks[i]);
}
printf("Enter the number of process:");
scanf("%d", &np);
for (int i = 0; i < np; i++)
{
printf("Enter the size of the process %d:", i);
scanf("%d", &process[i]);
}
// copy of memory
int copy1P[100], copy1MB[100], copy2P[100], copy2MB[100];
for (int i = 0; i < np; i++)
{
copy1P[i] = process[i];
copy2P[i] = process[i];
}
for (int i = 0; i < nb; i++)
{
copy1MB[i] = mblocks[i];
copy2MB[i] = mblocks[i];
}
firstFitAllocation(process, mblocks, np, nb);
bestFitAllocation(copy1P, copy1MB, np, nb);
worstFitAllocation(copy2P, copy2MB, np, nb);
return 0;
}