320 likes | 461 Views
CSNB334 Advanced Operating Systems 5. Memory Management. Review of basic concepts. What are the requirements of memory management? Relocation For managing the available memory in a multiprogramming environment. Protection Must be satisfied by the hardware (processor) rather than the OS.
E N D
Review of basic concepts • What are the requirements of memory management? • Relocation • For managing the available memory in a multiprogramming environment. • Protection • Must be satisfied by the hardware (processor) rather than the OS. • Permissibility of a memory reference by an instruction can only be checked at the time of execution of the instruction. • Sharing • Physical Organization • Moving information between the main memory and secondary memory.
Relocation • Phenomenon by which a process may occupy different partitions during the course of its life. • Three types of addresses • Logical Address • Reference to a memory location independent of the current assignment of the process to memory. • Relative Address • Type of logical address – address is expressed as a location relative to some known point. • Physical Address/ Absolute Addres • Actual location in main memory.
Loading • Absolute Loading • Decision of where to load a module in the memory is made at compile time. • Thus, a given module is always loaded into a specific location in main memory. • Relocatable Loading. • Decision is made at load time. • Thus a module can be loaded anywhere in the main memory. • But, is swapped back to the same memory. • Dynamic Run-Time loading • Decision is made at run-time. • Therefore, we can swap a process image into different locations at different times. • Done by special processor hardware rather than software. • Base Register, Bounds Register, Adder, Comparator.
Memory Management Techniques • Fixed partitioning • Main memory is divided into a number of static partitions at system generation time. • Equal-size partitions • Unequal-size partitions • Pros • Simple to implement; little OS overhead • Cons • Inefficient : internal fragmentation • Maximum number of active processes – fixed. • Placement Algorithm • One process queue per partition. • Single queue.
Memory Management Techniques • Dynamic partitioning • Partitions are created dynamically • Each process is loaded into a partition of exactly the same size as that process. • Pros • No internal fragmentation. • Cons • External fragmentation. • Counteract : by compaction. • But an overhead for the processor. • Placement Algorithm : (because compaction is time consuming) • Best-fit • First-fit. • Next-fit.
Memory Management Techniques • Paging • Main memory is divided into a number of equal-sized, relatively small frames. • Each process is divided into a number of equal-sized pages – same length as a frame. • A process is loaded by loading all of its pages into available frames. • Not necessarily be contiguous. • Possible thru the use of a page table for each process. • Logical address (page number, offset) --- Physical Address (frame number, offset). • Pros • No external fragmentation • Cons • A small amount of internal fragmentation.
Address Translation in a Paging System Logical address Page # Offset Frame # Offset Register Page Table Ptr Page Table Offset Page Frame P# + Frame # Program Paging Main Memory
Memory Management Techniques • Segmentation • Each process is divided into number of segments. • Need not be of same size. • A process is loaded by loading all of its segments into dynamic partitions. • Need not be contiguous • Use segment table. • Difference with dynamic partitioning • A process may occupy more than one partition. • Partitions need not be contiguous. • Pros • No internal fragmentation. • Cons • External fragmentation : though less severe than dynamic partitioning because of the small size of the segments.
Hardware support Virtual Address Segment Table + Seg # Offset = d Base + d Register Seg Table Ptr Segment Table d Segment S# + Length Base Program Segmentation Main Memory
Memory Management Techniques • Virtual memory • Similar to paging/segmentation except that it is not necessary to load all of the segments/pages of a process into main memory. • Nonresident pages/segments that are needed are brought in later automatically. • May require writing a page/segment out to disk if the memory is full. • Pros: • Large virtual address space. • More processes may be maintained in main memory. • A process may be larger than all of main memory. • Cons • Overhead of complex memory management. • Thrashing : The system spends most of its time swapping pieces rather than executing instructions.
Translation Lookaside Buffer • Every virtual memory reference causes two physical memory access: • To fetch the appropriate page table entry. • To fetch the desired data. • Thus, the memory access time is doubled. To overcome this: • Use a special high-speed cache for page table entries – Translation Lookaside Buffer • Similar to a memory cache. • Contains those page table entries – most recently used.
Virtual Memory • Virtual memory can be based on • Paging only • Virtual Address : Page Number + Offset • Page table entry : P(bit)+M(bit)+ Frame Number • Segmentation • Virtual Address : Segment Number + Offset • Segment table entry : P(bit)+M(bit)+ Length + Segment Base • Or, a combination of the two. • Virtual Address : Segment Number + PageNumber + Offset • Segment table entry : Length + Segment Base • Page table entry : P(bit)+M(bit)+ Frame Number
Memory Management in Linux • Linux uses demand paged virtual memory for memory management design. • It's a dynamic memory allocation technique that consists of deferring page frame allocation until the last possible moment, for example, when a process attemps to access a page that is not present in RAM. • Basic unit of memory allcation – page. • Page size : 212 (4096 bytes or 4KB). • Allocation of blocks in physical memory is as page frames • Protection mechanism is page by page • Sharing is also based on pages • Swapping controls automatic movement through the memory hierarchy.
Getting the Page size • The standard POSIX method • #include <unistd.h> • long sysconf (int name); • long page_size = sysconf(_SC_PAGESIZE); • Linux also provides • int getpagesize (void); • Returns the page size in bytes. • PAGE_SIZE macro • int page_size = PAGE_SIZE • Retrieves the page size at compile time.
Managing the Virtual Address Space in Linux • Each process : its own virtual address space. • In i386 arch, the virtual address is 32-bits wide. • Therefore, the total virtual memory that a virtual address can reference = 232 = 4GB. • Page Size = 212. Therefore, number of pages that a virtual address can reference = 220. • Assuming that each PTE is 4 bytes, how many pages are needed to store the page table?
Two Level Page Table • The amount of memory devoted to page tables alone is quite high. • Therefore, page tables are stored in virtual memory rather than main memory. This is achieved thru the use of a two-level hierarchical page table. Root Page Table (4KB) User Page Table (4MB) User Address Space (4 GB)
Hierarchical paging • Address translation scheme:
Virtual -> physical address translation ……is a three level process in Linux
Virtual address • 4 parts: • Page directory offset j.pgd • Page middle director offset j.pmd • Page table offset j.pte • Offset within page j.offset • The physical address i for a virtual address j is : • i = PTE(PMD(PGD(j.pgd)+j.pmd)+j.pte)+j.offset.
The x86(32-bit addressing) only supports a two level conversion of the address. • This is dictated by the hardware’s MMU… • This is accomplished by reducing each page middle directory to only a single entry.
Segmentation in Linux • Linux uses the segmentation model in a limited way. • Each virtual address space is divided into two segments: • User segment (3 Gb) to contain the applications code and data. Addressable by the user. • Unmapped virtual addresses are simply not used. • Kernel segment (1 Gb) permanently mapped and associated with fixed physical memory addresses used by the kernel. • System calls execute in kernel segment(mode). 4 GB Kernel Space (Code + Data) Kernel 3 GB 2 GB Tasks User Space (Code + Data) 1 GB ox00000000
Code (also called text) segment Static Data segments Initialized global (and C static) variables Uninitialized global variables (zeroed when initializing the process, also called bss) Stack segment: function calls, local variables (also called automatic in C) Heap segment (malloc()) Per-Process Virtual Memory Layout
Page Table Flags • Each entry in the theoretical page table contains the following information: • Valid flag. This indicates if this page table entry is valid, • The physical page frame number that this entry is describing, • Access control information. This describes how the page may be used. Can it be written to? Does it contain executable code? • Flags in the page table entry indicate • The legal access modes into the page. • The page’s status. • A page’s status can give vital information for how memory management is performed.
Page Table Flags • PAGE_NONE – No physical memory page associated with entry. • PAGE_SHARED – All types of access permitted. • PAGE_READONLY – No writing. “Copy-on-Write” can be used. • PAGE_KERNEL – kernel segment only allowed access. • PAGE_KERNEL_RO – kernel read-only access.