1 / 62

Memory Management

Memory Management. Background. Program must be brought into memory and placed within 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.

dylan-lane
Download Presentation

Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Management

  2. Background • Program must be brought into memory and placed within 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.

  3. Base and Limit Register • Memory space divided into partitions. • One partition to OS and others for different user process. • Memory protection required so that an attempt by user program to access OS memory or other user memory. • Base register holds smallest physical memory address and limit register specifies size of range.

  4. Address Protection

  5. Binding of Instructions and Data to Memory Address binding of instructions and data to memory addresses canhappen 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. • 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).

  6. Logical vs. Physical Address Space • 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. • 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. • Logical address space is the set of all logical addresses generated by a program • Physical address space is the set of all physical addresses generated by a program

  7. Memory Management Unit • Hardware device that maps virtual to physical address. • Base register now called relocation register • 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.

  8. Dynamic Relocation using Relocation Register

  9. Dynamic Binding • Routine is not loaded until it is called • Better memory-space utilization; unused routine is never loaded. • Useful when large amounts of code are needed to handle infrequently occurring cases. • No special support from the operating system is required implemented through program design.

  10. 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 • Programmer defines two or more overlays segments within the program such that no two overlays segments need to be in memory at same time. • Os swaps overlays segments as required, reducing amount of physical memory required.

  11. Swapping • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution. • 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. • System maintains a ready queue of ready-to-run processes which have memory images on disk.

  12. Swapping View

  13. Continuous Allocations • 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 • 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.

  14. Hardware support for limit and relocation register

  15. Continuous Memory Allocation • 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 about:a) allocated partitions b) free partitions (hole) 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

  16. Dynamic 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.

  17. 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.

  18. Paging • Physical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available • Divide physical memory into fixed-sized blocks called frames • Size is power of 2, between 512 bytes and 16 Mbytes • 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 • Backing store likewise split into pages • Still have Internal fragmentation

  19. Address Translation Scheme • Address generated by CPU is divided into: • Page number (p)– used as an index into a page table 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 • For given logical address space 2m and page size2n

  20. Paging Hardware

  21. Paging Model of Logical and Physical Memory

  22. Paging Example • For logical address n=2 , m=4. • Page size = 4 bytes and physical memory = 32 byte (8 pages or frame) • Page 0 is in frame 5 from page table. So logical address 3(page 0, offset 3) maps to physical address 23[= (5X4) +3]

  23. Paging(Cont..) • Calculating internal fragmentation • Page size = 2,048 bytes • Process size = 72,766 bytes • 35 pages + 1,086 bytes (36 frame) • Internal fragmentation of 2,048 - 1,086 = 962 bytes • Worst case fragmentation = n frame +1 byte • On average fragmentation = 1 / 2 frame size • So small frame sizes desirable? • But each page table entry takes memory to track • Page sizes growing over time • Solaris supports two page sizes – 8 KB and 4 MB • Process view and physical memory now very different • By implementation process can only access its own memory

  24. Free Frame and Frame Table

  25. Implementation of Page Table • Page table is kept in main memory • Page-table base register (PTBR)points to the page table • Page-table length register (PTLR)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 fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs) • Some TLBs storeaddress-space identifiers (ASIDs) in each TLB entry – uniquely identifies each process to provide address-space protection for that process • Otherwise need to flush at every context switch • TLBs typically small (64 to 1,024 entries) • On a TLB miss, value is loaded into the TLB for faster access next time • Replacement policies must be considered • Some entries can be wired down for permanent fast access

  26. TLB(Associate memory) • Associative memory – parallel search • Address translation (p, d) • If p is in associative register, get frame # out • Otherwise get frame # from page table in memory

  27. Paging Hardware with TLB

  28. Effective Access Time • Associative Lookup =  time unit • Can be < 10% of memory access time • Hit ratio =  • Hit ratio – percentage of times that a page number is found in the associative registers; ratio related to number of associative registers • Consider  = 80%,  = 20ns for TLB search, 100ns for memory access • Effective Access Time(EAT) EAT = (1 + )  + (2 + )(1 – ) = 2 +  –  • Consider  = 80%,  = 20ns for TLB search, 100ns for memory access and page table search each. • EAT = 0.80 x 120 + 0.20 x 220 = 140ns • Consider slower memory but better hit ratio ->  = 98%,  = 20ns for TLB search, 140ns for memory access • EAT = 0.98 x 160 + 0.02 x 300 = 162.8ns

  29. Memory Protection • Memory protection implemented by associating protection bit with each frame to indicate if read-only or read-write access is allowed • Can also add more bits to indicate page execute-only, and so on • Valid-invalidbit 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 • Or use PTLR • Any violations result in a trap to the kernel

  30. Valid(v) or Invalid(i) bit in a Page table

  31. Segmentation • Memory-management scheme that supports user view of memory. • Divides program into blocks called segments each of which is allocated memory independently. • Dividing program into segment lies with user (compiler). • Segments are of variable size. • 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

  32. User View of a Program

  33. Logical View of Segmentation

  34. Segmentation Architecture • Logical address consists of a two tuple: <segment-number, offset>, • Segment table – 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; segment number s is legal if s < STLR.

  35. Segmentation Architecture (Cont..) • Protection • With each entry in segment table associate: • validation bit = 0  illegal segment • read/write/execute privileges • Since segments vary in length, memory allocation is a dynamic storage-allocation problem. • Shared segment may be used by process executing different programs but using the same subroutine library. • Allocation. • first fit/best fit • external fragmentation

  36. Segmentation Hardware

  37. Segmentation Example

  38. Virtual Memory • Virtual memory – separation of user logical memory from physical memory. • Only part of the program needs to be in memory for execution. • Logical address space can therefore be much larger than physical address space. • Need to allow pages to be swapped in and out. • Virtual memory can be implemented via: • Demand paging • Demand segmentation

  39. Demand Paging • Could bring entire process into memory at load time • Or bring a page into memory only when it is needed • Less I/O needed, no unnecessary I/O • Less memory needed • Faster response • More users • Page is needed  reference to it • invalid reference  abort • not-in-memory  bring to memory • Lazy swapper– never swaps a page into memory unless that page will be needed • Swapper that deals with individual pages is a pager

  40. Transfer of a paged memory to Contiguous disk space

  41. Valid Invalid Bit • With each page table entry a valid–invalid bit is associated(v  in-memory, i not-in-memory) • Initially valid–invalid but is set to i on all entries. • Example of a page table snapshot. • During address translation, if valid–invalid bit in page table entry is i  page fault.

  42. Page Table when some Pages are not in Memory

  43. Aspect of Demand Paging • Extreme case – start process with no pages in memory • OS sets instruction pointer to first instruction of process, non-memory-resident -> page fault • And for every other process pages on first access • Pure demand paging: Never bring page until required • Actually, a given instruction could access multiple pages -> multiple page faults resulting in degraded system performance. • Pain decreased because of locality of reference • Hardware support needed for demand paging • Page table with valid / invalid bit • Secondary memory (swap device with swap space) • Instruction restart

  44. Steps in Handling Page Fault

  45. Performance of Demand Paging • Page Fault Rate 0  p  1.0 (probability of page fault) • if p = 0 no page faults • if p = 1, every reference is a fault • Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)

  46. Example of Demand Paging • Memory access time = 200 nanoseconds • Average page-fault service time = 8 milliseconds • EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p) x 200 + p x 8,000,000 = 200 + p x 7,999,800 • If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a slowdown by a factor of 40!! • If want performance degradation < 10 percent • 220 > 200 + 7,999,800 x p20 > 7,999,800 x p • p < .0000025 • < one page fault in every 400,000 memory accesses

  47. Page and Frame Replacement Algorithm • Frame-allocation algorithm determines • How many frames to give each process • Which frames to replace • Page-replacement algorithm • Want lowest page-fault rate on both first access and re-access • Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string • String is just page numbers, not full addresses • Repeated access to the same page does not cause a page fault • In all our examples, the reference string is 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

  48. Graph of Page Fault vs. Number of Frames • Page fault should decrease as number of frame increases. But not always true

  49. FIFO Page Replacement Algorithm • Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 • First in first out : Oldest page is chosen to replace • 3 frames (3 pages can be in memory at a time per process) • Use FIFO queue

  50. Belady’s Anomaly

More Related