Chapter-6 Memory Management
Chapter-6 Memory Management
Chapter-6 Memory Management
Management
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
Cache sits between main memory and CPU registers
Protection of memory is required to ensure correct
operation
Protection
User processes must be restricted so that they only
access memory locations that "belong" to that
particular process.
We can provide this protection by using a pair of base
and limit registers define the logical address space of
a process.
The changing the contents of the base and limit
registers is a privileged activity, and it is allowed only
to the OS kernel.
Protection
Protection
The base register holds the smallest legal physical
address and limit register specifies the size of the
range.
The process can legally access all addresses from
300040 to 420939 (inclusive)
Every memory access made by a user process is
checked against these two registers, and if a memory
access is attempted outside the valid range, then a
fatal error is generated.
The OS obviously has access to all existing memory
locations, as this is necessary to swap users' code
and data in and out of memory.
Hardware Address Protection
CPU must check every memory access generated in
user mode to be sure it is between base and limit for
that user.
The instructions to loading the base and limit
registers are privileged.
Hardware Address Protection
Address Binding
User programs typically refer to memory addresses
with symbolic names such as "i", "count", and
"average-Temperature".
These symbolic names must be mapped
or bound to physical memory addresses, which
typically occurs in several stages:
Binding of Instructions and Data to Memory
Address binding of instructions and data to memory
addresses can happen at three different stages
Compile time: If memory location known a priori,
absolute code can be generated; must recompile
code if starting location changes
Load time: Must generate relocatable code if
memory location is not known at compile time.
Binding is delayed until load time.
Execution time: if the process can be moved
during its execution from one memory segment to
another, then binding delayed until run time
Multistep
Processing
of a User
Program
Page # Frame #
Paging Hardware With TLB
Effective Access Time
Hit ratio – percentage of times that a page number
is found in the TLB
An 80% hit ratio means that we find the desired
page number in the TLB 80% of the time.
Suppose that 10 nanoseconds used to access
memory.
If we find the desired page in TLB then a
mapped-memory access take 10 ns
Otherwise we need two memory access so it is
20 ns
Effective Access Time
Effective Access Time (EAT)
EAT = 0.80 x 10 + 0.20 x 20 = 12
nanoseconds
implying 20% slowdown in access time
Consider amore realistic hit ratio of 99%,
EAT = 0.99 x 10 + 0.01 x 20 = 10.1ns
implying only 1% slowdown in access time.
Valid-invalid (Present/Absent) bit
Valid-invalid(1/0) bit attached to each entry in the
page table:
“valid” indicates that the associated page is in the
memory’
“invalid” indicates that the page is not in the
memory’and generates page fault.
Valid (v) or Invalid (i) Bit In A Page Table
Shared Pages
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 inter-process 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
Shared Pages Example
Shared Pages Example
Structure of the Page Table
Memory structures for paging can get huge using
straight-forward methods
Consider a 32-bit logical address space as on
modern computers
Page size of 4 KB (212)
Page table would have 1 million entries (232 / 212)
If each entry is 4 bytes ➔ each process 4 MB of
physical address space for the page table alone.
Which is too large to reasonably keep in
contiguous memory.
Two-Level Paging Example
A logical address (on 32-bit machine with 4K page
size) is divided into:
a page number consisting of 20 bits
a page offset consisting of 12 bits (4K frame)
One option is to use a two-tier paging system, i.e. to
page the page table.
the 20 bits described above could be broken down
into two 10-bit page numbers. The first identifies an
entry in the outer page table, which identifies where
in memory to find one page of an inner page table.
The second 10 bits finds a specific entry in that inner
page table, which in turn identifies a particular frame
in physical memory
Two-Level Paging Example
1
1 4
2
3 2
4
3