Contigous Memory
Main Memory (RAM)
The Central Processing Unit (CPU) can directly access both registers and main memory.
Accessing registers takes one CPU clock cycle (or less).
Accessing main memory, however, can take multiple cycles.
A cache system exists between the main memory and the CPU registers to improve access times.
L1 Data Cache
L1 Instruction Cache
L2 Cache
LL Cache
Access times:
Registers: less than 1 nanosecond (ns)
Main Memory: between 10 to 30 ns
Virtualizing Main Memory
For any program to run, it must first be brought from disk into memory and placed within a process.
Originally, every process had full access to memory.
Each process utilizes virtual addresses in assembly language.
Memory sharing among multiple processes raises two significant questions:
How do we determine a physical address in memory for a given virtual address?
How do we safeguard a process's memory from other processes?
Logical vs. Physical Address Space
The relationship between logical and physical address spaces is crucial for effective memory management.
Definitions:
Logical Address: Address generated by the CPU; also known as a virtual address.
Physical Address: Address that is visible to the memory unit.
Address Binding Decisions:
In compile-time and load-time address-binding schemes, logical and physical addresses are identical.
In execution-time address-binding schemes, logical (virtual) addresses differ from physical addresses.
Binding of Instructions and Data to Memory
The binding process of instructions and data to memory addresses can occur at three distinct stages:
Compile Time: If a memory location is known beforehand, absolute code can be generated. Recompilation is necessary if the starting location changes.
Load Time: Generation of relocatable code is required if the memory location isn't determined at compile time.
Execution Time: Binding is postponed until runtime, allowing for process relocation between memory segments, necessitating hardware support (e.g., base and limit registers).
Dynamic Loading
In dynamic loading, a routine is not loaded until it is explicitly called during execution.
This approach enhances memory utilization since unused routines are never loaded into memory.
It is particularly useful when large amounts of code are required for seldom-occurring cases.
Dynamic loading does not require special operating system support; it can be realized through program design.
Dynamic Linking
Dynamic linking is deferred until execution time.
A small piece of code, called a stub, is used to locate the appropriate memory-resident library routine.
The stub replaces itself with the address of the routine and executes it.
The operating system is responsible for verifying if the routine resides in the process’s memory address.
Dynamic linking is especially useful for libraries, often referred to as shared libraries.
Contiguous Allocation
Main memory is typically divided into two primary partitions:
Resident Operating System: Typically located in low memory alongside the interrupt vector.
User Processes: Stored in high memory areas.
Relocation registers are employed to protect user processes from one another and to prevent unauthorized modifications to operating-system code and data.
Base Register: Holds the smallest physical address.
Limit Register: Contains the range of logical addresses; every logical address must be less than the limit register value.
The Memory Management Unit (MMU) dynamically maps virtual addresses to physical addresses.
Finding a Process's Physical Limits
The base and limit registers define the logical address space for a process.
Example:
Operating System runs from address 0 to a certain point in memory.
Each process has its own defined limits based on its base and limit registers.
Memory layout may look like this:
OS: 0 - 256,000
Process 1: 300,040 - 420,940
Process 2, Process 3, etc.
Dynamic Relocation Using a Base Register
The value stored in the relocation register is added to every address generated by a user process at the moment it is sent to memory.
Hardware Support for Protection Using Limit Registers
Each logical address generated must be less than the value stored in the limit register. Failure to meet this condition results in a trap (an exception).
Finding Base and Limit Registers
In a given memory configuration:
The operating system distinguishes between different processes based on their assigned base and limit registers.
Free spaces in memory also have base and limit registers that define their range.
Fragmentation
External Fragmentation: Occurs when total memory space is available to satisfy a request but is not contiguous.
Internal Fragmentation: Happens when allocated memory may be slightly larger than requested. This discrepancy leads to wasted space within a partition.
Methods to reduce external fragmentation include:
Compaction: Rearranging memory contents to place all free memory together into one large block.
Compaction requires dynamic relocation and must occur at execution time.
Swapping
Swapping refers to the process of temporarily moving a process out of memory to a backing store and later bringing it back into memory for continued execution.
Backing Store: A fast disk large enough to keep copies of all memory images for all users; requires direct access to these images.
The process of roll out, roll in involves swapping out a lower-priority process to load and run a higher-priority process.
The primary component of swap time is transfer time, which relates directly to the amount of memory being swapped.
Variants of swapping can be found in various systems such as UNIX, Linux, and Windows.
The system maintains a ready queue of processes that are eligible to run, which holds memory images on disk.
Schematic View of Swapping
Diagram summarizing the swapping process:
Operating system manages the user space.
Main memory contains active processes (e.g., process P₁ and process P₂).
The backing store houses swapped-out processes.