Physical memory storage (RAM) is a contiguous byte-addressed array, each address being a physical address.
Name | Speed | Typical Amount | Cost |
---|---|---|---|
CPU registers | 1ns | 512B | Expensive |
Cache | 10ns | 12MB | $150/MB |
RAM | 100ns | 8GB | $0.58/MB |
HDD | 10ms | 2TB | $0.0025/MB |
Tape drives etc. | Seconds | PBs | Dirt cheap |
exec
syscall to Load and Run the executable which has the instructions of where to load and run includedfork
a process to
execve
fills 3 regions into the SWAP while loading:
Both can grow/shrink.
Option 1: Use physical memory directly.
Pros | Cons |
---|---|
No further conversion (fast) | Hard to protect memory space |
Memory address collisions: Erroneously share memory |
Option 2: Translate all memory addresses at load time
Pros | Cons |
---|---|
Is correct | Slow |
Protects memory | Not easy to distinguish references from integers |
Option 3: Base + limit
Pros | Cons |
---|---|
Is correct | Slow memory access (Each memory access requires 1 add and 1 comp.) |
Protects memory | |
Fast compile (addresses translated by Base at runtime) Addr = $Base + LogicalAddr |
|
Limit protects the memory space of processes Addr < $Limit |
Option 4: Segmentation mechanisms
Note what options 2, 3, 4 do is provide a mapping from logical addresses to physical addresses, which is performed by the OS.
Requirements:
Assumptions:
Problems:
When memory is full, remove terminated processes OR swap blocked processes to secondary storage.
Fixed-size partitions: physical memory split into a fixed number of partitions. Each process occupies a partition
Pros | Cons |
---|---|
Easy to manage | Partition size needs to be $\max({p_1 \dots p_n})$ i.e. small programs waste memory space (internal fragmentation) |
No need further computations to allocate | |
No external fragmentation (every page equally allocable) |
Variable-size partitions: size allocated based on actual size of process. OS tracks occupied/free regions, splits and merges as necessary.
Pros | Cons |
---|---|
Better memory utilization | Many holes from termination/swapping/creation (external fragmentation) |
No internal fragmentation (partition sizes exact) | Needs to maintain more information in OS |
Split the hole into N
which becomes the partition and M-N
which becomes the new hole (allocation).
Upon freeing of partitions (deallocation):
Merging | Compaction |
---|---|
![]() |
![]() |
Note allocation is $O(n)$ but deallocation is $O(1)$ assuming that the address of the memory block the process occupies is saved somewhere.
OS represents each equal memory partition with a bit.
Incurs overhead for bit manipulation operations because the memory is not bit-addressable.
Very important: Everytime a bitmap’s space is involved, remember to divide number of bits by 8 to get number of bytes!
OS represents the partitions as a linked list. In dynamic partitioning above, to allocate a new process:
To de-allocate a process, check the partitions before and after if they can be merged.
Free blocks recursively split in half to meet request. Each pair of neighbouring blocks become buddy blocks.
Once both are freed, can be merged to become larger block.
Allocation of process of memory block size $n$
At most $\log M$ steps for fixed total physical memory size $M$: $O(1)$
Deallocation: Check if any free block in $A[\lceil \log n \rceil]$ is the buddy (worst case $O(n)$ time for $\lceil \log n \rceil = 1$)
xx...xx000..0
and xx...xx100..0
with $k$ trailing zeroesInternal fragmentation: all memory requests are rounded up to the nearest power of two.
External fragmentation: due to limited coalescing ability of this allocator (unable to coalesce blocks of different sizes, if they are not buddy blocks)