Main Memory
Main Memory
CHAPTER 3
MAIN MEMORY
1
OUTLINE
Background
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Structure of the Page Table
Example: The Intel 32 and 64-bit Architectures
Example: ARM Architecture
OBJECTIVES
3
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
If the data are not in memory, they must be moved there before the
CPU can operate on them.
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
4
MEMORY HIERARCHY
5
APPLICATION MEMORY
6
MEMORY ALLOCATION TO A PROCESS
Stacks:
Allocations and deallocations are performed in a LIFO manner.
Only the last entry of the stack is accessible at any time
A contiguous area of memory is reserved for the stack
Stack is used to support function calls.
7
MEMORY ALLOCATION TO A PROCESS
8
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
9
HARDWARE ADDRESS PROTECTION
The base and limit registers can be loaded only by the operating
system, which uses a special privileged instruction.
10
LOGICAL VS. PHYSICAL ADDRESS SPACE
The user program deals; with logical addresses it never sees the real
physical addresses
Execution-time binding occurs when reference is made to location in memory
Logical addresses must be mapped to physical addresses before they are used
12
DYNAMIC RELOCATION USING A RELOCATION REGISTER
13
DYNAMIC RELOCATION USING A RELOCATION REGISTER
14
DYNAMIC LINKING
Static linking – system libraries and program code combined by the loader into the binary
program image
Dynamic linking – 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
16
SWAPPING
18
CONTEXT SWITCH TIME INCLUDING SWAPPING
If next processes to be put on CPU is not in memory, need to swap out a process
and swap in target process
Context switch time can then be very high
100MB process swapping to hard disk with transfer rate of 50MB/sec
Swap out time of 2 seconds
Can reduce if reduce size of memory swapped – by knowing how much memory
really being used
System calls to inform OS of memory use via request_memory() and release_memory()
19
SWAPPING ON MOBILE SYSTEMS
21
HARDWARE SUPPORT FOR RELOCATION AND LIMIT
REGISTERS
22
CONTIGUOUS ALLOCATION (CONT.)
Relocation registers used to protect user processes from each other, and from
changing operating-system code and data
Base register contains value of smallest physical address
Limit register contains range of logical addresses – each logical address must be less than
the limit register
MMU maps logical address dynamically
If a device driver (or other operating-system service) is not commonly used, we do not
want to keep the code and data in memory, as we might be able to use that space for
other purposes. Such code is sometimes called transient operating-system code;
23
MEMORY ALLOCATION
26
TWO WAYS TO TRACK MEMORY USAGE
27
DYNAMIC STORAGE-ALLOCATION
28
DYNAMIC STORAGE-ALLOCATION
29
FRAGMENTATION
30
FRAGMENTATION
31
COMPACTION
32
SEGMENTATION
34
LOGICAL VIEW OF SEGMENTATION
4
1
3 2
4
35
user space physical memory space
SEGMENTATION ARCHITECTURE
<segment-number, offset>,
Segment table – maps two-dimensional physical addresses; each table entry has:
base – contains the starting physical address where the segments reside in memory
limit – specifies the length of the segment
Segment-table base register (STBR) points to the segment table’s location in
memory
Segment-table length register (STLR) indicates number of segments used by a
program;
segment number s is legal if s < STLR
36
SEGMENTATION HARDWARE
37
EXAMPLE OF SEGMENTATION.
38
OUTLINE
Background
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Structure of the Page Table
Example: The Intel 32 and 64-bit Architectures
Example: ARM Architecture
39
PAGING
Physical address space of a process can be noncontiguous; process is allocated physical memory
whenever the latter is available
Avoids external fragmentation
Avoids problem of varying sized memory chunks
Divide physical memory into fixed-sized blocks called frames
Size is power of 2, between 512 bytes and 1Gb
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames and load program
Set up a page table to translate logical to physical addresses
Backing store likewise split into pages
Still have Internal fragmentation
40
ADDRESS TRANSLATION SCHEME
41
PAGING HARDWARE
42
PAGING MODEL OF LOGICAL AND PHYSICAL MEMORY
43
PAGING EXAMPLE
45
OBTAINING THE PAGE SIZE ON UNIX SYSTEMS
The page size varies according to architecture, and there are several
ways of obtaining the page size. One approach is to use the
getpagesize() system call.
Another strategy is to enter the following command on the command
line:
getconf PAGESIZE
Each of these techniques returns the page size as a number of bytes.
46
FREE FRAMES
Page tables are too large to be kept on the chip (Registers). Page
table is kept in main memory.
Page-table base register (PTBR) points to the beginning of the
page table for this process
In this scheme every data/instruction access requires two memory
accesses
One for the page table and one for the data / instruction
P a ge # F ra m e #
49
PAGING HARDWARE WITH TLB
50
EFFECTIVE ACCESS TIME
52
MEMORY PROTECTION
53
VALID (V) OR INVALID (I) BIT IN A PAGE TABLE
Shared code
One copy of read-only (reentrant) 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
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear anywhere in the logical
address space
55
SHARED PAGES EXAMPLE
56
STRUCTURE OF THE PAGE TABLE
58
TWO-LEVEL PAGE-TABLE SCHEME
59
TWO-LEVEL PAGING EXAMPLE
A logical address (on 32-bit machine with 1K page size) is divided into:
a page number consisting of 22 bits
a page offset consisting of 10 bits
Since the page table is paged, the page number is further divided into:
a 12-bit page number
a 10-bit page offset
Thus, a logical address is as follows:
Where p is an index into the outer page table, and p is the displacement within the
1 2
page of the inner page table
Known as forward-mapped page table 60
ADDRESS-TRANSLATION SCHEME
61
THREE-LEVEL PAGING SCHEME
62
VIRTUAL MEMORY
63
BACKGROUND
64
DEMAND PAGING
65
BASIC CONCEPTS
When a process is to be
swapped in, the pager guesses
which pages will be used before
the process is swapped out
again.
Instead of swapping in a whole
process, the pager brings only
those pages into memory.
It avoids reading into memory
pages that will not be used
anyway, decreasing the swap 66
time and the amount of
STEPS IN HANDLING A PAGE FAULT
67
PERFORMANCE OF DEMAND PAGING
69
PAGE REPLACEMENT
70
FIFO PAGE REPLACEMENT
Page fault=?
71
OPT(OPTIMAL) PAGE REPLACEMENT
Replace the page that will not be used for the longest period of time.
72
LRU PAGE REPLACEMENT
Most Frequency Used (MFU): The page with the smallest count was
probably just brought in and has yet to be used.
73