Unit III Memory Management
Objectives
We know that CPU can be shared by a set of processes. As a result of CPU scheduling, we can improve both the CPU utilization and the speed of the computer’s response to the users.
To realize this increase in performance, however we must keep several processes in memory; that is , we must share memory.
The main objective here is to understand the various ways of managing memory.
Address Binding
Program must be brought (from disk) into memory for it to be executed.
Depending on the memory management in use, the process may be moved between disk and memory during its execution.
The collection of processes on the disk that is waiting to be brought into memory for execution forms the input queue.
In most cases, a user program will go through several steps where binding takes place at every level.
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 you know at compile time where the process will reside in memory, then 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
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
Logical address – generated by the CPU; also referred to as
virtual address.
Physical address – address seen by the memory unit, one which
Logical vs. Physical Address Space
Logical address space : The set of all logical addresses generated by a
program is a logical address space.
Physical address space: The set of all physical addresses corresponding
Memory-Management Unit (
MMU
)
Hardware device that maps virtual to physical address
In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory
The user program deals with logical addresses; it never sees the
Swapping
A process can be swapped temporarily out of memory to a backing store, and then
brought back into memory for continued execution.
Assume a multiprogramming environment with round robin scheduling implementation.
The quantum must be sufficiently large that reasonable amounts of computing are
done between swaps.
Roll out, roll in – swapping variant used for priority-based scheduling algorithms;
lower-priority process is swapped out so higher-priority process can be loaded and executed.
Normally, a process that is swapped out will be swapped back into the same memory
Swapping
Backing store – fast disk large enough to accommodate copies of all
memory images for all users; must provide direct access to these memory images
Contiguous Allocation
Main memory usually into two partitions:
Resident operating system
User processes
Relocation registers used to protect user processes from each other, and from changing operating-system code and data
Relocation/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
Base and Limit Registers
Memory Management
1. Keep track of what parts of memory are in use.
2. Allocate memory to processes when needed.
3. Deallocate when processes are done.
Contiguous Allocation (Cont)
Multiple-partition allocation
Hole – block of available memory; holes of various size are
scattered throughout memory
When a process arrives, it is allocated memory from a hole
large enough to accommodate it
Operating system maintains information in a table about:
a) allocated partitions b) free partitions (hole)
Dynamic Storage-Allocation Problem
First-fit: Allocate the first hole that is big enough
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
Produces the largest leftover hole
How to satisfy a request of size n from a list of free holes. There are
many solutions to this problem. The set of holes is searched to
determine which hole is best to allocate.
Best Fit
Best fit: The allocator places a process in the smallest block of unallocated memory in which it will fit. For example, suppose a process requests 12KB of memory and the memory manager currently has a list of unallocated blocks of 6KB, 14KB, 19KB, 11KB, and 13KB blocks. The best-fit strategy will allocate 12KB of the 13KB block to the process.
Advantage
Memory utilization is much better than first fit as it searches the smallest free partition first available.
Disadvantage
Worst Fit
Worst fit: The memory manager places a process in the largest block of unallocated memory available. Using the same example as above, worst fit will allocate 12KB of the 19KB block to the process, leaving a 7KB block for future use.
In worst fit approach is to locate largest available free portion so that the portion left will be big enough to be useful. It is the reverse of best fit.
Advantage
Reduces the rate of production of small gaps.
Disadvantage
First Fit
First fit: There may be many holes in the memory, so the operating system, to reduce the amount of time it spends analyzing the available spaces, begins at the start of primary memory and allocates memory from the first hole it encounters large enough to satisfy the request. Using the same example as above, first fit will allocate 12KB of the 14KB block to the process.
Advantage
Fastest algorithm because it searches as little as possible.
Disadvantage
Example
The shaded areas are allocated blocks; the white
areas are free blocks.
The next FOUR memory requests are 20K, 50K, 10K
and 30K (loaded in that order).
Using the following placement algorithms, show the
partition allocated for the requests.
First-fit
Best-fit
Next-fit
40K
80K
30K
50K
30K
1
29
Example: First-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
Example: First-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
31
Example: First-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
Allocate for 10K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
20
50
Example: First-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
Allocate for 10K
Allocate for 30K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
20
50
33
Example: Best-fit
20K, 50K, 10K and 30K (in that order).
40K
80K
30K
50K
30K
1
Example: Best-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
35
Example: Best-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
Example: Best-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
Allocate for 10K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
20
50
37
Example: Best-fit
20K, 50K, 10K and 30K (in that order).
Allocate for 20K
Allocate for 50K
Allocate for 10K
Allocate for 30K
40K
80K
30K
50K
30K
1
0K
2
0K
20
K
2
0K
10
K
1
0K
2
0K
20
50
Contiguous Allocation Techniques
Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb, 600Kb (in order), how would the first-fit, best-fit, and worst-fit algorithms place
Contiguous Allocation Techniques
First-fit:
212K is put in 500K partition 417K is put in 600K partition
112K is put in 288K partition (new
partition 288K = 500K - 212K)
426K must wait
Best-fit:
212K is put in 300K partition 417K is put in 500K partition 112K is put in 200K partition 426K is put in 600K partition
Worst-fit:
212K is put in 600K
partition 417K is put in 500K partition 112K is put in 388K partition
426K must wait In this example,
FRAGMENTATION
All the
preceding algorithms suffer from:External Fragmentation
As processes are loaded and removed from memory the free memory is broken into little pieces and enough total space exists to satisfy
a
request, but it is not contiguous.
Internal Fragmentation
Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used
One solution to Reduce external fragmentation is by compaction
Shuffle memory contents to place all free memory together in
one large block
Compaction is possible only if relocation is dynamic, and is
done at execution time as it requires changing the base register to reflect the new base address.
This scheme can be expensive.
Another possible solution is to permit the logical address space
Paging
Paging is a memory management scheme that permits the physical address
space of a process to be noncontiguous;
Divide physical memory into fixed-sized blocks called frames (size is power of 2,
between 512 bytes and 16 MB per page)
Divide logical memory into blocks of same size called pages
When a process is to be executed, its pages are loaded into any available
frames from the backing store. The backing store is divided into fixed sized
Paging Example
Paging
No External Fragmentation.
Internal fragmentation is still there.
Frame Table
Since the operating system is managing physical memory, it must be aware of the allocation details of physical memory.: which frames are allocated, which frames are available, how many total frames are there and so on.
Hardware Support
Each operating system has its own methods for storing page tables. Most
allocate a page table for each process. And a pointer to the page table is stored with the other register values in the process control block.
The hardware implementation of the page table can be done in several
ways. In the simplest case, the page table is implemented as a set of dedicated registers.
The use of registers for page table is satisfactory if the page table is
Implementation of Page Table
Most computers, however allow the page table to be very large (e.g. 1 million
entries) . For these machines, use of fast registers to implement the page table is infeasible. Rather, the Page table is kept in main memory and a
Page-table base register (PTBR) points to the page table
Page-table length register (PRLR) indicates size of the page table
In this scheme every data/instruction access requires two memory accesses.
One for the page table and one for the data/instruction.
The two memory access problem can be solved by the use of a special
Effective Access Time
If the page number is found in the TLB, that is known as TLB hit. Otherwise, it is known as TLB miss.
The percentage of times that a particular page number is found in the TLB is called TLB hit ratio.
Consider a paging hardware with a TLB. Assume that the entire page table and all the pages are in the physical memory. It takes 10 milliseconds to search the TLB and 80 milliseconds to access the physical memory. If the TLB hit ratio is 0.6, the effective memory access time (in milliseconds) is _________.
(A) 120
(B) 122
Memory Protection
Memory protection in a paged environment is accomplished by associating
protection bit with each frame.
Normally, these bits are kept in the page table. One bit can define a page to be
read – write or read – only.
Valid-invalid bit attached to each entry in the page table:
“valid” indicates that the associated page is in the process’ logical address
space, and is thus a legal page
Structure of the Page Table
Hierarchical Paging
Hashed Page Tables
Hierarchical Page Tables
Break up the logical address space into multiple page 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:
where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table
page number page offset
pi p2 d
Hashed Page Tables
Common in address spaces > 32 bit
The virtual page number is hashed into a page table
This page table contains a chain of elements hashing to the same location
because the
same hash function can have same value for different page no. .
Each element contains
(1) the virtual page number
(2) the value of the mapped page frame
(3) a pointer to the next element
Virtual page numbers are compared in this chain searching for a match
Inverted Page Table
Usually, each process has an associated page table. The page table has one entry for each real page of memory
Since, the table is sorted by virtual address, the operating system is able to calculate where in the table the associated physical address entry is and to use that value directly.
Drawback is that large amount of physical memory is used just to keep track of how other physical memory is used. To solve this, use INVERTED PAGE TABLE.
Decreases memory needed to store each page table, but increases time
Inverted Page Table
An alternate approach is to use the Inverted Page Table structure that consists of one
page table entry for every frame of the main memory. So the number of page table entries in the Inverted Page Table reduces to the number of frames in physical memory and a single page table is used to represent the paging information of all the processes.
Through inverted page table, the overhead of storing an individual page table for every
process gets eliminated and only a fixed portion of memory is required to store the paging information of all the processes together.
This technique is called as inverted paging as the indexing is done with respect to the
Inverted Page Table
Advantages and Disadvantages:
Reduced memory space –
Inverted Page tables typically reduces the amount of memory required to store the page tables to a size bound of physical memory. The maximum number of entries could be the number of page frames in the physical memory.
Longer lookup time –
Segmentation
An important aspect of memory management that became unavoidable with
paging is the separation of user’s view of memory and the actual physical memory.
Memory-management scheme that supports user view of memory
A program is a collection of segments
A segment is a logical unit such as:
main program procedure function method object
local variables, global variables stack
Segmentation
Elements within a segment are identified by their offset from the beginning of the segment: the first statement of the program, the seventh stack frame entry in the stack and so on.
Segmentation is a memory management technique that supports this user view of memory.
A logical address space is a collection of segments. Each segment has a name and length.
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Segment table – maps physical addresses; each table entry has:
base – contains the starting physical address where the segments
reside in memory
Segmentation
For example, segment 2 is 400 bytes long and begins at location 4300. thus a reference to byte 53 of segment 2 is mapped onto location
4300+53 = 4353.
A reference to segment 3 byte 852 is mapped to 3200+852 = 4052.
External Fragmentation
Segmentation may cause external fragmentation, when all blocks of free memory are too small to accommodate a segment.
In this case, the process may simply have to wait until some memory ( or at least a larger hole) becomes available
Example: The Intel Pentium
Supports both segmentation and segmentation with paging
In Pentium systems, the CPU generates logical addresses which are
Given to segmentation unit
Which produces linear addresses
Linear address given to paging unit
Which generates physical address in main memory
Segmentation and paging units form the equivalent of MMU
Intel Pentium Segmentation
The Pentium architecture allows a segment to be as large as 4 GB and the
Maximum number of segments per process is 16 KB.
The logical address space of a process is divided into two partitions.
First 8 KB – private to that process (Local descriptor table)
Last 8 KB – shared among all the processes. (Global descriptor table).
Each entry in the LDT and GDT consists of an 8-byte segment descriptor with detailed information about a particular segment , including the base location and limit of that segment.
The logical address is a pair (selector, offset) where the selector is a 16-bit number:
In which s designates the segment number
G indicates whether the segment is in GDT or LDT P deals with the protection.
Intel Pentium Segmentation
The segment number in selector part points to the appropriate entry in the GDT or LDT.
The base and limit information about the segment in question is used to generate the linear address.
First, the limit is checked for address validity. If the address is not valid, a memory fault is generated.
Intel Pentium Paging
The Pentium architecture allows a page size of either 4 KB or 4 MB.
For 4 KB pages, the Pentium uses a two level paging scheme in which the division of the 32 bit linear address is as follows:
Pentium Paging Architecture
The ten high order bits reference an entry in the outermost page table, which the Pentium terms the page directory.(The CR3 register points to the page directory for the current process)
The page directory entry points to the inner page table that is indexed by the contents of the innermost ten bits in the linear address.
Pentium Paging Architecture
One entry in the page directory is the page size flag, which if set indicates that the size of the page frame is 4 MB.