Contiguous Memory Allocation
Notes
- This is 9.2
- The first logical division of memory is os/process
- Generally memory is divided between the os and processes
- It doesn't matter where each is put,
- But generally the OS is put in high memory.
- In contiguous memory allocation, each process has a single section of memory that is contiguous
- This is somewhat of a dream, but a good starting point.
- Remember, we are dealing with a limit register and a reallocation register
- A logical address is 0 to whatever
- If whatever is lower then the value in the limit register, the access can proceed
- If it is higher, a memory out of bounds error (possible sigsegv)
- And the real address is found by adding the logical address to the reallocation register to get the actual physical address
- See figure 9.6
- This provides memory protection.
- But should the OS keep everything memory resident?
- In the past, this was not viable.
- On a busy system, this is also not viable
- Consider a rarely used device driver
- Memory allocation
- Assume we can estimate the amount of memory a program needs (is this reasonable?)
- Then as a process is created, it is allocated this memory
- When it completes it frees the memory
- But since not all processes need the same memory, different sized chunks are allocated.
- Assume we have 10 memory units.
- P1-2U , P2-5 P3-2, P4-3, P5-3
- P1 - P3 run and use 9 units of memory
- If P2 exits and P4 is scheduled
- If P1 exits, P5 can still not be loaded , but there are more than enough memory.
- I see two problems here
- We have to track the free and used memory (probably always) and find space for the process to fit.
- We have unused memory sufficient for a process, but we can not let it run .
- Finding the place to put a block
- The book proposes three algorithms
- First Fit: Find the first available hole and place the block
- Potentially fastest (but O(n), n = number of free holes, worst case)
- In simulations this does about as well as Best Fit for reducing holes
- But is faster than Best Fit
- Best Fit: Find the closest size hole and place the block
- O(n), unless there is a hole exactly the size of the requested block.
- Tends to be the best at reducing fragmentation (see next)
- Worst Fit: Find the largest hole and place the block
- External Fragmentation: the result of contiguous memory allocation, free portions of memory are split into smaller and smaller holes.
- This is problematic because there may be sufficient free memory to hold a new process, but can not be used.
- Simulations suggest over time if N blocks are allocated, 1/2N blocks will be lost to fragmentation
- Or 1/3 of the memory.
- Depending on how we allocate memory, we can also produce internal fragmentation
- Say we require memory to be issued in 1K blocks.
- This is to reduce book keeping (reduce the number of very small blocks)
- If a process wants 2.3K, it will be given 3K
- Or an internal fragmentation of .7K
- External fragmentation can be addressed with compaction
- Move all process memory to one end, removing the holes
- But this is time consuming and expensive.
- Memory copy is O(n) on bytes