You are on page 1of 5

Ho Chi Minh University of Technology

Faculty of Computer Science and Engineering

Operating Systems
Laboratory Report 6

Ngày 15 tháng 5 năm 2021

Student’s name: Vu Hoang Hai


Student ID: 1952669
Class: CC01 - CO2017
Date Performed: May 8th, 2021
Instructor: Le Thanh Van

CONTIGUOUS MEMORY ALLOCATION

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 void * best_fit_allocator(unsigned int size) {


2 struct mem_region* current_region = free_regions;
3
4 // THE RECORDED BEST REGION
5 struct mem_region* bestFit = NULL;

1
After declaring bestFit, we will iterate and find in the memory pool until the end the
required location and stores in it.

1 // ITERATE THRU ALL ELEMENTS


2 do {
3 if ((current_region->size >= size)
4 && (bestFit == NULL || bestFit->size > current_region->size))
5 bestFit = current_region;
6 else
7 current_region = current_region->next;
8 } while (current_region != NULL);
9
10 current_region = bestFit;

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.

1 struct mem_region* current_region = free_regions;


2
3 // THE RECORDED LARGEST REGION
4 struct mem_region* largestFit = NULL;
5
6 // ITERATE THRU ALL ELEMENTS
7 do {
8 if ((current_region->size >= size) && (largestFit == NULL ||
largestFit->size < current_region->size))
9 largestFit = current_region;
10 else
11 current_region = current_region->next;

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:

We have the following allocation table:

Size (KB) First-fit Best-fit Worst-fit


115 300 125 750
500 600 600 600
358 750 750 NULL
200 350 200 350
375 NULL NULL NULL

As we can see from the table above:

ˆ First-fit inefficiently uses memory slots, leading to internal fragmentation where


processes that has low memory space get occupied on larger pool slots (e.g. 115 KB
process uses the 300 KB slot, 200 KB uses 350 KB, and most severe is 358 KB uses
750 KB).

ˆ 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.

ˆ Performance-wise, it is obvious Best-fit algorithm takes much more time (worst


performance is O(n)) to search for the required allocation than First-fit, since the

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

First-fit Best-fit Worst-fit


- Fastest memory Optimal memory
allocation, short allocation, minimum Fast and simple
Advantages
execution time fragmentation implementation
- Simple implementation. and memory waste
- Wasteful memory space - Linearity search
usage inefficient memory leads to slow Memory wastage,
allocation, fragmentation performance time-consuming,
Disadvantages prone. - Complicated method, severe fragmentation,
- More reasonable require sub-algorithms worst case searching
allocation method to optimize searching is more prone.
is advised speed

You might also like