I have a program that simulates best-fit memory management.
Basically, while there are available holes in the holes list, large enough for a process we are trying to allocate, processes are allocated and added to the process list. However, eventually, we get to a point where holes become very fragmented and we need to perform compaction.
The easiest way to do this, is obviously to create a new list and add all the processes in sequential order. However, that is not very realistic, since in real world, you wouldn’t have space to move things to and create a new list.
Can you think of a way to push all the processes to one end of memory and free space to the other? Basically it is set up like this array of holes (holes are structs that contain starting index and size) and an array of processes (processes are also structs that contain process id, starting index and size).
You could shuffle the the allocated memory to the end of available memory one after another.
Pseudocode:
this should lead to one free memory block at the beginning of available memory. You could also stop this function after one region has moved (after a time threshold has reached) and continue later (By testing whter there is a gap beetween subsequent process alocated regions, to skip unneccesary moves).