-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemory Allocation Policy.c
211 lines (190 loc) · 4.63 KB
/
Memory Allocation Policy.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <stdlib.h>
typedef struct free_block
{
int start_addr;
int size;
struct free_block *next;
} free_block;
typedef struct allocated_block
{
int start_addr;
int size;
struct allocated_block *next;
} allocated_block;
free_block *free_head = NULL;
allocated_block *allocated_head = NULL;
void insert_free_block(int start_addr, int size)
{
free_block *new_block = (free_block *)malloc(sizeof(free_block));
new_block->start_addr = start_addr;
new_block->size = size;
new_block->next = NULL;
if (free_head==NULL)
{
free_head=new_block;
}
else
{
free_block* curr=free_head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new_block;
}
}
void insert_allocated_block(int start_addr, int size)
{
allocated_block *new_block = (allocated_block *)malloc(sizeof(allocated_block));
new_block->start_addr = start_addr;
new_block->size = size;
new_block->next = allocated_head;
allocated_head = new_block;
}
void best_fit(int size)
{
free_block *curr = free_head;
free_block *best = NULL;
int best_size = -1;
while (curr)
{
if (curr->size >= size && (best_size == -1 || curr->size < best_size))
{
best = curr;
best_size = curr->size;
}
curr = curr->next;
}
if (best)
{
insert_allocated_block(best->start_addr, size);
if (best_size == size)
{
free_head = best->next;
free(best);
}
else
{
best->size -= size;
best->start_addr += size;
}
}
}
void first_fit(int size)
{
free_block *curr = free_head;
while (curr)
{
if (curr->size >= size)
{
insert_allocated_block(curr->start_addr, size);
if (curr->size == size)
{
free_head = curr->next;
free(curr);
}
else
{
curr->size -= size;
curr->start_addr += size;
}
return;
}
curr = curr->next;
}
}
void worst_fit(int size)
{
free_block *curr = free_head;
free_block *worst = NULL;
int worst_size = -1;
while (curr)
{
if (curr->size >= size && (worst_size == -1 || curr->size > worst_size))
{
worst = curr;
worst_size = curr->size;
}
curr = curr->next;
}
if (worst)
{
insert_allocated_block(worst->start_addr, size);
if (worst_size == size)
{
free_head = worst->next;
free(worst);
}
else
{
worst->size -= size;
worst->start_addr += size;
}
}
}
void print_free_list()
{
free_block *curr = free_head;
printf("Free List: \n");
while (curr)
{
printf("[%d-%d] \n", curr->start_addr, curr->start_addr + curr->size);
curr = curr->next;
}
}
void print_allocated_list()
{
allocated_block *curr = allocated_head;
printf("Allocated List: \n");
while (curr)
{
printf("[%d-%d] \n", curr->start_addr, curr->start_addr + curr->size);
curr = curr->next;
}
}
int main()
{
int ch,b,n,ba,s,i;
printf("enter no of blocks:\n");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("enter the base address and size:");
scanf("%d%d",&ba,&s);
insert_free_block(ba, s);
}
printf("Initial Free List: ");
print_free_list();
while(1)
{
printf("enter choice 1:best fit, 2: first fit or3: worst fit:");
scanf("%d",&ch);
switch (ch)
{
case 1:printf("enter the size of process to be allocated:");
scanf("%d",&b);
best_fit(b);
printf("After Best Fit Allocation: ");
print_free_list();
print_allocated_list();
break;
case 2:printf("enter the size of process to be allocated:");
scanf("%d",&b);
first_fit(b);
printf("After First Fit Allocation: ");
print_free_list();
print_allocated_list();
break;
case 3:printf("enter the size of process to be allocated:");
scanf("%d",&b);
worst_fit(b);
printf("After Worst Fit Allocation: ");
print_free_list();
print_allocated_list();
break;
case 4: return 0;
}
}
return 0;
}