Professional Documents
Culture Documents
Operating Systems
Laboratory Report 6
1 Best-fit Algorithm
Task:
Stimulate the contiguous allocation algorithm Best-fit by completing the
void * best_fit_allocator(unsigned int size) function.
Solution:
The Best-fit algorithm seeks for the smallest slot in the memory pool and must fit the
required size at the same time. This is done by checking the entire memory pool (in
this case, iterate through all elements in the list) and find the necessary slot to fit in,
causing little to no leftover regions.
The function void * best_fit_allocator takes in the size of the memory space. We
first declare a struct mem_region* bestFit to hold the location of the slot.
1
After declaring bestFit, we will iterate and find in the memory pool until the end the
required location and stores in it.
The last step is to check the availability of bestFit and transfer memory data.
1 if (bestFit != NULL) {
2 struct mem_region* tmp =
3 (struct mem_region*)malloc(sizeof(struct mem_region));
4 tmp->pointer = current_region->pointer;
5 tmp->size = size;
6 tmp->next = used_regions;
7 tmp->prev = NULL;
8 if (used_regions == NULL) {
9 used_regions = tmp;
10 }
11 else {
12 used_regions->prev = tmp;
13 used_regions = tmp;
14 }
15 if (current_region->size == size) {
16 if (current_region == free_regions) {
17 free_regions = free_regions->next;
18 if (free_regions != NULL) {
19 free_regions->prev = NULL;
20 }
21 }
22 else {
23 if (current_region->prev != NULL) {
24 current_region->prev->next = current_region->next;
25 }
26 if (current_region->next != NULL) {
27 current_region->next->prev = current_region->prev;
2
28 }
29 }
30 free(current_region);
31 }
32 else {
33 current_region->pointer += size;
34 current_region->size -= size;
35 }
36 return tmp->pointer;
37 }
38 else {
39 return NULL;
40 }
2 Worst-fit Algorithm
Task:
Stimulate the contiguous allocation algorithm Worst-fit by completing the
void * worst_fit_allocator(unsigned int size) function.
Solution:
The Worst-fit algorithm merely looks for the largest hole to fill. This is also done by
checking the entire memory pool and find the necessary slot to fit in. They generates
the largest number of unfilled slots.
Like above, we first declare a struct mem_region* largestFit to hold the location
of the slot. And afterwards, we will iterate through the pool and find the largest hole.
3
12 } while (current_region != NULL);
13
14 current_region = largestFit;
The last step is to check availability of largestFit and add in the block.
3 Allocation Algorithms
Task:
Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and
125 KB (in order), how would the first-fit, best-fit, and worst-fit algorithms place
processes of size 115 KB, 500 KB, 358 KB, 200 KB, and 375 KB (in order)? Rank
the algorithms in terms of how efficiently they use memory.
Solution:
Best-fit allocates memory in an resourceful manner, e.g. 115 KB process only uses
just enough 125 KB slot, 500 KB uses 600 KB, 200 KB uses 200 KB). Each process
above uses just enough memory slots for their tasks and significantly avoid heavy
fragmentation of unused memory space.
4
whole process has to look up the entire memory pool in a linear matter. Other sub-
algorithmic solutions can be devised to assist in searching (e.g. binary, dictionary,
jump, etc) and reduce the time.
Worst-fit has implicit inefficiency since it uses the largest memory slot available,
causing severe fragmentation and leaving many processes unable to allocate for
new memory space.
4 Algorithms Comparison