Memory Management(2)
Relocation and Protection
- Principles
- Relocation: when a program is run, it does not know in advance which partition/addresses it will occupy
- Cannot simply generate static addresses that are absolute
- Addresses should be relative to where the program has been loaded
- Relocation must be solved in an operating system that allows processes to run at changing memory locations
- Protection: once you can have two programs in memory at the same time, protection must be enforced
- Relocation: when a program is run, it does not know in advance which partition/addresses it will occupy
- Address Types
- A logical address is a memory address seen by the process
- It is independent of the current physical memory assignment
- A physical address refers to an actual location in main memory
- The logical address space must be mapped onto the machine’s physical address space
- A logical address is a memory address seen by the process
- Approaches
- Static “relocation” at compile time: a process has to be located at the same location every single time(impractical)
- Dynamic relocation at load time
- An offset is added to every logical address to account for its physical location in memory
- Slows down the loading of a process, does not account for swapping
- Dynamic relocation at runtime
- Base and limit registers (requires hardware support)(L14 - p10)
- Two special purpose registers in CPU(the MMU)
- base register stores the start address of the partition
- the limit register holds the size of the partition
- At runtime
- base register is added to logical address to generate the physical address
- the resulting address is compared against the limit register
- Two special purpose registers in CPU(the MMU)
- Dynamic partitioning
- A variable number of partitions of which the size and starting address can change over time
- A process is allocated the exact amount of contagious memory it requires, thereby preventing internal fragmentation
- Base and limit registers (requires hardware support)(L14 - p10)
-
-
-
- Swapping
- holds some of the processes on the drive and shuttles processes between the drive and main memory as necessary
- Reasons for swapping:
- some processes only run occasionally
- we have more processes than partition
- a process’s memory requirements have changed
- total amount of memory that is required for the processes exceeds the available memory
- Difficulties
- The exact memory requirements may not be known in advance(heap and stack grow dynamically)
- External fragmentations
- Swapping a process out of memory will create “a hole”
- A new process may not use the entire “hole”, leaving a small unused block
- A new process may be too large for a given a “hole”
- Swapping
-
-
-
-
-
-
-
-
- The overhead of memory compaction to recover holes can be prohibitive and requires dynamic relocation
-
- Question: How to keep track of available memory
- Linked lists
- consists of a number of entries, each one of them contains data items(start of memory block, size, free/allocated flag)
- Each link contains a pointer to the next in the chain
- Linked lists
-
-
-
-
-
-
-
-
-
- Bitmaps
- Memory is split into blocks of say 4K size
- A bitmap is set up so that each bit
- is 0 -> free
- is 1 -> the block is used
- A bitmap is set up so that each bit
- Trade-off exists between size of the bitmap and the size of blocks exists
- The size of bitmaps can become prohibitive for small blocks and may make searching the bitmap slower
- Larger blocks may increase internal fragmentation
- Memory is split into blocks of say 4K size
- Bitmaps
-
-
-
-