lecture 11: memory management example memory configuration before and after allocation of a 16kb...
Post on 20-Dec-2015
225 views
TRANSCRIPT
Example memory configuration before and after allocation of a 16KB block
8K
12K
22K
18K
8K
6K
14K
36K
Last allocated block (14K)
8K
12K
6K
2K
8K
6K
14K
20K
First Fit (16K)
Best Fit (16K)
Next Fit (16K)
Hardware support for relocation
Base register
Adder
Bounds register Comparator
Process Control Block
Program
Data
Stack
Relative Address
Interrupt to Operating System
Absolute Address
Logical addresses
0000010111011110 000001 0111011110 0001 001011110000
Relative Address = 1502 Logical Address:
Page# = 1, Offset = 478
Logical Address:
Segment# = 1, Offset = 752
User process 2700 bytes
Page 0
Page 1
Page 2
478
Segment 0 750 bytes
Segment 1 1950 bytes
752
Paging
000001 0111011110
6-Bit page 10-Bit offset
Process Page Table
000101
000110
011001
0
1
2
000101 011101111016-Bit physical address
Segment
0001 001011110000
4-Bit segment
10-Bit offset
Process Segment Table
Length Base
0
1
0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 016-Bit physical address
001011101110 0000010000000000
011110011110 0010000000100000+
Memory Management Techniques
Technique Description Strengths Weaknesses
Fixed Partioning Main memory is divided into a number of static partitions at system generation time. A process may be loaded into a partition of equal or greater size
Simple to implement; little operating-system overhead
Inefficient use of memory due to internal fragmenation; number of active processes is fixed
Dynamic Partitioning Partitions are crerated dynamically, so that each process is loaded into a partition of exactly the same size as that process
No internal fragmentation; more efficient use of main memory
Inefficient use of processor due to the need for compaction to counter external fragmentation
Simple paging Main memory is divided into a number of equal size frames. Each process is divided into a number of equal size pages of the same length as frames. A process is loaded by loading all of its pages into available, not necessarily contiguous frames
No external fragmentation A small ammount of internal fragmentation
Simple segmentation Each process is divided into a number of segments. A process is loaded by loading all of its segments into dynamic partitions that need not be contiguous
No internal fragmentation Need for compaction
Paging Technique
• Overlays– When a program is too big to fit in the available
memory, the programmer has to split it into pieces
– The swapping is done by the OS
• Paging– The physical memory is partitioned into small equal
fixed-size chunks (called frames)
– The logical memory is also divided into small and equal fixed-size chunks (called pages)
Paging Technique (ctd)
• Features– Internal fragmentation: only a fraction of the last page
of a process– No external fragmentation– In order to load the pages of a process into non
contiguous frames the OS needs page table for each process
– Logical address consists of a page number and an offset within the page
– Translation logical-to-physical address is done by the processor hardware
Paging Hardware
CPU
PhysicalMemory
Page table
P d
F d
P
Logical Address
PhysicalAddress
TLB hit
TLB miss
Analysis
• Definition– Hit ratio: percentage of times that a page
number is found in the associative registers
• Performance– Effective Access Time: – Hit ratio x TLB access time + Miss ratio x
Memory access time
Segmentation Technique• Segmentation
– The logical memory is divided into a number of segments of different length
– Logical address consists of two parts: segment number and an offset
• Features– With segmentation a process may occupy more than one
partition
– Internal fragmentation: solved
– External fragmentation: Not solved
Real Programs
• An examination of real programs shows as that a program can be divided into two parts:– part of code rarely or never executed
• code handling unusual error conditions
• code handling certain options and features rarely used
• allocation of much more memory needed (arrays, lists, tables)
– part of code usually executed
• Benefit of executing a program that is partially in physical memory
Virtual Memory• Characteristics of paging and segmentation:
– Process may be broken up into a number of pieces (pages or segments)
– Memory references within a process are logical addresses– Logical addresses are dynamically translated into physical
addresses– Pages or segments of a process do not need to be
contiguously located in main memory by using page or segment table
• With these characteristics it is not necessary that all pages or all segments of a process be in main memory during execution
Virtual Memory (ctd)
• Definition
– Virtual memory is an illusion supported by system hardware and software that a user has a vast linear expanse of useful storage
– In fact, a much smaller real memory is used to hold portions of a user’s program during execution
Virtual Memory (ctd)
• Advantages
– A program is not constrained by the physical memory space available
– Users can write an extremely large virtual address space
– The increase in CPU utilisation and throughput
– Less I/O would be needed to load or swap each user program into memory
Virtual Memory Features• Features
– Usually virtual memory systems use paging technique– OS must maintain a page/segment table for each process– OS must maintain a free frame list – A page or segment number and the offset are used to
calculate absolute address– Not all pages or segments of a process need to be in main
memory frames for the process to run– Reading a page or segment into main memory may require
writing a page or segment out to disk (called page fault or segment fault)
Page Table Structure
N. Page Offset N. Frame Offset
Pointer
+
F.No.Page
num
ber
Page tableProcess Main Memory
Register
Virtual Address
PageFrameO
ffse
t
Page Fault
• Three major components of the page-fault service time are required:– Service the page-fault interrupt
– Read in the page
– Restart the process
Page Fault (ctd)
• A page fault causes the following sequence to occur:– Trap to the OS– Save the user registers and process state– Determine that the interrupt is a page fault– Check if the page reference is legal and determine its
location in the disk– Issue a read from the disk to a free frame– While waiting, allocate the CPU to another process– Interrupt form the disk (I/O completed)
Page Fault (ctd)
– Save the registers and the state of the current process
– Determine that the interrupt is from the disk
– Update the page table and other tables to show that the desired page is now in memory
– Wait for the CPU to be allocated to this process again
– Restore the process context (registers, state) and the new page table, then resume the interrupted instruction
SEGMENTATION ONLY
Segment Number Offset
Virtual Address
P M Other Control Bits Length Segment Base
Segment Table Entry
COMBINED SEGMENTATION & PAGING
Segment Number Page number Offset
Virtual Address
P M Other Control Bits Length Segment Base
Segment Table Entry
P M Other Control Bits Frame Number
Page Table Entry
Address translation in a segmentation system
Segment Number S# Offset d
Virtual Address
Seg. Table Ptr
Register
Base + d
Real Address
+
+
Length Base
Segment Table Main Memory
Segment length
d
S
Address translation in a segmentation/paging system
Segment # Page # Offset
Virtual Address
Seg. Table Ptr
Register
Frame # Offset
++
Segment Table Main Memory
Segment length
d
S
P
Page Table
Protection relationships between segmentsMain Memory
Dispatcher
Process A
Process B
Process C
0
20 K
35 K
50 K
80 K90 K
140 K
190 K
All accesses not allowed
Branch instruction not allowed
Reference to data (not allowed)
Reference to data (allowed)
Replacement Policy• Deals with the selection of a page in memory to be replaced
when a new page must be brought in
• Memory management concepts:– The number of frames to be allocated to each active process– The replacement can be
• limited to those of the process that caused the page fault • encompass all frames in main memory
– Frame Locking: Much of the kernel and control structures of the OS is held on locked frames
– the particular page that should be selected for replacement• The first 2 concepts are called “resident set management”
Basic Algorithms• Optimal
– replaces the page that will not be used for the longest period of time
• LRU (Least recently used) – replaces the page in memory that has not been referenced
for the longest time • FIFO
– the pages are removed in round-robin style• Clock policy
– When a page is referenced after a page fault its use bit is set to 1. When a page is replaced the pointer is set to indicate the next frame in the buffer. For replacement, the OS scans the buffer to find a frame with a use bit to 0
Example
• Consider the following page address stream: 232152453252 and a fixed frame allocation of 3 frames
2 2 2 2 2 2 4 4 4 2 2 2
2 3 2 1 5 2 4 5 3 2 5 2
3 3 3 3 3 3 3 3 3 3 3
1 5 5 5 5 5 5 5 5
Optimal
2 2 2 2 2 2 2 2 3 3 3 3
3 3 3 5 5 5 5 5 5 5 5
1 1 1 4 4 4 2 2 2
LRU
Example (ctd)
2 2 2 2 5 5 5 5 3 3 3 3
2 3 2 1 5 2 4 5 3 2 5 2
3 3 3 3 2 2 2 2 2 5 5
1 1 1 4 4 4 4 4 2
FIFO
2 2 2* 2* 2* 2* 2* 2* 2* 2* 2* 2*
3 3 3 5 5 5 5* 5 5 5* 5*
1 1 1 4 4 3 3 3 3
CLOCK
• Fetch policy: Process pages can be brought in on demand, or a pre-paging policy can be used; the latter clusters the input activity by bringing in a number of pages at once
• Placement policy: With a pure segmentation system, an incoming segment must be fit into an available space in memory
• Replacement policy: When memory is full, a decision must be made as to which page or pages are to be replaced
• Resident set management: The operating system must decide how much main memory to allocate to a particular process when that process is swapped in. This can be a static allocation made at process creation time, or it can change dynamically.
• Cleaning policy: Modified process pages can be written out at the time or replacement, or a pre-cleaning policy can be used; the latter clusters the output activity by writing out a number of pages at once
• Load control: Load control is concerned with determining the number of processes that will be resident in main memory at any given time