560 likes | 790 Views
Chapter 9. Memory Management. Logical versus Physical Address Space Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging. Multistep Processing of a User Program. User programs go through several steps before being run on memory.
E N D
Chapter 9. Memory Management • Logical versus Physical Address Space • Swapping • Contiguous Allocation • Paging • Segmentation • Segmentation with Paging Applied Operating System Concepts
Multistep Processing of a User Program User programs go through several steps before being run on memory. Applied Operating System Concepts
Binding Symbolic Identifier – Binary Number • Compile time binding • 주소가 이때 알려져 있어야 함 • absolute (최종, 절대주소)code generated • mustrecompile code if starting location changes. • Load time binding • Loader 책임 하에 address 부여? (현재 빈 공간은?) • Compiler 는 relocatable code를 만들었어야 • (어느 공간이라도 올라가야 – given base address) • Execution time binding • 수행이 시작된 이후로도 프로세스를 메모리에서 계속 옮김? • CPU 가 주소를 생성할 때마다 binding 을 점검 (address mapping table) • binding run time. • need hardware support (e.g., base and limit registers, MMU). Applied Operating System Concepts
memory CPU가 준 주소를 MMU 가 변환 Add 10, 20 10: 100 20: 330 Jump 30 30: exit Add A, B A: 100 B: 330 Jump C C: exit 10 CPU가 issue 하는 번지 Mapping Memory 내번지 500 Binary on Disk Source Reference 될때 load Any Place Applied Operating System Concepts
Address Mapping Table CPU가 제시한 주소 : 실제 메모리 내 주소 logical physical Memory 500 번지 0 번지 0 CPU 500: 500 번지 20000 번지 Applied Operating System Concepts
Logical vs. Physical Address Space • Logical address • address generated by the CPU • also referred to as “virtual address”. • Physical address • address seen by the memory unit. Applied Operating System Concepts
Memory-Management Unit (MMU) • MMU (Memory-Management Unit) • is a Hardware device that maps • virtual address 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 (from CPU). • The user program • deals with logical addresses • never sees the realphysical addresses. Applied Operating System Concepts
Dynamic relocation using a relocation register Applied Operating System Concepts
Some Terminologies • Dynamic Loading • Dynamic Linking • Overlays • Swapping Applied Operating System Concepts
Dynamic Loading • Routine is not loaded until it is called • Better memory-space utilization • 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. • Dynamic Linking이 없이는 매번 같은 곳으로 올라와야 (다음 페이지) • Loading – occupy memory space Applied Operating System Concepts
Dynamic Linking • 올라올 때마다 매번 동일한 주소가 비어있어야? -- no! • 아무 주소로도 올라올 수 있어야… (to where, from where, is where) • 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. • Linking – Symbolic address v.s. Physical address Applied Operating System Concepts
Overlays • Keep in memory only those part when actually needed. • Useful when process is larger than memory. • Coded by user – “Manual Overlay”. Applied Operating System Concepts
Overlays for a Two-Pass Assembler Applied Operating System Concepts
Swapping • Swapping • A process can be swappedtemporarily out of memory to a backing store • and then brought back into memory sometime later. • Backing store • disk – fast, large enough to accommodate all images for all users • Roll out, roll in • lower-priority process is swapped out • so higher-priority process can be loaded and executed. • swapping variant used for priority-based scheduling algorithms; • Major part of swap time is transfer time; • total transfer time is proportional to the amount of memory swapped. Applied Operating System Concepts
Schematic View of Swapping hwp web Applied Operating System Concepts
Contiguous Allocation (한 덩어리로load) • 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 • to protect user processes from each other, and OS. • Relocation register • contains value of smallest physical address. • Limit register • contains range of logical addresses – • each logical address bound by limit register. OS user process Applied Operating System Concepts
Hardware Support for Relocation and Limit Registers Applied Operating System Concepts
Contiguous Allocation (Cont.) • 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 hole process 9 process 9 partitions process 8 hole process 10 hole process 2 process 2 process 2 process 2 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 many small leftover holes. • Worst-fit • Allocate the largest hole; • must also search entire list. • Produces the largest leftover hole. • Buddy system • 2의 배수 First-fit and best-fit better than worst-fit in terms of speed and storage utilization. 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 bycompaction • 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, instead • Graceful degradation? (Very expensive especially at peak load?) • . Applied Operating System Concepts
Paging • Entire program image resides on disk • Divide (memory, disk) into fixed size – • (page frame – ‘틀’ -- in memory, page -- ‘내용’ -- in disk) • When the program starts, just load 1st page only • Rest of the pages are loaded in memory on-demand page mapping table • Pages can be placed anywhere in memory • A particular page X of the program can be either • already loaded in memory page-frame Y • or never been loaded before, it is in disk Whenever CPU presents an address, MMU looks up page mapping table X Y disk address Applied Operating System Concepts
Paging Example Page name as issued by CPU Applied Operating System Concepts
Paging • Paging • is a scheme that permits address space to be noncontiguous • Basic Method • Divide physical memory into fixed-sized blocks called frames(size is power of 2, between 512 bytes and 8192 bytes). • Divide logical memory into blocks of same size called pages. • Keep track of all free frames. • Set up a page table to translate logical to physical addresses. • Internal fragmentation. Applied Operating System Concepts
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. p d Applied Operating System Concepts
Address Translation Architecture Applied Operating System Concepts
Paging Example Applied Operating System Concepts
Paging Example Applied Operating System Concepts
Free Frames Before allocation After allocation Applied Operating System Concepts
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 (PRLR) indicates size of table. • Every memory access requires two memory accesses. • One for the page table and one for the data/instruction. • To speed up, a special fast-lookup hardware cache called associative registers • or • translation look-aside buffers (TLBs) Applied Operating System Concepts
Table 내용의 대비 Page Table TLB Frame # Page # Page # Frame # 극히 일부만 record You need frame # here Applied Operating System Concepts
Associative Register • Two types of memory • 일반 memory: (예: 컴퓨터 메모리) • give address -- return record • Associative memory: (예: 전화번호부) • give field of record -- return record • Parallel search 를 안하면 너무 느리다 (또는 index 구비) • 따라서 implementation이 비싸진다. • Associative registers (TLB)– parallel search • Since only part of page table is in TLB (register – fast – expensive-keep only active pages) • Address translation (A´, A´´) • Try associative register first (portion of page table) • If A´ is in associative register, get frame # out. • Otherwise get frame # from page table in main memory • context switch 때 flush (remove old entries) Frame # Page # You need frame # here record Applied Operating System Concepts
Paging Hardware With TLB Applied Operating System Concepts
Effective Access Time • Associative lookup = time unit • memory cycle time = 1 microsecond • Hit ratio = • percentage found in the associative registers • Effective Access Time (EAT) <hit> <miss> EAT = (1 + ) + (2 + )(1 – ) = 2 + – Applied Operating System Concepts
Memory Protection • Protection bit with each frame. (예: rwx) • Valid-invalid bit - each entry in the page table: • “valid” indicates the page is legal (not a valid page). • “invalid” indicates otherwise (access not allowed) 디스크 정보 memory copy memory copy is not valid any more (it’s value has been changed by other process) Applied Operating System Concepts
Valid (v) or Invalid (i) Bit In A Page Table Applied Operating System Concepts
Two-Level Page-Table Scheme • Program has a large address space. Use 32 bit address 1 million page table entries (if page size – 4K) • 4 MB page table (if each entry -- 4B) • 전체 page table 을 disk에 • Page table 자체를 page 단위로 on-demand load Applied Operating System Concepts
Two-Level Page-Table Scheme 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 number page offset p2 pi d 10 12 10 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 • Address space 가 더 커지면 세번째 disk block 이 (like B-tree) (not data block, but next level page table) • Since each level is stored as a separate table in memory, covering a logical address to a physical one may take three, four memory accesses. • if address is 64 bits • Even though time needed for one memory access is quintupled, caching permits performance to remain reasonable. • Cache hit rate of 98 percent yields: effective access time = 0.98 x 120 + 0.02 x 520 = 128 nanoseconds.which is only a 28 percent slowdown in memory access time. Applied Operating System Concepts
Inverted Page Table 문제: page table 매우 큰 이유 page 개수에 (disk size) 비례한 table 크기 one “page-table-entryper page”, 그러나 한 순간에는 소수의 페이지만 (필요 .AND. Memory Resident) Cure:let “one page-table- entryper pageframe” (다음 페이지 그림 참조) one page table system-wide page frame 수만큼의 entry만 필요 (fixed size) table entry 의 내용은=(process ID, page #) table 의 index가 frame # 단점 It is associative search and need to search entire table. 조치 Either use hash table to limit the search (slower) Or use associative registers (expensive) (1) (2) (3) (4) (5) Applied Operating System Concepts
Inverted Page Table Architecture Frame Number Applied Operating System Concepts
Shared Pages ㅣoop에서 명령어 자체를 (즉 address를) increment 하면? • Shared code • Re-entrant Code, Non-self-modifying Code: eg inc memory_address • One copy of read-only (re-entrant) code shared among processes (eg, text editors, compilers, window systems). • Shared code must appear in same location in the logical addressspace of all processes. • Private code and data • Each process keeps separate copy of data. (eg hwp – file, 글자크기 ..) • Private data can appear anywhere in logical address space (base 주소). P조유근 P.C. 글자크기 font P이석호 P.C. 글자크기 font P고건 P.C. 글자크기 font hwp code Applied Operating System Concepts
Shared Pages Example Editor is shared Editor consists of 3 pages -- ed1, ed2, ed3 Applied Operating System Concepts
Segmentation • A program is a collection of segments. • A segment is a logical unit such as: main (), function, global variables, stack, symbol table, arrays Applied Operating System Concepts
User’s View of a Program variable size main memory DISK Applied Operating System Concepts
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. Applied Operating System Concepts
Segmentation Architecture (Cont.) • Protection • Protection bits associated with segments • Each entry : • validation bit = 0 illegal segment • R / W / X / Abits • Sharing • shared segments • same segment number • Allocation • first fit / best fit • external fragmentation • Since segments vary in length, memory allocation is a dynamic storage-allocation problem. 여러 종류의 fault -- segment system S L valid rwxa disk_주소 Segment length Applied Operating System Concepts
Segmentation Hardware Applied Operating System Concepts
Example of Segmentation Question: What if (segment size >> memory size) ? Applied Operating System Concepts