Lecture 11: Memory Management
Example memory configuration before and after allocation of a 16KB block First Fit (16K) 22K 6K Best Fit (16K) 18K 2K Last allocated block (14K) 8K 8K 6K 6K 14K 14K Next Fit (16K) 36K 20K
Hardware support for relocation Relative Address Process Control Block Base register Program Adder Absolute Address Data Bounds register Comparator Stack Interrupt to Operating System
Logical addresses Logical Address: Page# = 1, Offset = 478 Logical Address: Segment# = 1, Offset = 752 Relative Address = 1502 0000010111011110 000001 0111011110 0001 001011110000 Page 0 Segment 0 750 bytes 478 752 Page 1 User process 2700 bytes Segment 1 1950 bytes Page 2
Paging 6-Bit page 10-Bit offset 000001 0111011110 Process Page Table 000101 000110 011001 1 2 000101 0111011110 16-Bit physical address
Segment 4-Bit segment 10-Bit offset 0001 001011110000 Process Segment Table Length Base 1 001011101110 0000010000000000 011110011110 0010000000100000 + 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 16-Bit physical address
Memory Management Techniques 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 Paging 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 P d F d P CPU Physical Memory Logical Address Physical Page table
Paging Hardware CPU P d Physical Memory F d P Logical Address Address Page table P d F d P Logical Address Address TLB hit TLB miss
Analysis Definition Performance 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 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
trap: addressing error CPU Physical Memory Segment table S d S + < limit Base Yes No trap: addressing error
Virtual Memory
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 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 number Page table Process Main Memory Register Virtual Address Page Frame Offset
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
NO LECTURE ON THURSDAY 20/02/2003 – SCIENCE DAY LECTURE ON FRIDAY 11-12 INSTEAD
Lecture 12: Memory Management
PAGING ONLY Page Number Offset P M Other Control Bits Frame Number Virtual Address Page Number Offset Page Table Entry P M Other Control Bits Frame Number
SEGMENTATION ONLY Segment Number Offset Virtual Address Segment Number Offset Segment Table Entry P M Other Control Bits Length Segment Base
COMBINED SEGMENTATION & PAGING Virtual Address Segment Number Page number Offset Segment Table Entry P M Other Control Bits Length Segment Base Page Table Entry P M Other Control Bits Frame Number
Address translation in a segmentation system Virtual Address Real Address + Segment Number S# Offset d Base + d d Register Seg. Table Ptr Segment length + S Length Base Segment Table Main Memory
Address translation in a segmentation/paging system Virtual Address Segment # Page # Offset Frame # Offset d Register Seg. Table Ptr Segment length P + + S Page Table Segment Table Main Memory
Page size Page Fault Rate Page Fault Rate P Number of page frames allocated Page size
Protection relationships between segments Main Memory 20 K All accesses not allowed Dispatcher 35 K Branch instruction not allowed 50 K Reference to data (not allowed) Process A Reference to data (allowed) 80 K 90 K Process B 140 K Process C 190 K
Replacement Policy Memory management concepts: 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 4 3 1 5 Optimal 2 3 5 1 4 LRU
Example (ctd) 2 5 3 1 4 FIFO 2 2* 3 5 5* 1 4 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
WEEK 8: EXAMPLES OF MEMORY MANAGEMENT System/370 and MVS Windows NT Unix System V LINUX