5.1 Main Memory
5.1 Main Memory
5.1 Main Memory
Watch these videos to get a good idea of how CPU and Memory work together
to execute instructions:
Main memory and registers are only storage CPU can access directly
Memory unit only sees
a stream of addresses + read requests,
or address + data and write requests
Register access in one CPU clock (or less)
Main memory can take many cycles, causing a stall
Cache sits between main memory and CPU registers
Protection of memory required to ensure correct operation
Protection of Memory using Base and Limit
Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to
be sure it is between base and limit for that user
Hardware Address Protection
Multistep Processing of a User Program
Address Binding
Programs on disk, ready to be brought into memory to execute form an
input queue
Without support, must be loaded into address 0000
Inconvenient to have first user process physical address always at 0000
How can it not be?
Further, addresses represented in different ways at different stages of a
program’s life
Source code addresses usually symbolic
Compiled code addresses bind to relocatable addresses
i.e. “14 bytes from beginning of this module”
Linker or loader will bind relocatable addresses to absolute addresses
i.e. 74014
Each binding maps one address space to another
Binding of [Instructions and Data] to [Memory]
Execution time: Binding delayed until run time if the process can be
moved during its execution from one memory segment to another. Need
hardware support for address maps (e.g., base and limit registers).
Logical vs. Physical Address Space
Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped
Fixed/Static
Partition
Memory Allocation
Contiguous
Variable Size
Partition
Paging
Non-Contiguous
Segmentation
Contiguous Allocation
Advantages
1. Simple to implement.
2. Little OS overhead.
Disadvantages
1. Inefficient use of memory due to internal
fragmentation.
2. Maximum number of active processes is fixed.
Contiguous Storage Allocation
Variable Size or Dynamic Partitioning
With dynamic partitioning, the partitions are of
variable length and number.
When a process is brought into main memory, it is
allocated exactly as much memory as it requires and
no more.
Contiguous Storage Allocation
Variable-partition sizes for efficiency (sized to a given process’ needs)
Hole – block of available memory; holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large enough to
accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Contiguous Storage Allocation
This method starts out well, but eventually it leads to
a situation in which there are a lot of small holes in
memory.
As time goes on, memory becomes more and more
fragmented and memory utilization declines. This
phenomenon is referred to as external fragmentation.
It indicates the memory that is external to all
partitions becomes increasingly fragmented.
Placement algorithm
How to satisfy a request of size n from a list of free holes?
The most common strategies to allocate free partitions to the
new processes are as follows:
1. First fit: Allocate the first free partition, large enough to
accommodate the process. It executes faster.
2. Best fit: Allocate the smallest free partition that meets
the requirement of the process. It achieves higher
utilization of memory by searching smallest free partition.
3. Worst fit: Allocate the largest available partition to the
newly entered process in the system.
4. Next fit: Start from current location in the list.
Placement algorithm
Example: Consider the following memory configuration after a number of
placement and swapping out operations. The last block that was used was
a 22 MB block from which a 14 MB partition was created. The figure (b)
shows 16 MB allocation request.
Placement algorithm
Advantages of dynamic partitioning
1. Memory utilization is generally better as
partitions are created dynamically.
2. Less internal fragmentation as partitions are
changed dynamically.
3. The process of merging adjacent holes to form a
single larger hole is called coalescing.
Disadvantages
1. Lots of OS space, time, complex memory
management algorithms are required.
2. Compaction time is very high.
Memory Allocation/Mapping Techniques
Fixed/Static
Partition
Memory Allocation
Contiguous
Variable Size
Partition
Paging
Non-Contiguous
Segmentation
Operating System Concepts – 9th Edition 8.37 Silberschatz, Galvin and Gagne ©2013
Non-Contiguous Allocation: Segmentation
4
1
3 2
4
0000
0001
In the previous example, the page tables of each process will be:
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
Answer:
Page Table Size = Number of Pages * Size of each Page Table Entry
Assuming that, each page table entry contains the Frame Number and nothing else and we found
that the Frame Number is represented using 14-bits (as there are 214 frames).
So, Page Table Size = 220×14-bits ≈ 220×2B = 2MB (if the memory is byte-addressable)
Paging (Cont.)
Answer: