memory management · – need for compaction to counter external fragmentation (increased cpu load)...
TRANSCRIPT
MEMORY MANAGEMENT
• Requirements:
– Relocation (to different memory areas)– Protection (run time, usually implemented together with relocation)– Sharing (and also protection)– Logical organization– Physical organization
• Key terms:
– Frame – a fixed-size block of the main memory– Page – a fixed-size block of data (secondary memory)
∗ A page may temporarily be copied into a frame
– Segment – a variable-size block of data (secondary memory)
∗ An entire segment may temporarily be copied into the main memory (segmen-tation) or the segment may be divided into pages which can be individuallycopied into the main memory (combined segmentation and paging)
CS 409, FALL 2013 MEMORY MANAGEMENT/1
LOGICAL AND PHYSICAL ORGANIZATION
• Logical organization is linear– Different parts of the memory
can have different permis-sions (read-write, read-only,etc.)
– Best implemented using seg-mentation
• Physical organization cannot belinear
– Cannot leave the responsibil-ity of managing memory tothe programmer
– Memory requirements mayexceed physical memory, areunknown at build time any-way
CS 409, FALL 2013 MEMORY MANAGEMENT/2
MEMORY MANAGEMENT TECHNIQUES
• External fragmentation = memory exists to satisfy a request, but is not contiguous
• Internal fragmentation = allocated memory may be slightly larger than requestedmemory; difference not used
• Fixed Partitioning: Main memory is divided into a number of static partitions; a pro-cess may be loaded into a partition of equal or greater size
– Simple to implement, but inefficient (internal fragmentation)– Maximum number of active processes is fixed
• Dynamic Partitioning: Partitions are created dynamically, so that each process isloaded into a partition of exactly the same size as that process
– No internal fragmentation, more efficient use of main memory– Need for compaction to counter external fragmentation (increased CPU load)
• Simple Paging: Main memory divided into a number of equal-size frames; each pro-cess is divided into a number of equal-size pages (same size as frames); a processis loaded by loading all of its pages into available, not necessarily contiguous, frames
– No external fragmentation; some (small) internal fragmentation
CS 409, FALL 2013 MEMORY MANAGEMENT/3
MEMORY MANAGEMENT TECHNIQUES (CONT’D)
• Simple Segmentation: Process divided into a number of segments; process isloaded by loading all of its segments into dynamic partitions (need not be contiguous)
– No internal fragmentation, improved memory utilization, reduced overhead vs.dynamic partitioning
– External fragmentation
• Virtual Memory Paging: As with simple paging, except that it is not necessary to loadall of the pages of a process; nonresident pages are brought in later, as needed
– No external fragmentation, higher degree of multiprogramming, large virtual ad-dress space
– Overhead of complex memory management
• Virtual Memory Segmentation: As with simple segmentation, but is not necessary toload all of the segments; nonresident segments are brought in later, as needed
– No internal fragmentation, higher degree of multiprogramming, large virtual ad-dress space, protection and sharing support
– Overhead of complex memory management
CS 409, FALL 2013 MEMORY MANAGEMENT/4
PARTITIONING
• Same-size partitions– All programs occupy the same amount of space no matter their
size = inefficient, internal fragmentation (whenever programdata smaller than partition size)
• Unequal size partitions– Existence of smaller partitions reduce the internal fragmenta-
tion– Memory assignment: queues of processes waiting to be
brought into main memory∗ A single queue or one queue per partition size
• The fixed number of partitions limits the number of active pro-cesses in the system
• Small jobs will not utilize partition space efficiently• Programs larger than any partition loaded via overlays• OS can only swap out whole processes if needed
CS 409, FALL 2013 MEMORY MANAGEMENT/5
DYNAMIC PARTITIONING
• Process is allocated exactly as much memoryas it requires = partitions of variable length andnumber
• Used by IBM’s mainframe operating systemOS/MVT
• Placement algorithms:– Best-fit: chooses the block that is closest
in size to the request– First-fit: first block that is large enough– Next-fit: next large enough block since the
last allocation• Main disadvantage: external fragmentation
– Memory utilization declines as memorybecomes more and more fragmented
– Solution: compacting = OS shifts pro-cesses in memory so that they are con-tiguous and free memory is in one block;costly
CS 409, FALL 2013 MEMORY MANAGEMENT/6
DYNAMIC PARTITIONING
• Process is allocated exactly as much memoryas it requires = partitions of variable length andnumber
• Used by IBM’s mainframe operating systemOS/MVT
• Placement algorithms:– Best-fit: chooses the block that is closest
in size to the request– First-fit: first block that is large enough– Next-fit: next large enough block since the
last allocation• Main disadvantage: external fragmentation
– Memory utilization declines as memorybecomes more and more fragmented
– Solution: compacting = OS shifts pro-cesses in memory so that they are con-tiguous and free memory is in one block;costly
CS 409, FALL 2013 MEMORY MANAGEMENT/6
DYNAMIC PARTITIONING (CONT’D)
CS 409, FALL 2013 MEMORY MANAGEMENT/7
RELOCATION
• Logical address = referenceto memory location indepen-dent on the assignment ofdata to memory
• Relative address = addressexpressed as a location rel-ative to some known point
• Physical/Absolute address:actual location in main mem-ory
• Relocation: Logical address⇒ relative (to the beginningof the process) address ⇒physical address
– Needs hardware sup-port = Memory Manage-ment Unit (MMU)
CS 409, FALL 2013 MEMORY MANAGEMENT/8
PAGING
• Partition memory into equal fixed-size chunks that are relativelysmall (pages)
• Process is also divided into small fixed-size chunks of the samesize (frames)
• Key data structure: page table– Maintained by operating system for each process– Contains the frame location for each page in the process– Processor must know how to access for the current process– Used by processor to produce a physical address
CS 409, FALL 2013 MEMORY MANAGEMENT/9
LOGICAL ADDRESS AND PAGING/SEGMENTATION
CS 409, FALL 2013 MEMORY MANAGEMENT/10
LOGICAL ADDRESS AND PAGING
CS 409, FALL 2013 MEMORY MANAGEMENT/11
SEGMENTATION
• A program can be subdivided into segments
– May vary in length– Maximum length usually given
• Addressing consists of two parts:
– Segment number– Offset
• Similar to dynamic partitioning
• Eliminates internal fragmentation
CS 409, FALL 2013 MEMORY MANAGEMENT/12
LOGICAL ADDRESS AND SEGMENTATION
CS 409, FALL 2013 MEMORY MANAGEMENT/13
VIRTUAL MEMORY
• Not necessary that all the process is in memory as long as all references are logicaladdresses and address space can be split into pages
– OS brings into main memory a few pieces of the program = resident set– An interrupt is generated when an address is needed but is in the virtual memory
(usually on disk) instead of the main memory– OS places the process in a Blocked state while the missing piece is brought into
the main memory
∗ Operating system issues a disk I/O Read request (usually)∗ Another process is dispatched to run while the disk I/O takes place∗ When disk I/O is complete (interrupt), the affected process is placed back in
the Ready state
• More processes can be maintained in the main memory
– Only some of the pieces of each process are loaded– With so many processes in main memory it is very likely a process will be in the
Ready state at any particular time
• A process can even be larger than all of the main memory!
CS 409, FALL 2013 MEMORY MANAGEMENT/14
PRINCIPLE OF LOCALITY
• Main problem: Thrashing = the system spends most of its time swapping processpieces rather than executing instructions
• To avoid this, the OS tries to guess based on recent history which pieces are leastlikely to be used in the near future
• Guess is based on the principle of locality
– Program and data references within a process tend to cluster– Only a few pieces of a process will be needed over a short period of time– Therefore it is possible to make intelligent guesses about which pieces will be
needed in the future
CS 409, FALL 2013 MEMORY MANAGEMENT/15
HARDWARE SUPPORT FOR VIRTUAL MEMORY
• Hardware must support paging and segmentation
• OS must include software for managing the movement of pages and/or segmentsbetween secondary memory and main memory
– Virtual memory is usually associated with systems that employ paging– Each process has its own page table– Each page table entry contains the frame number of the corresponding page in
main memory
CS 409, FALL 2013 MEMORY MANAGEMENT/16
MEMORY MANAGEMENT FORMATS
CS 409, FALL 2013 MEMORY MANAGEMENT/17
ADDRESS TRANSLATION
CS 409, FALL 2013 MEMORY MANAGEMENT/18
TWO-LEVEL HIERARCHICAL PAGE TABLE
CS 409, FALL 2013 MEMORY MANAGEMENT/19
ADDRESS TRANSLATION IN A TWO-LEVEL PAGING SYSTEM
CS 409, FALL 2013 MEMORY MANAGEMENT/20
INVERTED PAGE TABLE
• Page number portion of avirtual address is mappedinto a hash value
– Hash value points toinverted page table
• Fixed proportion of realmemory is required for thetables regardless of thenumber of processes orvirtual pages supported
• Structure is called invertedbecause it indexes pagetable entries by framenumber rather than byvirtual page number
CS 409, FALL 2013 MEMORY MANAGEMENT/21
TRANSLATION LOOKASIDE BUFFER (TLB)
• Problem: Eachvirtual memoryreference cancause two phys-ical memoryaccesses:
– one to fetchthe page ta-ble entry
– one to fetchthe data
• Solution: usea special high-speed cachecalled a trans-lation lookasidebuffer
CS 409, FALL 2013 MEMORY MANAGEMENT/22
TLB ALGORITHM
CS 409, FALL 2013 MEMORY MANAGEMENT/23
ASSOCIATIVE MAPPING
• TLB cannot be indexed on page number (it contains only some page table entries)
• TLB entry must include the page number as well as the complete page table entry
• Hardware that allows simultaneously interrogation of a number of TLB entries todetermine if there is a match on page number = associative mapping
CS 409, FALL 2013 MEMORY MANAGEMENT/24
TLB AND CACHE
CS 409, FALL 2013 MEMORY MANAGEMENT/25
PAGE SIZE
• The smaller the page size, thelesser the amount of internal frag-mentation
• However, more pages are requiredper process
• More pages per process meanslarger page tables
• For large programs in a heav-ily multiprogrammed environmentsome portion of the page tables ofactive processes must be in virtualmemory instead of main memory
• The physical characteristics ofmost secondary-memory devicesfavor a larger page size for moreefficient block transfer of data
Computer Page SizeAtlas 512 48-bit wordsHoneywell-Multics 1024 36-bit wordsIBM 370/XAIBM 370/ESA 4 KBVAX family 512 bytesIBM AS/400 512 bytesDEC Alpha 8 KBMIPS 4 KB to 16 MBUltraSPARC 8 KB to 4 MBPentium 4 KB or 4 MBIBM POWER 4 KBItanium 4 KB to 256 MB
CS 409, FALL 2013 MEMORY MANAGEMENT/26
COMBINED PAGING AND SEGMENTATION
• Segmentation allows programmer to view memory as consisting of multiple addressspaces (or segments)
– Simplifies handling of large data structures– Facilitates sharing data among processes– Facilitates protection
• Each segment table entry contains the starting address of the corresponding seg-ment in main memory and the length of the segment
• A bit is needed to determine if the segment is already in main memory
• Another bit is needed to determine if the segment has been modified since it wasloaded in main memory
• In a combined paging/segmentation system a user’s address space is broken up intoa number of segments
– Each segment is broken up into a number of fixed-sized pages which are equalin length to a main memory frame
– Segmentation is visible to the programmer, paging is transparent
CS 409, FALL 2013 MEMORY MANAGEMENT/27
ADDRESS TRANSLATION
CS 409, FALL 2013 MEMORY MANAGEMENT/28
PROTECTION RELATIONSHIP
• Segmentation lends itself tothe implementation of protec-tion and sharing policies
• Each entry has a base ad-dress and length so inadver-tent memory access can becontrolled
• Sharing can be achieved bysegments referencing multipleprocesses
• Each entry in segment tablefeatures:
– Validation bit (0 = illegalsegment)
– Read/write/execute privi-leges
CS 409, FALL 2013 MEMORY MANAGEMENT/29
OS MEMORY MANAGEMENT SOFTWARE
• The design of the memory management portion of an operating system depends onthree fundamental areas of choice:
– Whether or not to use virtual memory techniques– The use of paging or segmentation or both– The algorithms employed for various aspects of memory management
CS 409, FALL 2013 MEMORY MANAGEMENT/30
VIRTUAL MEMORY FETCH POLICIES
• Fetch policy: Determines when a page should be brought into memory
• Demand paging only brings pages into main memory when a reference is made to alocation on the page
– Many page faults when process is first started– But drops as more and more pages are brought in (principle of locality – most
future references will be to pages that have recently been brought in)
• Prepaging once a page is demanded, more are brought in
– Exploits the characteristics of most secondary memory devices– If pages of a process are stored contiguously in secondary memory it is more
efficient to bring in a number of pages at one time– Ineffective if extra pages are not referenced
CS 409, FALL 2013 MEMORY MANAGEMENT/31
VIRTUAL MEMORY PLACEMENT AND REPLACEMENT POLICIES
• Placement determines where in real memory a process piece is to reside
– Important design issue in a segmentation system– But irrelevant to paging or combined paging with segmentation (hardware per-
forms functions with equal efficiency)
• Replacement deals with the selection of a page in main memory to be replaced whena new page must be brought in
– Objective: the page that is removed is the page least likely to be referenced inthe near future
– The more elaborate the better at guessing, but at the cost of greater hardwareand software overhead
• Frame Locking: When a frame is locked the page currently stored in that frame maynot be replaced
– The kernel as well as key control structures are held in locked frames– I/O buffers and time-critical areas may be locked into main memory frames– Achieved by associating a lock bit with each frame
CS 409, FALL 2013 MEMORY MANAGEMENT/32
BASIC PAGE REPLACEMENT ALGORITHMS
• Optimal: Selects page for which the time to the next reference is the longest (ideal)
• Least Recently Used (LRU): Replaces the page that has not been referenced for thelongest time
– Principle of locality ⇒ LRU = page least likely to be referenced in the near future– Difficult to implement, substantial overhead– One approach is to tag each page with the time of last reference
CS 409, FALL 2013 MEMORY MANAGEMENT/33
BASIC PAGE REPLACEMENT ALGORITHMS (CONT’D)
• First-in-First-out (FIFO): Treats page frames allocated to a process as a circularbuffer; pages are removed in round-robin style
– Simple to implement, page that has been in memory the longest is replaced
• Clock: Associates a use bit with each frame, initialized with 1 on load
– Any frame with a use bit of 1 is passed over by the algorithm (but the bit is reset)– Page frames visualized as laid out in a circle
CS 409, FALL 2013 MEMORY MANAGEMENT/34
THE CLOCK REPLACEMENT ALGORITHM (CONT’D)
CS 409, FALL 2013 MEMORY MANAGEMENT/35
COMPARISON OF PAGE REPLACEMENT ALGORITHMS
CS 409, FALL 2013 MEMORY MANAGEMENT/36
ADDITIONAL REPLACEMENT POLICIES
• Page buffering: A replaced page is assigned to the free page list or modified pagelist (as the case might be)
– Pages are written to disk in clusters to improve efficiency
• Replacement Policy and Cache Size: With large caches replacement of pages canhave a performance impact; if the page frame to be replaced is in the cache thenthat cache block is lost as well
• Resident Set Management: The OS must decide how many pages to bring into mainmemory
– The smaller the amount of memory allocated to each process, the more pro-cesses can reside in memory
– But small number of pages loaded increases page faults– But beyond a certain size further allocation of pages will not effect page fault rate– Fixed-allocation gives a process a fixed number of frames in main memory within
which to execute (page fault ⇒ one of the pages of that process is replaced)– Variable-allocation allows the number of frames allocated to a process to vary
CS 409, FALL 2013 MEMORY MANAGEMENT/37
ADDITIONAL REPLACEMENT POLICIES (CONT’D)
• Replacement Scope
– Local: chooses only among the resident pages of the process that generatedthe page fault
– Global: considers all unlocked pages in main memory (not possible for fixedallocation)
– Most used policy: variable allocation, global scope
• Cleaning policy: determine when a modified page should be written out to secondarymemory
– Demand Cleaning: a page is written out only when it has been selected forreplacement
– Precleaning: allows the writing of pages in batches
CS 409, FALL 2013 MEMORY MANAGEMENT/38
LOAD CONTROL
• Determines the number of processes that will be resident in main memory
• Critical in effective memory management
• Too few processes, many occasions when all processes will be blocked and muchtime will be spent in swapping
• Too many processes will lead to thrashing
• If the degree of multiprogramming is to be reduced, one or of the currently residentprocesses must be swapped out; six possibilities exist:
– Lowest-priority process– Faulting process– Last process activated– Process with the smallest resident set– Largest process– Process with the largest remaining execution window
• Do not confuse swapping with page replacement!
CS 409, FALL 2013 MEMORY MANAGEMENT/39
LINUX MEMORY MANAGEMENT
• Two main aspects: process virtual memory and kernel memory allocation
• Three-level page table structure for the virtual memory
• Page replacement based on the clock algorithm, with an 8-bit age variable (incre-mented each time a page is accessed, decremented periodically)
– An age of 0 not accessed in a long time, best candidate for replacement (LRU)
CS 409, FALL 2013 MEMORY MANAGEMENT/40
KERNEL MEMORY ALLOCATION IN LINUX
• Page allocator inefficient because the kernel usually requires small chunks in oddsizes
• A buddy algorithm (power-of-two allocator) and slab allocation are used so that mem-ory for the kernel can be allocated and deallocated in units of one or more pages
CS 409, FALL 2013 MEMORY MANAGEMENT/41
OTHER ADVANTAGES OF VIRTUAL MEMORY
• Process creation: Copy-on-write (COW)
– Allows both parent and child processes to initially share the same pages– If either process modifies a shared page, only then is the page copied– Allows more efficient process creation as only modified pages are copied
• Memory-mapped files
– Memory-mapped file I/O allows file I/O to be treated as routine memory accessby mapping a disk block to a page in memory
– A file is initially read using demand paging. A page-sized portion of the file isread from the file system into a physical page. Subsequent reads/writes to/fromthe file are treated as ordinary memory accesses.
– Simplifies file access by treating file I/O through memory rather than read()and write() system calls
– Also allows several processes to map the same file allowing the pages in mem-ory to be shared
• Mapping of shared libraries
CS 409, FALL 2013 MEMORY MANAGEMENT/42
COPY ON WRITE
• Before any writing takes place:
CS 409, FALL 2013 MEMORY MANAGEMENT/43
COPY ON WRITE (CONT’D)
• As soon as Process 1 modifies Page C:
CS 409, FALL 2013 MEMORY MANAGEMENT/44
SHARED LIBRARIES
CS 409, FALL 2013 MEMORY MANAGEMENT/45
MEMORY MAPPED FILES
CS 409, FALL 2013 MEMORY MANAGEMENT/46