Lecture 3 Main Memory Operating System Concepts Essentials































- Slides: 31

Lecture 3: Main Memory Operating System Concepts Essentials – 2 nd Edition Silberschatz, Galvin and Gagne © 2013

Chapter 7: Memory Management n Background n Swapping n Contiguous Memory Allocation n Segmentation n Paging n Structure of the Page Table Operating System Concepts Essentials – 2 nd Edition 7. 2 Silberschatz, Galvin and Gagne © 2013

Contiguous Allocation n Main memory must support both OS and user processes n Limited resource, must allocate efficiently n Main memory usually into two partitions: l Resident operating system l User processes l Each process contained in single contiguous section of memory Operating System Concepts Essentials – 2 nd Edition 7. 3 Silberschatz, Galvin and Gagne © 2013

Contiguous Allocation (Cont. ) n Relocation registers used to protect user processes from each other, and from changing operating-system code and data l Base register contains value of smallest physical address l Limit register contains range of logical addresses – each logical address must be less than the limit register Operating System Concepts Essentials – 2 nd Edition 7. 4 Silberschatz, Galvin and Gagne © 2013

Hardware Support for Relocation and Limit Registers Operating System Concepts Essentials – 2 nd Edition 7. 5 Silberschatz, Galvin and Gagne © 2013

Multiple-partition allocation n Multiple-partition allocation l Degree of multiprogramming limited by number of partitions l Variable-partition sizes for efficiency (sized to a given process’ needs) l Hole – block of available memory; holes of various size are scattered throughout memory l When a process arrives, it is allocated memory from a hole large enough to accommodate it l Process exiting frees its partition, adjacent free partitions combined l Operating system maintains information about: a) allocated partitions b) free partitions (hole) Operating System Concepts Essentials – 2 nd Edition 7. 6 Silberschatz, Galvin and Gagne © 2013

Dynamic Storage-Allocation Problem How to satisfy a request of size n from a list of free holes? n First-fit: Allocate the first hole that is big enough n Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size l Produces the smallest leftover hole n Worst-fit: Allocate the largest hole; must also search entire list l Produces the largest leftover hole First-fit and best-fit better than worst-fit in terms of speed and storage utilization Operating System Concepts Essentials – 2 nd Edition 7. 7 Silberschatz, Galvin and Gagne © 2013

Fragmentation n External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous n Internal Fragmentation – allocated memory may be slightly larger than requested memory. Operating System Concepts Essentials – 2 nd Edition 7. 8 Silberschatz, Galvin and Gagne © 2013

Segmentation n Memory segmentation is the division of a computer's primary memory into segments or sections. n Memory-management scheme that supports user view of memory n A program is a collection of segments l A segment is a logical unit such as: main program procedure function method object local variables, global variables common block stack symbol table arrays Operating System Concepts Essentials – 2 nd Edition 7. 9 Silberschatz, Galvin and Gagne © 2013

User’s View of a Program Operating System Concepts Essentials – 2 nd Edition 7. 10 Silberschatz, Galvin and Gagne © 2013

Logical View of Segmentation 1 4 1 2 3 2 4 3 user space Operating System Concepts Essentials – 2 nd Edition physical memory space 7. 11 Silberschatz, Galvin and Gagne © 2013

Segmentation Architecture n Logical address consists of a two tuple: <segment-number, offset>, n Segment table – maps two-dimensional physical addresses; each table entry has: l base – contains the starting physical address where the segments reside in memory l limit – specifies the length of the segment n Segment-table base register (STBR) points to the segment table’s location in memory n Segment-table length register (STLR) indicates number of segments used by a program; Operating System Concepts Essentials – 2 nd Edition 7. 12 Silberschatz, Galvin and Gagne © 2013

Segmentation Hardware Operating System Concepts Essentials – 2 nd Edition 7. 13 Silberschatz, Galvin and Gagne © 2013

Paging n Physical address space of a process can be noncontiguous; process is allocated physical memory. n Divide physical memory into fixed-sized blocks called frames l Size is power of 2, between 512 bytes and 16 Mbytes n Divide logical memory into blocks of same size called pages n Keep track of all free frames n To run a program of size N pages, need to find N free frames and load program n Set up a page table to translate logical to physical addresses Operating System Concepts Essentials – 2 nd Edition 7. 14 Silberschatz, Galvin and Gagne © 2013

Address Translation Scheme n Address generated by CPU is divided into: l Page number (p) – used as an index into a page table which contains base address of each page in physical memory l Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit l For given logical address space 2 m and page size 2 n Operating System Concepts Essentials – 2 nd Edition 7. 15 Silberschatz, Galvin and Gagne © 2013

Paging Hardware Operating System Concepts Essentials – 2 nd Edition 7. 16 Silberschatz, Galvin and Gagne © 2013

Paging Model of Logical and Physical Memory Operating System Concepts Essentials – 2 nd Edition 7. 17 Silberschatz, Galvin and Gagne © 2013

Paging Example n=2 and m=4 32 -byte memory and 4 -byte pages Operating System Concepts Essentials – 2 nd Edition 7. 18 Silberschatz, Galvin and Gagne © 2013

Paging (Cont. ) n Calculating internal fragmentation l If Page size = 2, 048 bytes and Process size = 72, 766 bytes. How many pages do you need ? Ø 35 pages + 1, 086 bytes Operating System Concepts Essentials – 2 nd Edition 7. 19 Silberschatz, Galvin and Gagne © 2013

Free Frames Before allocation Operating System Concepts Essentials – 2 nd Edition After allocation 7. 20 Silberschatz, Galvin and Gagne © 2013

Memory Protection n Memory protection implemented by associating protection bit with each frame to indicate if read-only or read-write access is allowed n Valid-invalid bit attached to each entry in the page table: l “valid” indicates that the associated page is in the process’ logical address space, and is thus a legal page l “invalid” indicates that the page is not in the process’ logical address space n Any violations result in a trap to the kernel Operating System Concepts Essentials – 2 nd Edition 7. 21 Silberschatz, Galvin and Gagne © 2013

Valid (v) or Invalid (i) Bit In A Page Table Operating System Concepts Essentials – 2 nd Edition 7. 22 Silberschatz, Galvin and Gagne © 2013

Shared Pages n Shared code l One copy of read-only (reentrant) code shared among processes (i. e. , text editors, compilers, window systems) n Private code and data l Each process keeps a separate copy of the code and data Operating System Concepts Essentials – 2 nd Edition 7. 23 Silberschatz, Galvin and Gagne © 2013

Shared Pages Example Operating System Concepts Essentials – 2 nd Edition 7. 24 Silberschatz, Galvin and Gagne © 2013

Structure of the Page Table n In this section, we explore some of the most common techniques for structuring the page table. n Most modern computer systems support a large logical address space (232 to 264). n Clearly, we would not want to allocate the page table contiguously in main memory. One simple solution to this problem is to divide the page table into smaller pieces. n We can accomplish this division in several ways: l Hierarchical Paging l Hashed Page Tables l Inverted Page Tables Operating System Concepts Essentials – 2 nd Edition 7. 25 Silberschatz, Galvin and Gagne © 2013

Hierarchical Page Tables n Break up the logical address space into multiple page tables n A simple technique is a two-level page table n We then page the page table Operating System Concepts Essentials – 2 nd Edition 7. 26 Silberschatz, Galvin and Gagne © 2013

Two-Level Page-Table Scheme Operating System Concepts Essentials – 2 nd Edition 7. 27 Silberschatz, Galvin and Gagne © 2013

Hashed Page Tables n Common in address spaces > 32 bits n The virtual page number is hashed into a page table l This page table contains a chain of elements hashing to the same location n Each element contains (1) the virtual page number (2) the value of the mapped page frame (3) a pointer to the next element n Virtual page numbers are compared in this chain searching for a match l If a match is found, the corresponding physical frame is extracted Operating System Concepts Essentials – 2 nd Edition 7. 28 Silberschatz, Galvin and Gagne © 2013

Hashed Page Table Operating System Concepts Essentials – 2 nd Edition 7. 29 Silberschatz, Galvin and Gagne © 2013

Inverted Page Table n Rather than each process having a page table and keeping track of all possible logical pages, track all physical pages n One entry for each real page of memory n Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page n Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs Operating System Concepts Essentials – 2 nd Edition 7. 30 Silberschatz, Galvin and Gagne © 2013

Inverted Page Table Architecture Operating System Concepts Essentials – 2 nd Edition 7. 31 Silberschatz, Galvin and Gagne © 2013