Professional Documents
Culture Documents
Chapter 3 Memory Management Part II
Chapter 3 Memory Management Part II
Fixed/Variable
Partitioning
Contiguous Allocation
An executing process must be loaded entirely in main memory (if
overlays are not used).
Main memory is usually split into two (Memory split) or more
(Memory division) partitions:
Resident operating system, usually held in low memory partition
with interrupt vector.
User processes then held in high memory partitions.
Relocation registers used to protect user processes from each other,
and from changing OS 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.
2
Real Memory Management Techniques
Although the following simple/basic memory management
techniques are not used in modern OSs, they lay the ground for a
later proper discussion of virtual memory:
Fixed/Static Partitioning
Variable/Dynamic Partitioning
Simple/Basic Paging
Simple/Basic Segmentation
3
Fixed Partitioning
Partition main memory into a
or unequal sizes.
Leftover space in partition,
after program assignment, is
called internal fragmentation.
A. Equal-size partitions:
If there is an available partition, a process can be loaded
into that partition:
Because all partitions are of equal size, it does
not matter which partition is used.
If all partitions are occupied by blocked processes, choose
one process to swap out to make room for the new process.
5
Contd.
B. Unequal-size partitions using
multiple queues
assign each process to the smallest
partition within which it will fit.
a queue exists for each partition size.
Tries to minimize internal fragmentation.
problem: some queues might be empty
while some might be loaded.
Solutions: Have at least one small
partition around.
Such a partition will allow small jobs to
run without having to allocate a large
partition for them.
Have a rule stating that a job that is
eligible to run may not be skipped over Fig. 3.14 Unequal-size
more than k times. Partitions using Multiple
queues
Contd.
C. Unequal-size partitions using
single queue:
when it’s time to load a process
process 2 process 2
process 2 process 2
Fig. 3.16
9 Variable(dynamic)Partitions
Managing allocated and free partitions
Example: memory with 5 processes and 3 holes:
tick marks show memory allocation units.
shaded regions (0 in the bitmap) are free.
14
Reducing External Fragmentation
Reduce external fragmentation by doing compaction:
contiguous.
Compaction reduces external fragmentation by shuffling memory
requires.
Eventually holes are formed in main memory. This can cause
external fragmentation.
Must use compaction to shift processes so they are
16
number of Tasks).
Dynamic Storage-Allocation Problem
Satisfy request of size n from list of free holes: four basic
methods:
First-fit: Allocate the first hole that is big enough.
Next-fit: Same logic as first-fit but starts search always
from the last allocated hole (need to keep a pointer to this)
in a wraparound fashion.
Best-fit: Allocate the smallest hole that is big enough; must
search entire list, unless ordered by size.
Produces the smallest leftover hole.
Worst-fit: Allocate the largest hole; must also search entire
list.
17 Produces the largest leftover hole.
Placement Algorithm
Used to decide which free
block to allocate to a process.
Goal: to reduce usage of
compaction procedure (Its
time consuming).
Example algorithms:
First-fit
Next-fit
Best-fit
Fig.
Worst-fit (not recommended)? 3.19
Comments on Placement Algorithm
First-fit favors allocation near the beginning: tends to
create less fragmentation then Next-fit.
Next-fit often leads to allocation of the largest block at
the end of memory.
Best-fit searches for smallest block: the fragment left
behind is small as possible:
Main memory quickly forms holes too small to hold any
process: compaction generally needs to be done more often.
Slower in speed compared to First/Next-fit.
19
fitting) in terms of speed and storage utilization.
Replacement Algorithm
20
Knuth’s Buddy System
The buddy memory allocation technique is a memory allocation
algorithm that divides memory into partitions to try to satisfy a
memory request as suitably as possible.
This system makes use of splitting memory into halves to try to
give a best-fit.
Implementation and Consequences
Is relatively easy compared to the memory allocation techniques
that modern operating systems use.
Has little external fragmentation compared to other simpler
techniques such as dynamic allocation and has little overhead
trying to do compaction of memory.
It may generate a moderate amount of internal fragmentation.
21
How it works?
The buddy memory allocation technique allocates memory in powers
of 2, i.e. 2x, where x is an integer.
Thus, the programmer has to decide on, or to write code to obtain, the
upper limit of x.
For instance, if the system had 2000K of physical memory, the upper
limit on x would be 10, since 210 (1024K) is the biggest allocatable
block.
This results in making it impossible to allocate everything in a single
chunk; the remaining 976K of memory would have to be taken in
smaller blocks.
Memory blocks are available in size of 2^{K} where L <= K <= U
and where:
2^{L} = smallest size of block allocatable.
2^{U} = largest size of block allocatable .
22
Contd.
After deciding on the upper limit, the programmer has to decide
on the lower limit, i.e. the smallest memory block that can be
allocated.
This lower limit is necessary so that the overhead of storing used and
free memory locations is minimized.
Typically this number would be a moderate number (like 2, so that
memory is allocated in 2² = 4K blocks), small enough to minimize
wasted space, but large enough to avoid excessive overhead.
Now that we have our limits, let us see what happens when a
program makes requests for memory. Let's say in this system, L =
7, which results in blocks 27 = 128K in size, and U= 10, which
results in a largest possible allocatable block, 210 = 1024K in size.
23
The following shows a possible state of the system after various
memory requests.
24
Dynamics of Buddy System
We start with the entire block of size 2^{U}.
When a request of size S is made:
If 2^{U-1} < S <= 2^{U} then allocate the entire block of size 2^{U}.
Else, split this block into two buddies, each of size 2^{U-1}.
If 2^{U-2} < S <= 2^{U-1} then allocate one of the 2 buddies.
Otherwise one of the 2 buddies is split again.
This process is repeated until the smallest block greater or equal to S is generated.
Two buddies are coalesced whenever both of them become unallocated.
The OS maintains several lists of holes:
the i-list is the list of holes of size 2^{i}.
whenever a pair of buddies in the i-list occur, they are removed from that list
and coalesced into a single hole in the (i+1)-list.
Presented with a request for an allocation of size k such that 2^{i-1} < k <=
2^{i}:
the i-list is first examined.
25 if the i-list is empty, the (i+1)-list is then examined ...
Comments on Buddy System
Mostly efficient when the size M of memory used by the
26
Operating Systems
Simple/Basic Paging
Simple/Basic Paging
Idea: logical address space of a process can be made noncontiguous;
process is allocated physical memory wherever it is available.
Divide physical memory into relatively small, equal sized blocks
called frames (size is power of 2, usually between 512 bytes and
8192 bytes).
Each process is also divided into blocks of the same size called
pages.
The system keeps track of all free frames.
page table.
Page table length register indicates the
31
Simple/Basic Paging
To run a program of size n pages, need to find any n free frames and load all
the program (pages).
So need to keep track of all free frames in physical memory; use free-frame list.
33
Contd.
When process A and C are blocked,
39
How to implement Page Table?
1. Keep Page Table in main memory:
Page-table base register (PTBR) points to the page table.
Page-table length register (PTLR) indicates size of the page
table.
However, in this scheme, every data/instruction access requires two
memory accesses, one for the page table and one for the
data/instruction.
2. Keep Page Table in hardware (in MMU): however, page table can be large,
too expensive.
3. Use a special fast-lookup hardware cache called Associative Memory
(Registers) or Translation Look-aside Buffer (TLB)
If p is in associative register, get frame # out.
Otherwise get frame # from page table in memory.
40
Flow chart for
Paging Hardware With TLB TLB
The TLB must be flushed each time a new process enters the
41
running state.
Effective Access Time (EAT)
Suppose:
TLB lookup takes 5 nano sec.
42
Memory Protection
Is a way to control memory access rights on a computer, and is a part of most
modern operating systems.
The main purpose of memory protection is to prevent a process from accessing
memory that has not been allocated to it.
This prevents a bug within a process from affecting other processes, or the
operating system itself..
Memory protection implemented by associating a protection bit with each frame.
43
Contd.
44
Shared Pages
1. Shared code:
One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems).
Shared code must appear in same location in the logical address space of all
processes.
2. Private code and data:
Each process keeps separate copy of code and data.
The pages for the private code and data can appear anywhere in the logical
address space.
Sh
ar
Ex ed
am Pag
pl es
e
45
Page Table Types
Essentially, a page table must store the virtual address, the
physical address, and possibly some address space
information like a bit that is set when the page is modified,
the protection code (read/write/execute permissions).
There are several different types of page tables, that are
best suited for different requirements.
1. Inverted Page Tables
2. Multilevel Page Tables
3. Virtualized Page Table
4. Nested Page Tables
46
1. Inverted Page Table(IPT)
Traditional page tables require one entry per virtual page, since they
are indexed by virtual page number.
As virtual address spaces grow so much bigger than physical
addresses, it becomes possible that we can save space and time by
inverting the page table, mapping physical to virtual, instead of the
other way around.
ITE decreases memory needed to store each page table, but increases
time needed to search the table when a page reference occurs.
Although inverted page tables save vast amounts of space, they have
a serious downside: virtual-to-physical translation becomes much
harder and time consuming.
Use hash table to limit the search to one, or at most a few, page table
entries.
47
Contd.
Then, each inverted Page table entry (IPTE) has to contain:
The process ID of the owning process.
The virtual page number.
A pointer to the next IPTE in the hash chain.
The normal protection, valid, modified bits referenced.
48
Contd.
The steps in a translation are:
1. Hash the process ID(PID) and virtual page
number (VPN) to get an index into the HAT
(hash anchor table).
2. Look up a Physical Frame Number in the HAT.
3. Look at the inverted page table entry, to see if it
is the right PID and VPN. If it is, you're done.
4. If the PID or VPN does not match, follow the
pointer to the next link in the hash chain.
Again, if you get a match then you're done; if
you don't, then you continue.
Eventually, you will either get a match or you
will find a pointer that is marked invalid. If you
get a match, then you've got the translation; if
you get the invalid pointer, then you have a
miss.
49
Multilevel Page Tables
Also known as hierarchical page tables.
Is a means of using page tables for large address spaces.
Instead of having a table which keeps a listing of mappings for all frames
in physical memory, in case of IPT, we could create a page table structure
that contains mappings for virtual pages.
Break up the logical address space into multiple page tables.
Each of these smaller page tables are linked together by a master page
table, effectively creating a tree data structure.
The secret to the multilevel page table method is to avoid keeping all the
page tables in memory all the time.
A virtual address in this schema could be split into three, the index in the
root page table, the index in the sub-page table, and the offset in that
50
page.
Two-Level Page-Table Scheme Two-Level Page-Table
Example
Second-
level page
51 tables
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:
52
Three-level Paging Scheme (64-bit)
53
Virtualized Vs. Nested Page Tables
It was mentioned that creating a page table structure that contained
mappings for every virtual page in the virtual address space could end
up being wasteful.
But, we can get around the excessive space concerns by putting the
page table in virtual memory, and letting the virtual memory system
manage the memory for the page table.
However, part of this linear page table structure must always stay
resident in physical memory, in order to prevent against circular page
faults, that look for a key part of the page table that is not present in
the page table, which is not present in the page table, etc.
Nested page tables can be implemented to increase the performance
of hardware virtualization.
54