Main Memory OS
Main Memory OS
Main Memory OS
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 8: Memory Management
Background
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Structure of the Page Table
Operating System Concepts – 9th Edition 8.2 Silberschatz, Galvin and Gagne ©2013
Objectives
To provide a detailed description of various ways of organizing memory hardware
To provide a detailed description of the Intel Pentium, which supports both pure segmentation
and segmentation with paging
Operating System Concepts – 9th Edition 8.3 Silberschatz, Galvin and Gagne ©2013
Background
The memory hierarchy includes:
Very small, extremely fast, extremely expensive, and volatile CPU registers
Hundreds and thousands of terabytes of very slow, almost free, and non-volatile
Internet Storage (Web pages, Ftp repositories, etc.)
Operating System Concepts – 9th Edition 8.4 Silberschatz, Galvin and Gagne ©2013
Background
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
Operating System Concepts – 9th Edition 8.5 Silberschatz, Galvin and Gagne ©2013
Memory Management
The purpose of memory management is to ensure fair, secure,
orderly, and efficient use of memory.
Operating System Concepts – 9th Edition 8.6 Silberschatz, Galvin and Gagne ©2013
Source to Execution
Translation of a source program in a high-level or assembly language involves
compilation and linking of the program.
This process generates the machine language executable code (also known as a
binary image) for the give source program. To execute the binary code, it is loaded into
the main memory and the CPU state is set appropriately. The whole process is shown
in the following diagram.
Operating System Concepts – 9th Edition 8.7 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 8.8 Silberschatz, Galvin and Gagne ©2013
Hardware Address Protection with Base and Limit Registers
Operating System Concepts – 9th Edition 8.9 Silberschatz, Galvin and Gagne ©2013
Binding of Instructions and Data to Memory
Address binding of instructions and data to memory addresses can
happen at three different stages
1. Compile time: if you know at compile where the process will reside in memory,
the absolute addresses can be assigned to instructions and data by the compiler.
2. Load time: if it is not known at compile time where the process will reside in
memory, then the compiler must generate re-locatable code. In this case the final
binding is delayed until load time.
3. Execution time: if the process can be moved during its execution from one
memory segment to another, then binding must be delayed until run time.
Special hardware must be available for this to work.
Operating System Concepts – 9th Edition 8.10 Silberschatz, Galvin and Gagne ©2013
Multistep Processing of a User Program
Operating System Concepts – 9th Edition 8.11 Silberschatz, Galvin and Gagne ©2013
Logical vs. Physical Address Space
The concept of a logical address space that is bound to a
separate physical address space is central to proper memory
management
Operating System Concepts – 9th Edition 8.12 Silberschatz, Galvin and Gagne ©2013
Logical vs. Physical Address Space
Operating System Concepts – 9th Edition 8.13 Silberschatz, Galvin and Gagne ©2013
Memory-Management Unit (MMU)
The run-time mapping from virtual to physical addresses is done by a
piece of hardware in the CPU, called the memory management unit
(MMU).
Operating System Concepts – 9th Edition 8.14 Silberschatz, Galvin and Gagne ©2013
Dynamic relocation using a relocation register
Operating System Concepts – 9th Edition 8.15 Silberschatz, Galvin and Gagne ©2013
Overlays
To enable a process to be larger than the amount of
memory allocated to it, we can use overlays. The idea of
overlays is to keep in memory only those instructions and
data that are needed at any given time. When other
instructions are needed, they are loaded into space
occupied previously by instructions that are no longer
needed.
Operating System Concepts – 9th Edition 8.18 Silberschatz, Galvin and Gagne ©2013
Overlays: Example
Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne ©2013
Overlays
Operating System Concepts – 9th Edition 8.20 Silberschatz, Galvin and Gagne ©2013
Swapping
A process needs to be in the memory to be executed. A
process, however, can be swapped temporarily out of
memory to a backing store, and then brought back into
memory for continued execution.
Operating System Concepts – 9th Edition 8.21 Silberschatz, Galvin and Gagne ©2013
Swapping
Backing store is a fast disk large enough to accommodate
copies of all memory images for all users; it must provide
direct access to these memory images.
Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne ©2013
Schematic View of Swapping
Operating System Concepts – 9th Edition 8.23 Silberschatz, Galvin and Gagne ©2013
Contiguous Allocation
The main memory must accommodate both operating system and the
various user spaces. Thus memory allocation should be done
efficiently.
The memory is usually divided into two partitions: one for the
resident operating system and one for the user processes.
The operating system normally placed in the low memory
User processes then held in high memory
Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne ©2013
Single Process Systems
OS
Single Process Allocation
Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne ©2013
Fixed Partition Memory
1000 K 1000 K
300 K
Partition # 1
200 K
100 K
OS
0K
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne ©2013
Fixed Partition Memory
Wastage of memory within the apace 1000 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.27 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
1000 K
In this technique, processes are 975 K
loaded in consecutive areas until the Available
ProcessSpace
D
memory is filled or more likely the 225 K
remaining space is too small to 750 K
accommodate another process.
Process C
300 K
450 K
Process B
200 K
250 K
Process A
150 K
100 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
1000 K
When a process terminates the space 975 K
occupied is freed and become Free
Available
Process Space
250 K D
available for the loading of new 225 K
process 750 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.29 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
1000 K
Possible Solution 975 K
Free
1. Coalescing of Holes Available Space
250 K
2. Compaction of Holes
750 K
Process C
300 K
450 K
Free
200 K
250 K
Process A
150 K
100 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.30 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
1000 K
1. Coalescing of Holes: A Process 975 K
adjacent to one or more holes will Free
Available Space
250 K
terminate and free its space
allocation. This results in two or 750 K
three adjacent holes which can then Three Holes Coalesce
be viewed and utilized as a single Total Free Space
Process C
hole 300 K
750 K
450 K
As in this case Process C will be
Free
terminated to make Three- 200 K
Holes-Coalesce
250 K
Process A
150 K
100 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.31 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
1000 K
2. Compaction of Holes: The other 975 K
possibility is to physically moving Free
Available Space
250 K
the resident process about the
Free Space
memory in order to close up the 450 K 750 K
holes and hence bring the free space
into a single large block Process C
300 K
450 K
Process C
FreeK
300
200 K
250 K
Process A
150 K
100 K
OS
Fixed Partition Allocation
Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne ©2013
Variable Partition Memory
Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne ©2013
Storage Placement Policies
When new processes have to be loaded it is necessary to try to
select the best location in which to place them by keeping in
mind that try to provide maximum overall throughput for the
system.
Operating System Concepts – 9th Edition 8.34 Silberschatz, Galvin and Gagne ©2013
Storage Placement Policies
1. Best Fit: An incoming process is placed in a hole in which it
fits mostly tightly
Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
Available Memory
slots
100 K
300 K
200 K
400 K
Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
Available Memory
slots
100 K
300 K
400 K
Best Fit ?
Operating System Concepts – 9th Edition 8.37 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
Available Memory
slots
100 K
300 K
200 K
Worst Fit ?
Operating System Concepts – 9th Edition 8.38 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
Available Memory
slots
100 K
200 K
400 K
First Fit ?
Operating System Concepts – 9th Edition 8.39 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
A variable partition memory system has following holes sizes in the given order
20 K
A new process is to be loaded of size 25 K which
hole size would be filled using Best Fit ? Worst 15 K
Fit ? First Fit ?
40 K
60 K
10 K
25 K
Operating System Concepts – 9th Edition 8.40 Silberschatz, Galvin and Gagne ©2013
Example: Storage Placement Policies
A variable partition memory system has following holes sizes in the given order
20 K
A new process is to be loaded of size 25 K which
hole size would be filled using Best Fit ? Worst 15 K
Fit ? First Fit ?
First Fit 40 K
Worst Fit 60 K
10 K
Best Fit 25 K
Operating System Concepts – 9th Edition 8.41 Silberschatz, Galvin and Gagne ©2013
Paging
Paging is a memory management scheme that permits the
physical address space of a process to be noncontiguous.
Operating System Concepts – 9th Edition 8.42 Silberschatz, Galvin and Gagne ©2013
Paging
Each Process is divided into number The memory space
A1
of fixed size chunks called Pages A2
is also viewed as a
set of page frames
A3
of the same size
Process A A4
B1
A1
Process B B2
A2
B3
A3
B1 B4
A4
B2 B5
B3 Process C C1
B4 C2
C1
B5 C3
C2
C3
Operating System Concepts – 9th Edition 8.43 Silberschatz, Galvin and Gagne ©2013
Paging
A1 A1 A1
A2 A2 A2
A3 A3 A3 Frames do not
A4 A4 A4 need to be
B1 -- D1 Contiguous
B2 -- D2
B3 -- D3
B4 -- E1
B5 -- E2
C1 C1 C1
C2 C2 C2
C3 C3 C3
-- E3
-- E4
--
Operating System Concepts – 9th Edition 8.44 Silberschatz, Galvin and Gagne ©2013
Binary Basics
1. Using 1 Bit we can represent 2^1 i.e 2 memory locations
.
2. Using 2 bits, we can represent 2^2 i.e. 4 memory locatio
ns.
3. Using 3 bits, we can represent 2^3 i.e. 8 memory locatio
ns.
Operating System Concepts – 9th Edition 8.45 Silberschatz, Galvin and Gagne ©2013
Binary Basics
Operating System Concepts – 9th Edition 8.46 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
If the size of the logical address space is 2 m, and a page size is 2n
bytes
Operating System Concepts – 9th Edition 8.47 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
Logical Address generated by CPU is divided into:
Operating System Concepts – 9th Edition 8.48 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
Logical 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
p d
m-n n
Operating System Concepts – 9th Edition 8.49 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
Operating System Concepts – 9th Edition 8.51 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
Physical Address
f d
Operating System Concepts – 9th Edition 8.52 Silberschatz, Galvin and Gagne ©2013
Paging
The size of a page is a power of 2, the typical page size lying between
1K (210) and 16K (214).
Operating System Concepts – 9th Edition 8.54 Silberschatz, Galvin and Gagne ©2013
Paging Hardware
Operating System Concepts – 9th Edition 8.55 Silberschatz, Galvin and Gagne ©2013
Paging Hardware
Operating System Concepts – 9th Edition 8.56 Silberschatz, Galvin and Gagne ©2013
Paging Model of Logical and Physical Memory
Operating System Concepts – 9th Edition 8.57 Silberschatz, Galvin and Gagne ©2013
Paging Example
0 n =2 and m=4
1 2
3
2
3
5
Operating System Concepts – 9th Edition 8.58 Silberschatz, Galvin and Gagne ©2013
Free Frames
Operating System Concepts – 9th Edition 8.59 Silberschatz, Galvin and Gagne ©2013
Examples of Pageing
Operating System Concepts – 9th Edition 8.60 Silberschatz, Galvin and Gagne ©2013
Implementation of Page Table
In the CPU registers: This is OK for small process address spaces and
large page sizes. It has the advantage of having effective memory
access time about the same as memory access time.
Operating System Concepts – 9th Edition 8.61 Silberschatz, Galvin and Gagne ©2013
Paging Hardware With TLB
Operating System Concepts – 9th Edition 8.62 Silberschatz, Galvin and Gagne ©2013
Effective Access Time
The percentage of times that the page number of interest is found in the
TLB is called the hit ratio.
An 80-percent hit ratio, for example, means that we find the desired
page number in the TLB 80 percent of the time.
If we fail to find the page number in the TLB then we must first access
memory for the page table and frame number (100 nanoseconds) and
then access the desired byte in memory (100 nanoseconds), for a total
of 200 nanoseconds.
Operating System Concepts – 9th Edition 8.63 Silberschatz, Galvin and Gagne ©2013
Effective Access Time
To find the effective memory-access time, we weight the case
by its probability:
Operating System Concepts – 9th Edition 8.64 Silberschatz, Galvin and Gagne ©2013
Effective Access Time
For a 99-percent hit ratio we have
Operating System Concepts – 9th Edition 8.65 Silberschatz, Galvin and Gagne ©2013
Shared Pages
Shared code
One copy of read-only (reentrant or non-self-modifying) code shared
among processes (i.e., text editors, compilers, window systems)
Similar to multiple threads sharing the same process space
Also useful for interprocess communication if sharing of read-write
pages is allowed
Operating System Concepts – 9th Edition 8.66 Silberschatz, Galvin and Gagne ©2013
Shared Pages
Consider a system that supports 40 users, each of whom executes a text
editor. If the text editor consists of 150 KB of code and 50 KB of data
space, we need 8,000 KB to support the 40 users.
By Using shared Pages Technique, Each user’s page table maps onto
the same physical copy of the editor, but data pages are mapped onto
different frames.
Thus, to support 40 users, we need only one copy of the editor (150 KB),
plus 40 copies of the 50 KB of data space per user. The total space
required is now 2,150 KB instead of 8,000 KB — a significant savings
Operating System Concepts – 9th Edition 8.67 Silberschatz, Galvin and Gagne ©2013
Shared Pages Example
Operating System Concepts – 9th Edition 8.68 Silberschatz, Galvin and Gagne ©2013
Segmentation
A logical address space is a collection of segments.
Each segment has a name and a length. The addresses specify both
the segment name and the offset within the segment.
Operating System Concepts – 9th Edition 8.69 Silberschatz, Galvin and Gagne ©2013
Segmentation
An example of the logical address space of a process with
segmentation is shown below.
Operating System Concepts – 9th Edition 8.70 Silberschatz, Galvin and Gagne ©2013
Logical View of Segmentation
1
4
1
3 2
4
Operating System Concepts – 9th Edition 8.71 Silberschatz, Galvin and Gagne ©2013
Logical View of Segmentation
Operating System Concepts – 9th Edition 8.72 Silberschatz, Galvin and Gagne ©2013
Segment Table Architecture
Each entry of a segment table has a base and a
segment limit.
Operating System Concepts – 9th Edition 8.73 Silberschatz, Galvin and Gagne ©2013
Segmentation Architecture
There are two more registers, relevant to the concept of segmentation:
Operating System Concepts – 9th Edition 8.74 Silberschatz, Galvin and Gagne ©2013
Segmentation Hardware
Operating System Concepts – 9th Edition 8.75 Silberschatz, Galvin and Gagne ©2013
Segmentation Hardware
Operating System Concepts – 9th Edition 8.76 Silberschatz, Galvin and Gagne ©2013
Protection in Segmentation
Consider the following segment table
Operating System Concepts – 9th Edition 8.77 Silberschatz, Galvin and Gagne ©2013
Protection in Segmentation
The bits associated with each entry in the segment table, for the
purpose of protection are:
Operating System Concepts – 9th Edition 8.78 Silberschatz, Galvin and Gagne ©2013
Issues with Segmentation
Segmentation may then cause external fragmentation (i.e. total memory
space exists to satisfy a space allocation request for a segment, but
memory space is not contiguous), when all blocks of memory are too
small to accommodate a segment.
In this case, the process may simply have to wait until more memory (or
at least a larger hole) becomes available or until compaction creates a
larger hole
Operating System Concepts – 9th Edition 8.79 Silberschatz, Galvin and Gagne ©2013
End of Chapter 8
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013