Real Memory Management

Download Report

Transcript Real Memory Management

Operating Systems
Real Memory
Management
A. Frank - P. Weisberg
Real Memory Management
•
•
•
•
•
•
•
2
Background
Memory Management Requirements
Fixed/Static Partitioning
Variable/Dynamic Partitioning
Simple/Basic Paging
Simple/Basic Segmentation
Segmentation with Paging
A. Frank - P. Weisberg
Background (1)
3
• Program must be brought (from disk) into memory
and placed within a process for it to be run.
• 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
A. Frank - P. Weisberg
operation.
Background (2)
• Memory management is the task carried out by
the OS and hardware to accommodate multiple
processes in main memory.
• User programs go through several steps before
being able to run.
• This multi-step processing of the program
invokes the appropriate utility and generates
the required module at each step (see next
slides).
4
A. Frank - P. Weisberg
Multi-step processing of user program (1)
5
A. Frank - P. Weisberg
Multi-step processing of user program (2)
6
A. Frank - P. Weisberg
A C Compilation Example
7
A. Frank - P. Weisberg
Object Module
• Public names are usable by other object
modules.
• External names are defined in other
object modules:
– Includes the list of instructions having
these names as operands.
• Relocation dictionary:
– Has the list of instructions who’s operands
are addresses (since they are relocatable).
• Only code and data will be loaded in
physical memory:
– The rest is used by the linker and then
removed.
8
• The stack is allocated only at load time.
A. Frank - P. Weisberg
End of module
Relocation dictionary
Data
Machine code
External names table
Public names table
Module identification
Object Modules
• Initially, each object module
has its own address space.
• All addresses are relative to
the beginning of the module.
9
A. Frank - P. Weisberg
Addressing Requirements for Process
10
A. Frank - P. Weisberg
Static Linking
• The linker uses tables in
object modules to link
modules into a single
linear addressable space.
• The new addresses are
addresses relative to the
beginning of the load
module.
11
A. Frank - P. Weisberg
Dynamic Linking
• The linking of some external modules is done after the
creation of the load module (executable file).
• Load-time dynamic linking:
– The load module contains references to external modules
which are resolved at load time.
• Run-time dynamic linking:
– references to external modules are resolved when a call is
made to a procedure defined in the external module.
– unused procedure is never loaded.
– Process starts faster.
12
A. Frank - P. Weisberg
Static vs. Load-time Dynamic Linking
13
A. Frank - P. Weisberg
Program vs. Memory sizes
•
What to do when program size is larger than
the amount of memory/partition (that exists or
can be) allocated to it?
•
There are two basic solutions within real
memory management:
1. Overlays
2. Dynamic Linking (Libraries – DLLs)
14
A. Frank - P. Weisberg
1. Overlays
• Keep in memory only the overlay (those instructions
and data that are) needed at any given phase/time.
• Overlays can be used only for programs that fit this
model, i.e., multi-pass programs like compilers.
• Overlays are designed/implemented by programmer.
Needs an overlay driver.
• No special support needed from operating system, but
program design of overlays structure is complex.
15
A. Frank - P. Weisberg
Overlays for a Two-Pass Assembler
16
A. Frank - P. Weisberg
2. Dynamic Linking
17
• Dynamic linking is useful when large amounts
of code are needed to handle infrequently
occurring cases.
• Routine is not loaded unless/until it is called.
• Better memory-space utilization; unused
routine is never loaded.
• Useful when large amounts of code are needed
to handle infrequently occurring cases.
• Not much support from OS is required –
implemented through program design.
A. Frank - P. Weisberg
Dynamics of Dynamic Linking
18
• Linking postponed until execution time.
• Small piece of code, stub, used to locate the
appropriate memory-resident library routine.
• Stub replaces itself with the address of the
routine, and executes the routine.
• OS needed to check if routine is in processes’
memory address.
• Dynamic linking is particularly useful for
shared/common libraries – here full OS support
is needed.
A. Frank - P. Weisberg
Advantages of Dynamic Linking
• Executable files can use another version of the external
module without the need of being modified.
• Each process is linked to the same external module.
– Saves disk space.
• The same external module needs to be loaded in main
memory only once.
– Processes can share code and save memory.
• Examples:
– Windows: external modules are .DLL files.
– Unix: external modules are .SO files (shared
library).
19
A. Frank - P. Weisberg
Dynamic Linking/Loading Scenario
20
A. Frank - P. Weisberg
Memory Management Requirements
•
•
21
If only a few processes can be kept in main memory,
then much of the time all processes will be waiting
for I/O and the CPU will be idle.
Hence, memory needs to be allocated efficiently in
order to pack as many processes into memory as
possible. Need additional support for:
1.
2.
3.
4.
5.
Relocation
Protection
Sharing
Logical Organization
Physical Organization
A. Frank - P. Weisberg
Memory Management Requirements (1)
• Relocation:
– Programmer cannot know where the program
will be placed in memory when it is executed.
– A process may be (often) relocated in main
memory due to swapping/compaction:
• Swapping enables the OS to have a larger pool
of ready-to-execute processes.
• Compaction enables the OS to have a larger
contiguous memory to place programs in.
22
A. Frank - P. Weisberg
Memory Management Requirements (2)
• Protection:
– Processes should not be able to reference
memory locations in another process without
permission.
– Impossible to check addresses in programs at
compile/load-time since the program could
be relocated.
– Address references must be checked at
execution-time by hardware.
23
A. Frank - P. Weisberg
Memory Management Requirements (3)
• Sharing:
– must allow several processes to access a common
portion of main memory without compromising
protection:
 Better to allow each process to access the same
copy of the program rather than have their own
separate copy.
 Cooperating processes may need to share access
to the same data structure.
24
A. Frank - P. Weisberg
Memory Management Requirements (4)
• Logical Organization:
– Users write programs in modules with different
characteristics:
• instruction modules are execute-only.
• data modules are either read-only or read/write.
• some modules are private and others are public.
– To effectively deal with user programs, the OS and
hardware should support a basic form of a module
to provide the required protection and sharing.
25
A. Frank - P. Weisberg
Memory Management Requirements (5)
• Physical Organization:
– External memory is the long term store for
programs and data while main memory holds
programs and data currently in use.
– Moving information between these two levels of the
memory hierarchy is a major concern of memory
management –
• it is highly inefficient to leave this responsibility to the
application programmer.
26
A. Frank - P. Weisberg
The need for Relocation
• Because of need for process swapping and
memory compaction, a process may occupy
different main memory locations during its
lifetime.
• Consequently, physical memory references
(addresses) by a process cannot always be
fixed.
• This problem is solved by distinguishing
between logical address and physical address.
27
A. Frank - P. Weisberg
Address Types
• A physical (absolute) address is a physical location in
main memory.
• A logical (virtual) address is a reference to a memory
location that is independent of the physical
organization of memory.
• Compilers produce code in which all memory
references are logical addresses.
• A relative address is an example of logical address in
which the address is expressed as a location relative to
some known point in the program (ex: the beginning).
28
A. Frank - P. Weisberg
Relocation Scheme
• Relative address is the most frequent type of
logical address used in program modules (i.e.,
executable files).
• Relocatable modules are loaded in main
memory with all memory references left in
relative form.
• Physical addresses are calculated “on the fly”
as the instructions are executed.
• For adequate performance, the translation from
relative to physical address must by done by
hardware.
29
A. Frank - P. Weisberg
Memory-Management Unit (MMU)
• Hardware device that maps logical/virtual
address to real/physical address.
• In MMU scheme, the value in the base
(relocation) register is added to every logical
(virtual) address generated by a user process
at the time it is sent to memory.
• The user program deals with logical/virtual
addresses; it never sees the real/physical
addresses.
30
A. Frank - P. Weisberg
CPU, MMU and Memory
31
A. Frank - P. Weisberg
Dynamic relocation using a relocation register
32
A. Frank - P. Weisberg
Hardware Support for Relocation and Limit Registers
33
A. Frank - P. Weisberg
When binding of Instructions/Data to Memory?
•
34
Address-binding of instructions and data to
memory addresses can happen at three
different stages:
1. Compile-time: If memory location is known a priori,
absolute code can be generated; must recompile code if
the starting location changes.
2. Load-time: Must generate relative code if memory
location is not known at compile-time; loading maps
relative code to absolute code by adding start location.
3. Execution-time: Binding delayed until run-time if the
process can be relocated (i.e., relocatable code) during its
execution from one place to another. Need hardware
support for address maps (e.g., base and limit registers).
A. Frank - P. Weisberg
Logical vs. Physical Address Space
• The concept of a logical address space of a program
that is bound to a separate physical address space is
central to proper memory management.
– Logical address – generated by the CPU; also referred to
later as Virtual address.
– Physical address – address seen by the memory unit.
35
• Logical and physical addresses are the same at the end
in compile-time and load-time address-binding
schemes.
• Logical (virtual) and physical addresses differ in
execution-time address-binding scheme.
A. Frank - P. Weisberg
Logical and Physical Address Spaces
36
A. Frank - P. Weisberg
Example Hardware for Address Translation
37
Dynamics of hardware translation of addresses
• When a process is assigned to the running state, a
relocation/base register gets loaded with the starting
physical address of the process.
• A limit/bounds register gets loaded with the process’s
ending physical address.
• When a relative addresses is encountered, it is added
with the content of the base register to obtain the
physical address which is compared with the content
of the limit/bounds register.
• This provides hardware protection: each process can
only access memory within its process image.
38
A. Frank - P. Weisberg