Operating Systems: Internals and Design Principles Chapter 7 Memory Management Seventh Edition William Stallings
Memory Management Requirements Memory management is intended to satisfy the following requirements: Relocation Protection Sharing Logical organization Physical organization
Physical Organization Cannot leave the programmer with the responsibility to manage memory Memory available for a program plus its data may be insufficient overlaying allows various modules to be assigned the same region of memory but is time consuming to program Programmer does not know how much space will be available
Fixed Portioning Equal-size partitions Any process whose size is less than or equal to the partition size can be loaded into an available partition If all partitions are full, the operating system can swap a process out of a partition A program may not fit in a partition. The programmer must design the program with overlays Fixed partitioning in main memory is inefficient. Any program, no matter how small, occupies an entire partition. What about the memory left over if the program does not fit perfectly. This is called internal fragmentation.
Fixed Portioning Solution to Equal-size partitions? Assign each process to the smallest partition within which it will fit Queue for each partition Processes are assigned in such a way as to minimize wasted memory within a partition Unequal size portioning. Less Internal fragmentation.
The number of partitions specified at system generation time limits the number of active processes in the system Small jobs will not utilize partition space efficiently
Dynamic Portioning Partitions are of variable length and number Process is allocated exactly as much memory as required Eventually get holes in the memory. This is called external fragmentation Must use compaction To shift processes so they are contiguous and all free memory is in one block
Placement Algorithms Best-fit First-fit chooses the block that is closest in size to the request begins to scan memory from the beginning and chooses the first available block that is large enough Next-fit begins to scan memory from the location of the last placement and chooses the next available block that is large enough
Addresses Logical reference to a memory location independent of the current assignment of data to memory Relative address is expressed as a location relative to some known point Physical or Absolute actual location in main memory
Relocation
Paging
Paging Paging Main memory and process both are divided into fixed size small chunks, the chunk of process is known as page and chunk of memory is known as frames. Each process has its own page table A bit is needed to indicate whether the page is in main memory or not Page table entry contains frame# of the corresponding page in memory.
Paging
Paging
Segmentation A program can be subdivided into segments may vary in length there is a maximum length Addressing consists of two parts: segment number an offset Similar to dynamic partitioning Eliminates internal fragmentation Paging is invisible to programmer while segmentation is usually visible.
Logical view of segmentation
Segmentation Logical to physical address mapping
Summary Memory Management one of the most important and complex tasks of an operating system needs to be treated as a resource to be allocated to and shared among a number of active processes desirable to maintain as many processes in main memory as possible desirable to free programmers from size restriction in program development basic tools are paging and segmentation (possible to combine)» paging small fixed-sized pages» segmentation pieces of varying size
Memory Management Real memory main memory, the actual RAM Virtual memory memory on disk allows for effective multiprogramming and relieves the user of tight constraints of main memory
Thrashing A state in which the system spends most of its time swapping process pieces rather than executing instructions To avoid this, the operating system tries to guess, based on recent history, which pieces are least likely to be used in the near future
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 Avoids thrashing
Paging The term virtual memory is usually associated with systems that employ paging Use of paging to achieve virtual memory was first reported for the Atlas computer Each process has its own page table each page table entry contains the frame number of the corresponding page in main memory
Memory Management
Paging System
Two Level Paging
Two Level Paging Example A logical address (32bit machine, 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 consists of a 10-bit page number, a 10-bit page offset Thus, a logical address is organized as (p1,p2,d) where p1 is an index into the outer page table p2 is the displacement within the page of the outer page table
Paging System with Inverted Page Table
TLB
TLB
Page Size The design issue of page size is related to the size of physical main memory and program size Contemporary programming techniques (OO & multi-threading) used in large programs tend to decrease the locality of references within a process main memory is getting larger and address space used by applications is also growing most obvious on personal computers where applications are becoming increasingly complex
Basic Algorithms Algorithms used for the selection of a page to replace: 1. Optimal Replace the page that won t be needed for the longest time in the future 2. Least recently used (LRU Replace the page that hasn t been referenced for the longest time 3. First-in-first-out (FIFO) 4. Clock Clock hand sweeps over pages looking for one with used bit = 0 Replace pages that haven t been referenced for one complete revolution of the clock
Replacement policies
Replacement policies
Replacement policies
Clock Policy
Summary Desirable to: maintain as many processes in main memory as possible free programmers from size restrictions in program development With virtual memory: all address references are logical references that are translated at run time to real addresses a process can be broken up into pieces two approaches are paging and segmentation management scheme requires both hardware and software support