560 likes | 831 Views
Module 9: Memory Management. Updated 11/7/03. Distinct from Virtual memory: Chapter 10 Background Logical versus Physical Address Space Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging Annotations by instructor are in blue. Background.
E N D
Module 9: Memory Management Updated 11/7/03 • Distinct from Virtual memory: Chapter 10 • Background • Logical versus Physical Address Space • Swapping • Contiguous Allocation • Paging • Segmentation • Segmentation with PagingAnnotations by instructor are in blue Applied Operating System Concepts
Background • Program must be brought into memory and loaded as a process for it to be executed. • Input queue – collection of processes on the disk that are waiting to be brought into memory for execution. • User programs go through several steps before being executed. Applied Operating System Concepts
Binding of Instructions and Data to Memory See notes by Guydosh: Address “Binding, Loading and Linking” on this topic - High level summary below: Address binding of instructions and data to memory addresses can happen at three different stages. • Compile time: If memory location known a priori, 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. – if swapped out must be re-loaded to same location from original location • Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers). - dynamic address translation Applied Operating System Concepts
Multistep Processing of a User Program Applied Operating System Concepts
Dynamic Loading and Linking • “DLL’s” - See notes by Guydosh: Address “Binding, Loading and Linking” • Routine is not loaded until it is called at runtime “application” module is built up at run time. • Obviously the newly loaded module must be also linked in real time to resolve addresses relative to the original module referencing it. • Better memory-space utilization; unused routine is never loaded. • Useful when large amounts of code are needed to handle infrequently occurring cases. • Logical to real address binding is at runtime. • No special support from the operating system is required implemented through program design - loaded modules are in re-locatable format? Applied Operating System Concepts
Dynamic Linking to common shared libraries • “DLL’s” - See notes by Guydosh: Address “Binding, Loading and Linking” • Rather than building a “private module” by loading at runtime, we link to a common “library” of common functions. The library may or may not be in memory, if it is not at the time called, then load it. In all cases dynamically link (at run time) to it via “stubs”. • Linking postponed until execution time. • Small piece of code, stub, used to locate the appropriate memory-resident library routine. • Stub replaces itself with the address of the routine, and executes the routine. • Operating system needed to check if routine is in processes’ memory address. Applied Operating System Concepts
Overlays • Keep in memory only those instructions and data that are needed at any given time. • Needed when process is larger than amount of memory allocated to it. • Implemented by user, no special support needed from operating system, programming design of overlay structure is complex Applied Operating System Concepts
Logical vs. Physical Address Space • Will assume run-time binding of logical to physical • The concept of a logical address space that is bound to a separate physicaladdress 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 (not MMU). • Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme. • MMU sees logical addresses: hardware assist in converting LA to PA. Applied Operating System Concepts
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 real physical addresses.See Figure 9.5 Applied Operating System Concepts
Dynamic relocation using a relocation register Figure 9.5 - a function of the MMU (should include error/range checking also. Applied Operating System Concepts
Swapping • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution –remember, suspend state frees up memory when all processes block for I/O – maximizes degree of multiprogramming. • Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images. • 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. • Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped. • Modified versions of swapping are found on many systems, i.e., UNIX and Microsoft Windows. • Need run-time address binding to allow re-locatability of swapped processes. Applied Operating System Concepts
Schematic View of Swapping Applied Operating System Concepts
Contiguous Allocation • Contiguous means non-paged – keep process together- each process is kept in a single contiguous section of memory. • Main memory usually into two partitions: • Resident operating system, usually held in low memory with interrupt vector. • User processes then held in high memory. • Single-partition allocation – see later slide • Multiple processes in a single partition in user space (see dynamic contiguous allocation below) • Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data. • Relocation register contains value of smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register. Applied Operating System Concepts
Contiguous Allocation (cont.) • Allocation could be within a series of fixed size partitions (below), or “dynamically” within a single user partition (as above) • Multi- partition allocation (fixed or variable size partitions) -“over-allocation”: • Fixed size Partitions: User space is partitioned into a series of fixed partitions: • Variable size Partitions: User space is partitioned into a series of partitions of varying sizes: • equal vs. variable size blocks tradeoffs • Internal fragmentation – within a partition owned by a process - due to “over-allocation” • Allocation unit results in over-allocationif process too large, use overlaysEarly example: IBM’s MFT operating systemRef: See Stallings fig 7.2 and 7.3 Applied Operating System Concepts
Contiguous Allocation (cont.) Stallings, fig. 7.2 Examples of partitioning Of a 64 MB memory using constant sized and variable sized partitions Applied Operating System Concepts
Contiguous Allocation (cont.) Stallings, fig. 7.3 Examples of memory assignment for multi partitioning with variable or constant partitions Applied Operating System Concepts
Contiguous Allocation (cont.) • Single user partition using “dynamic” (term from book) contiguous allocationMemory is not “carved” up into a series of partitionsrather we have a single partition where processes of any size may be allocated. Could start anywhere.Problem is external fragmentationEarly example: IBM’s MVT operating system • Need Relocation hardware scheme in any Contiguous scheme: multiple or single to convert logical to physical addresses at run time, since we do not know where the process will be swapped to Applied Operating System Concepts
Contiguous Allocation (Cont.) • Contiguous Allocation - within a single user partition • 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 about:a) allocated partitions b) free partitions (hole) Examples of alloclated and un-allocated (holes) memory in single partition: OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 10 process 2 process 2 process 2 process 2 Applied Operating System Concepts
Fragmentation Stallings, fig. 7.4 Examples of external fragmentation due to “dynamic” allocation in a single user partition Applied Operating System Concepts
Another Example of External Fragmentation From an “alternate” Silberschatz book Applied Operating System Concepts
Fragmentation • External fragmentation – total memory space exists to satisfy a request, but it is not contiguous. • Internal fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. • Reduce external fragmentation 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. • I/O problem • Latch job in memory while it is involved in I/O. • Do I/O only into OS buffers. • Reference: See Stallings: figs 7.4, 7.5 Applied Operating System Concepts
Dynamic Storage-Allocation Problem How to satisfy a request of size n from a list of free holes. • 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 entier list. Produces the largest leftover hole. • Next fit (see Stallings) • See Stallings fig 7.5 First-fit and best-fit better than worst-fit in terms of speed and storage utilization. Applied Operating System Concepts
Fragmentation Stallings, fig. 7.5 Examples of dynamic allocation algorithms Allocation of 16 mb block Applied Operating System Concepts
Paging – Non-contiguous Schemes • Logical address space of a process can be contiguous;but physical addresses are noncontiguous;Process is allocated physical memory whenever the latter is available.Requires Run-time address binding • Divide physical memory into fixed-sized blocks called frames (of relatively small size - size is power of 2 - WHY? , typically fbetween 512 bytes and 8192 bytes). • Divide logical memory into blocks of same size called pages. • Keep track of all free frames. • To run a program of size n pages, need to find n free frames and load program. • Set up a page table to translate logical to physical addresses. • Internal fragmentation –a but this is minor compared to external Fragmentation. Reminder: just because paging used, does not necessarily mean virtual memory Applied Operating System Concepts
Address Translation Scheme • Address generated by CPU is divided into: • Page number(p) – used as an index into a pagetable which contains base address of each page in physical memory. • Page offset(d) – combined with base address to define the physical memory address that is sent to the memory unit. • See Stallings fig. 7.9, page 318 Applied Operating System Concepts
Page Assignment Assignment of process Pages to free frames Stallings, fig 7.9 Page/frame tables Stallings, fig 7.9 below Key idea: each process has its own page table (f) Applied Operating System Concepts
Address Translation Architecture Page table is directly indexed - not searched Applied Operating System Concepts
Logical addresses Same logical addr as in partitioning case = 0x1DE (10 bits) 1K page ==> low 10 bits are the offset Logical addressing with Partitioning and paging Stallings fig 7.11 For Partitioning: Logical address is an offset into the process For paging: LA specifies a page in the process and offset into this page Assume the Logical Address 0x05DE points to a byte boundry in a memory Applied Operating System Concepts
Logical to physical address translation A “page” in physical memory is called a “frame” = 0x05DE= (0x1, 0x1DE) LA ordered pair Logical to physical address translation Stallings, fig. 7.12 Page number = 0x01 Frame number = 0x06 Frame offset = 0x1DE Physical page “frames” could be “scattered” anywhere in memory (on page boundries) via address translation. Frame numbers are determided by memory allocation algorthms … use of free list etc. Applied Operating System Concepts
Paging Example Stored in memory See Stallings , fig. 7.10, pg. 319 Also examples in fig. 7.11 – 7.12 See also fig 9.6., p. 288 (silberschatz) for example on address translation Applied Operating System Concepts
Implementation of Page Table • Page table is kept in main memory. • Page-tablebase 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.Doubles memory access time! • The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative registers or translation look-aside buffers(TLBs) • See fig 9.10, page 294 ==> Applied Operating System Concepts
TLB: A Cache for the Page Table “paging” the page table Silber. Fig. 9.10 TLB is logical subset of PT on TLB miss load TLB from PT, while immediately translating to real. Applied Operating System Concepts
Associative Register (TLB) • Associative registers – parallel search • “Broadside parallel hardware search for Page number - use multiplexers and compare circuits. Address translation (A´, A´´) • If A´ is in associative register, get frame # out. • Otherwise get frame # from page table in memory Page # Frame # Applied Operating System Concepts
Effective Access Time • Associative Lookup = time unit • Assume memory cycle time is 1 microsecond • Hit ration – percentage of times that a page number is found in the associative registers; ration related to number of associative registers. • Hit ratio = • Effective Access Time (EAT) = (tTLB + tMEM_DATA) + (1- )(tTLB + tMEM_PT + tMEM_DATA) EAT = (1 + ) + (2 + )(1 – ) = 2 + – Applied Operating System Concepts
Memory Protection • Memory protection implemented by associating protection bits for each frame – normally kept in the page table. • Ex: “RW”, “RO”, or execute only bits … as the permission bits in UNIX • 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. • “invalid” indicates that the page is not in the process’ logical address space. • Retaining invalid page entries is wasteful ==> can use HW assist : Keep only valid entries - Page table length register (PTLR) to indicate size of PT. Every logical address must be < PTLR to be valid. • See fig. 9.11, page 296 Applied Operating System Concepts
Memory Protection 14 bit Logical address, page size is 2K , 11 bits for page offset, 3 bits for page number, Logical address space is 214 = 16384 bytes = 23 = 8 pages Note: although page 5 is marked valid, addresses beyond 10468 (last byte in process) are illegal – need range checking as in fig 9.5 Note: “pages” 6 & 7 not resident in memory, because they are invalid Has page entry for all pages in logical memory Applied Operating System Concepts
Two-Level Page-Table Scheme One solution to large PT problem For a 32 bit address, and a 4k page, the non-paged PT would have 1 meg entries - at 4 bytes /entry, the PT would take up 4MB! Solution: “demand” page in only those blocks of thepage table that are needed. “Page the page table” Outer “pinned” in memory ==> See also 3 level PT, p. 299 Applied Operating System Concepts
Two-Level Paging Example • A logical address (on 32-bit machine with 4K page size) is divided into: • a page number consisting of 20 bits. • a page offset consisting of 12 bits. • Since the page table is paged, the page number is further divided into: • a 10-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 offset page number pi p2 d 10 10 12 Applied Operating System Concepts
Address-Translation Scheme • Address-translation scheme for a two-level 32-bit paging architecture Applied Operating System Concepts
Multilevel Paging and Performance Four level PT (p. 280 bottom), Motorola 68000, p. 281: • Since each level is stored as a separate table in memory, covering a logical address to a physical one may take four memory accesses. • Even though time needed for one memory access is quintupled, caching permits performance to remain reasonable. • Cache (TLB) hit rate of 98 percent yields: effective access time = 0.98 x 120 + 0.02 x 520 = 128 nanoseconds.Where TLB access is 20 s, memory access is 100 s 4 accesses to get physical address plus 1 to get data.Thus 520 = 20 + 4x100 + 100 which is only a 28 percent slowdown in memory access time. Applied Operating System Concepts
Inverted Page Table • Another solution to large PT problem • One entry for each real page of memory.… one inverted PT for all processes • Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page, ie., PID, Frame NumberFrame # is index into table of desired entry • Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs. • Use hash table to limit the search to one — or at most a few — page-table entries.See also hashed page tables, section 9.4.4.2 … improves performance - hash table has entry only for “used” page numbers, not all. Applied Operating System Concepts
Inverted Page Table Architecture PT is now searched, and the index of the “hit” is the frame adress. Search inverted page table using a hash search … has the flavor of associate memory - but hash used instead of hardware TLB Applied Operating System Concepts
Shared Pages • Remember: each process has its own page table • Can implement shared “RO” data using shared pages – saves space. • 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. • Private code and data • Each process keeps a separate copy of the code and data. • The pages for the private code and data can appear anywhere in the logical address space. Applied Operating System Concepts
Shared Pages Example Applied Operating System Concepts
Segmentation • Memory-management scheme that supports user view of memory. • Similar to a paing scheme in which aht page size is variable - segments “build” around functional units. • A program is a collection of segments. A segment is a logical unit such as: main program, procedure, function, local variables, global variables, common block, stack, symbol table, arrays Applied Operating System Concepts
1 4 2 3 Logical View of Segmentation 1 2 3 4 user space physical memory space Applied Operating System Concepts
Segmentation Architecture • Logical address consists of a two tuple: <segment-number, offset>, • Segment table– (an array of base and limit registers) maps two-dimensional physical addresses; each table entry has: • base – contains the starting physical address where the segments reside in memory. • limit – specifies the length of the segment. • Segment-table base register (STBR) points to the segment table’s location in memory. • Segment-table length register (STLR) indicates number of segments used by a program (number of entries in table); segment number s is legal if s < STLR. Applied Operating System Concepts
Segmentation Architecture (Cont.) • Relocation. • Dynamic (at run time) • by segment table • Sharing. • shared segments • same segment number • Allocation. • suffers from external fragmentation - as did contifuous allocation in a single partition. • Use various algorithms to deal with external fragmenttion such as: first fit/best fit Applied Operating System Concepts
Segmentation Hardware (fig. 9.18) Where segment starts PA = base+d d limit d > limit:: error Applied Operating System Concepts
Segmentation Architecture (Cont.) • Protection. With each entry in segment table associate: • validation bit = 0 illegal segment • read/write/execute privileges • Protection bits associated with segments; code sharing occurs at segment level. • Since segments vary in length, memory allocation is a “dynamic” storage-allocation problem - contiguous in a single partition. • A segmentation example is shown in the following diagram Applied Operating System Concepts