Chapter 8 Memory Management Operating System Concepts with

  • Slides: 53
Download presentation
Chapter 8: Memory Management Operating System Concepts with Java – 8 th Edition 8.

Chapter 8: Memory Management Operating System Concepts with Java – 8 th Edition 8. 1 Silberschatz, Galvin and Gagne © 2009

Chapter 8: Memory Management n Background n Swapping n Contiguous Memory Allocation n Paging

Chapter 8: Memory Management n Background n Swapping n Contiguous Memory Allocation n Paging n Structure of the Page Table n Segmentation Operating System Concepts with Java – 8 th Edition 8. 2 Silberschatz, Galvin and Gagne © 2009

Objectives n To provide a detailed description of various ways of organizing memory hardware

Objectives n To provide a detailed description of various ways of organizing memory hardware n To discuss various memory-management techniques, including paging and segmentation Operating System Concepts with Java – 8 th Edition 8. 3 Silberschatz, Galvin and Gagne © 2009

Background n Program must be brought (from disk) into memory and placed within a

Background n Program must be brought (from disk) into memory and placed within a process for it to be run n Main memory and registers are only storage CPU can access directly n Register access in one CPU clock (or less) n Main memory can take many cycles n Cache sits between main memory and CPU registers Operating System Concepts with Java – 8 th Edition 8. 4 Silberschatz, Galvin and Gagne © 2009

Memory Protection n Each process is allocated memory by the OS n Protection of

Memory Protection n Each process is allocated memory by the OS n Protection of memory required to ensure correct operation n Process operating in the user mode should be able to access only its memory l Should be forbidden from accessing memory of belonging to other processes or kernel memory n Base and limit registers l Base register indicates the lower bound and limit register the amount of memory n Any illegal access will be trapped by the kernel Operating System Concepts with Java – 8 th Edition 8. 5 Silberschatz, Galvin and Gagne © 2009

Base and Limit Registers n A pair of base and limit registers define the

Base and Limit Registers n A pair of base and limit registers define the logical address space. Operating System Concepts with Java – 8 th Edition 8. 6 Silberschatz, Galvin and Gagne © 2009

Binding of Instructions and Data to Memory n Instructions and data need to be

Binding of Instructions and Data to Memory n Instructions and data need to be put into memory before execution n Binding is the process of mapping from symbolic to physical addresses l Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes l Load time: Must generate relocatable code if memory location is not known at compile time l 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) Operating System Concepts with Java – 8 th Edition 8. 7 Silberschatz, Galvin and Gagne © 2009

Multistep Processing of a User Program Operating System Concepts with Java – 8 th

Multistep Processing of a User Program Operating System Concepts with Java – 8 th Edition 8. 8 Silberschatz, Galvin and Gagne © 2009

Logical vs. Physical Address Space n The concept of a logical address space that

Logical vs. Physical Address Space n The concept of a logical address space that is bound to a separate physical address space is central to proper memory management. l Logical address – generated by the CPU; also referred to as virtual address l Physical address – address seen by the memory unit (loaded into memory address register) n For compile time and load time binding logical and physical addresses are same n Logical and physical addresses differ in execution-time address-binding scheme l Logical address usually referred to as “virtual address” Operating System Concepts with Java – 8 th Edition 8. 9 Silberschatz, Galvin and Gagne © 2009

Memory Management Unit n Set of all logical addresses generated by program – Logical

Memory Management Unit n Set of all logical addresses generated by program – Logical address space n Set of all corresponding physical addresses – physical address space n Run-time mapping between logical and physical addresses done by memory-management unit (MMU) l Many different MMU schemes n Simplest is relocation register – generalization of base register l Value in relocation register added to logical address n User programs never see physical addresses Operating System Concepts with Java – 8 th Edition 8. 10 Silberschatz, Galvin and Gagne © 2009

Dynamic Relocation Using a Relocation Register Operating System Concepts with Java – 8 th

Dynamic Relocation Using a Relocation Register Operating System Concepts with Java – 8 th Edition 8. 11 Silberschatz, Galvin and Gagne © 2009

Dynamic Loading n Requiring entire program and associated data to be in main memory

Dynamic Loading n Requiring entire program and associated data to be in main memory is inefficient and impractical l Size of program will be limited by available memory space n With dynamic loading a routine is not loaded until it is actually called l Routines kept on disk in relocatable format l Main program loaded at the beginning l Caller routine checks to see if the callee is in main memory l If not relocatable linker loader is called to fetch the routine from disk Operating System Concepts with Java – 8 th Edition 8. 12 Silberschatz, Galvin and Gagne © 2009

Dynamic Loading (Contd. ) n Several advantages n Better memory-space utilization n Unused routine

Dynamic Loading (Contd. ) n Several advantages n Better memory-space utilization n Unused routine is never loaded n Useful when large amounts of code are needed to handle infrequently occurring cases l E. g. , exception handling routines n No special support from the operating system is required implemented through program design l Responsibility of user programs l Oses provide library routines Operating System Concepts with Java – 8 th Edition 8. 13 Silberschatz, Galvin and Gagne © 2009

Dynamic Linking n Static linking – system libraries are combined by loader to generate

Dynamic Linking n Static linking – system libraries are combined by loader to generate binary image l Wastes disk-space and main memory because of duplication n Dynamic linking -- linking postponed until execution time n Stub is included in the image l Stub – a small piece of dummy code l Knows how to locate the appropriate memory-resident library routine Operating System Concepts with Java – 8 th Edition 8. 14 Silberschatz, Galvin and Gagne © 2009

Dynamic Linking n Stub checks if actual routine is in main memory. If not,

Dynamic Linking n Stub checks if actual routine is in main memory. If not, loads it from disk n Replaces itself with the address of the routine, and executes the routine n Operating system needed to check if routine is in processes’ memory address n Dynamic linking is particularly useful for libraries n System also known as shared libraries Operating System Concepts with Java – 8 th Edition 8. 15 Silberschatz, Galvin and Gagne © 2009

Swapping n A process can be swapped temporarily out of memory to a backing

Swapping n A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution n Backing store – fast disk large enough to accommodate copies of all memory images for all users l Must provide direct access to these memory images n Swapping in context of round-robin scheduling n Roll out, roll in – swapping variant used for priority- based scheduling algorithms l Lower-priority process is swapped out so higher-priority process can be loaded and executed Operating System Concepts with Java – 8 th Edition 8. 16 Silberschatz, Galvin and Gagne © 2009

Swapping n Swapped-out process can be swapped-in at the same location or at different

Swapping n Swapped-out process can be swapped-in at the same location or at different location l Swapping-in at different location possible only in execution-time loading n System maintains a ready queue of ready-to-run processes which have memory images on disk n Major part of swap time is transfer time n Total transfer time is directly proportional to the amount of memory swapped l Can be optimized by knowing the amount of memory used by a process n Swapping of a process that has pending I/O leads has complications Operating System Concepts with Java – 8 th Edition 8. 17 Silberschatz, Galvin and Gagne © 2009

Schematic View of Swapping Operating System Concepts with Java – 8 th Edition 8.

Schematic View of Swapping Operating System Concepts with Java – 8 th Edition 8. 18 Silberschatz, Galvin and Gagne © 2009

Contiguous Allocation n Main memory usually into two partitions: l Resident operating system, usually

Contiguous Allocation n Main memory usually into two partitions: l Resident operating system, usually held in low memory with interrupt vector l User processes then held in high memory n Relocation registers used to protect user processes from each other, and from changing operatingsystem code and data l Base register contains value of smallest physical address l Limit register contains range of logical addresses – each logical address must be less than the limit register l MMU maps logical address dynamically Operating System Concepts with Java – 8 th Edition 8. 19 Silberschatz, Galvin and Gagne © 2009

Hardware Support for Relocation and Limit Registers Operating System Concepts with Java – 8

Hardware Support for Relocation and Limit Registers Operating System Concepts with Java – 8 th Edition 8. 20 Silberschatz, Galvin and Gagne © 2009

Contiguous Allocation n Processes allocated continuous chunk of memory n Fixed-size partitions l Main

Contiguous Allocation n Processes allocated continuous chunk of memory n Fixed-size partitions l Main memory statically partitioned l Each partition holds one process n Variable partition l On-demand allocation of memory l OS keeps track of which parts are available and which parts are occupied l Hole – a block (of variable size) that is empty (available) Operating System Concepts with Java – 8 th Edition 8. 21 Silberschatz, Galvin and Gagne © 2009

Contiguous Allocation (Cont. ) n Multiple-partition allocation l Hole – block of available memory;

Contiguous Allocation (Cont. ) n Multiple-partition allocation l Hole – block of available memory; holes of various size are scattered throughout memory l When a process arrives, it is allocated memory from a hole large enough to accommodate it l Operating system maintains information about: a) allocated partitions b) free partitions (hole) OS OS process 5 process 9 process 8 process 2 process 10 process 2 Operating System Concepts with Java – 8 th Edition process 2 8. 22 process 2 Silberschatz, Galvin and Gagne © 2009

Dynamic Storage-Allocation Problem How to allocate memory to an incoming process (of size n)

Dynamic Storage-Allocation Problem How to allocate memory to an incoming process (of size n) from a list of free holes n First-fit: Allocate the first hole that is big enough n Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size l Produces the smallest leftover hole n Worst-fit: Allocate the largest hole; must also search entire list l Produces the largest leftover hole First-fit and best-fit better than worst-fit in terms of speed and storage utilization Operating System Concepts with Java – 8 th Edition 8. 23 Silberschatz, Galvin and Gagne © 2009

Implementation Issues n In theory, variable partition operates at granularity of bytes n In

Implementation Issues n In theory, variable partition operates at granularity of bytes n In practice, keeping track of very small holes is very cumbersome l Requires more memory than the size of the hole itself n Manage memory at granularity of blocks l Memory is broken into fixed sized blocks n Processes allocated contiguous blocks of memory depending upon its needs n Do not confuse with fixed-size partition Operating System Concepts with Java – 8 th Edition 8. 24 Silberschatz, Galvin and Gagne © 2009

Fragmentation n A major drawback of contiguous memory allocation n Holes sandwiched between allocated

Fragmentation n A major drawback of contiguous memory allocation n Holes sandwiched between allocated blocks that are not large enough for hosting any process n There can be many such holes – total memory in these unusable holes can be more than the needs of several processes n External Fragmentation – Entire blocks of memory sandwiched between allocated blocks n Internal Fragmentation – Leftover space in a block that is allocated to a process l Each process can have at most one partially used block Operating System Concepts with Java – 8 th Edition 8. 25 Silberschatz, Galvin and Gagne © 2009

Fragmentation n 50 -percent rule – For every N blocks of allocated memory 0.

Fragmentation n 50 -percent rule – For every N blocks of allocated memory 0. 5 N blocks lost to fragmentation l 33% of available memory lost to fragmentation n Reduce external fragmentation by compaction l Shuffle memory contents to place all free memory together in one large block n Compaction is possible only if relocation is dynamic, and is done at execution time n Expensive because it requires massive movement of memory blocks n Noncontiguous memory allocation – Paging and Segmentation Operating System Concepts with Java – 8 th Edition 8. 26 Silberschatz, Galvin and Gagne © 2009

Paging n Permits physical address space of processes to be noncontiguous l Process allocated

Paging n Permits physical address space of processes to be noncontiguous l Process allocated free blocks irrespective of where they are located (possible in different parts of main memory) n Translation from logical to physical addresses gets complicated l No longer possible by just adding a relocation value n Need to maintain information about all the blocks allocated to a process n Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8, 192 bytes) Operating System Concepts with Java – 8 th Edition 8. 27 Silberschatz, Galvin and Gagne © 2009

Paging (Contd. ) n Divide logical memory into blocks of same size called pages

Paging (Contd. ) n Divide logical memory into blocks of same size called pages n Logical address space is still contiguous !! l Adjacent logical addresses may be mapped to very different physical addresses if they fall in different pages n Logical address generated by CPU is divided into: l Page number (p) – Indicates the number of the page in which this address falls l Page offset (d) – Indicates offset within that page Operating System Concepts with Java – 8 th Edition 8. 28 Silberschatz, Galvin and Gagne © 2009

Address Translation Scheme n Suppose size of logical address space is 2 m and

Address Translation Scheme n Suppose size of logical address space is 2 m and size of each page is 2 n page number page offset p d m-n n n Page number forms higher order (m-n) bits and acts as a index to page table n Page offset forms lower order n bits. Combined with base address of the frame to obtain physical address that is sent to memory unit Operating System Concepts with Java – 8 th Edition 8. 29 Silberschatz, Galvin and Gagne © 2009

Paging Hardware Operating System Concepts with Java – 8 th Edition 8. 30 Silberschatz,

Paging Hardware Operating System Concepts with Java – 8 th Edition 8. 30 Silberschatz, Galvin and Gagne © 2009

Paging Model of Logical and Physical Memory Operating System Concepts with Java – 8

Paging Model of Logical and Physical Memory Operating System Concepts with Java – 8 th Edition 8. 31 Silberschatz, Galvin and Gagne © 2009

Paging Example 32 -byte memory and 4 -byte pages Operating System Concepts with Java

Paging Example 32 -byte memory and 4 -byte pages Operating System Concepts with Java – 8 th Edition 8. 32 Silberschatz, Galvin and Gagne © 2009

Memory Allocation Process n Keep track of all free frames n To run a

Memory Allocation Process n Keep track of all free frames n To run a program of size n pages, need to find n free frames and load program n Internal fragmentation is still a possibility Operating System Concepts with Java – 8 th Edition 8. 33 Silberschatz, Galvin and Gagne © 2009

Free Frames Before allocation Operating System Concepts with Java – 8 th Edition After

Free Frames Before allocation Operating System Concepts with Java – 8 th Edition After allocation 8. 34 Silberschatz, Galvin and Gagne © 2009

Implementation of Page Table n Page table is kept in main memory n Page-table

Implementation of Page Table n Page table is kept in main memory n Page-table base register (PTBR) points to the page table n Page-table length register (PRLR) indicates size of the page table n Every data/instruction access requires two memory accesses – one for the page table and one for the data/instruction l Memory access latency becomes high n Translation look-aside buffers (TLBs) – Special purpose hardware lookup cache Operating System Concepts with Java – 8 th Edition 8. 35 Silberschatz, Galvin and Gagne © 2009

Paging Hardware With TLB Operating System Concepts with Java – 8 th Edition 8.

Paging Hardware With TLB Operating System Concepts with Java – 8 th Edition 8. 36 Silberschatz, Galvin and Gagne © 2009

Effective Access Time n Memory translation is a TLB hit if page number found

Effective Access Time n Memory translation is a TLB hit if page number found in TLB (i. e. , the page-frame lookup can be completed by TLB) l TLB miss otherwise n TLB hit rate ( ) – fraction (%age) of lookups that are TLB hits n Suppose TLB Lookup takes time units and accessing main memory takes up λ time units n Effective Access Time (EAT) EAT = ( +λ) + (2λ + )(1 – ) Operating System Concepts with Java – 8 th Edition 8. 37 Silberschatz, Galvin and Gagne © 2009

TLB & ASIDs n Some TLBs have Address-Space Identifiers (ASIDs) that allow them to

TLB & ASIDs n Some TLBs have Address-Space Identifiers (ASIDs) that allow them to contain page-frame mappings from multiple processes n Each TLB entry associated with ASID that uniquely identifies the associated process n When TLB gets a page number for lookup, it checks whether the ASID of the lookup request matches with the ASID of the page number in the TLB n If not, the page number belongs to some other process and entry cannot to used for translation and hence treated as TLB miss n If TLB does not have ASID, TLB needs to be flushed each time there is a context-switch Operating System Concepts with Java – 8 th Edition 8. 38 Silberschatz, Galvin and Gagne © 2009

Shared Pages n Shared code l One copy of read-only (reentrant) code shared among

Shared Pages n Shared code l One copy of read-only (reentrant) code shared among processes (i. e. , text editors, compilers, window systems). l Shared code must appear in same location in the logical address space of all processes. n Private code and data l Each process keeps a separate copy of the code and data. l The pages for the private code and data can appear anywhere in the logical address space. Operating System Concepts with Java – 8 th Edition 8. 39 Silberschatz, Galvin and Gagne © 2009

Shared Pages Example Operating System Concepts with Java – 8 th Edition 8. 40

Shared Pages Example Operating System Concepts with Java – 8 th Edition 8. 40 Silberschatz, Galvin and Gagne © 2009

Structure of the Page Table n How is page table organized in main memory?

Structure of the Page Table n How is page table organized in main memory? n Simpler if entire page table fits in one frame n What if does not? l Remember memory allocation is non-contiguous l Page table itself will be in non-contiguous frames n Hierarchical Paging l Page table organized in multiple levels n Hashed Page Tables n Inverted Page Tables Operating System Concepts with Java – 8 th Edition 8. 41 Silberschatz, Galvin and Gagne © 2009

Two-Level Page-Table Scheme Operating System Concepts with Java – 8 th Edition 8. 42

Two-Level Page-Table Scheme Operating System Concepts with Java – 8 th Edition 8. 42 Silberschatz, Galvin and Gagne © 2009

Two-Level Paging Example n A logical address (on 32 -bit machine with 1 K

Two-Level Paging Example n A logical address (on 32 -bit machine with 1 K page size) is divided into: l a page number consisting of 22 bits l a page offset consisting of 10 bits n Since the page table is paged, the page number is further divided into: l a 12 -bit page number l a 10 -bit page offset n Thus, a logical address is as follows: page number p 2 pi page offset d 12 10 10 where pi is an index into the outer page table, and p 2 is the displacement within the page of the outer page table Operating System Concepts with Java – 8 th Edition 8. 43 Silberschatz, Galvin and Gagne © 2009

Address-Translation Scheme Operating System Concepts with Java – 8 th Edition 8. 44 Silberschatz,

Address-Translation Scheme Operating System Concepts with Java – 8 th Edition 8. 44 Silberschatz, Galvin and Gagne © 2009

Three-level Paging Scheme Operating System Concepts with Java – 8 th Edition 8. 45

Three-level Paging Scheme Operating System Concepts with Java – 8 th Edition 8. 45 Silberschatz, Galvin and Gagne © 2009

Segmentation n In paging, there is a disconnect between users view of memory and

Segmentation n In paging, there is a disconnect between users view of memory and actual physical memory n From a user’s perspective, a program is a collection of distinct parts (e. g. , main program, libraries, stack, etc. ) n Segmentation – A memory-management scheme that supports user view of memory n A program is a collection of segments A segment is a logical unit such as: main program procedure, method, object, local variables, global variables, stack, arrays, symbol table etc. l Operating System Concepts with Java – 8 th Edition 8. 46 Silberschatz, Galvin and Gagne © 2009

User’s View of a Program Operating System Concepts with Java – 8 th Edition

User’s View of a Program Operating System Concepts with Java – 8 th Edition 8. 47 Silberschatz, Galvin and Gagne © 2009

Segmentation (Contd. ) n Logical address space is a collection of segments n Each

Segmentation (Contd. ) n Logical address space is a collection of segments n Each segment has name and length For implementation, each segment has a unique number n Logical addresses specify segment name (number) and offset <segment number, offset> l Operating System Concepts with Java – 8 th Edition 8. 48 Silberschatz, Galvin and Gagne © 2009

Logical View of Segmentation 1 4 1 2 3 2 4 3 user space

Logical View of Segmentation 1 4 1 2 3 2 4 3 user space Operating System Concepts with Java – 8 th Edition physical memory space 8. 49 Silberschatz, Galvin and Gagne © 2009

Segmentation Architecture n Segment table – maps two-dimensional physical addresses; each table entry has:

Segmentation Architecture n Segment table – maps two-dimensional physical addresses; each table entry has: l base – contains the starting physical address where the segments reside in memory l limit – specifies the length of the segment n Segment-table base register (STBR) points to the segment table’s location in memory n Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR n Paging and segmentation both have advantages and disadvantages. Some architectures (Intel Pentium) supports both. Operating System Concepts with Java – 8 th Edition 8. 50 Silberschatz, Galvin and Gagne © 2009

Segmentation Hardware Operating System Concepts with Java – 8 th Edition 8. 51 Silberschatz,

Segmentation Hardware Operating System Concepts with Java – 8 th Edition 8. 51 Silberschatz, Galvin and Gagne © 2009

Example of Segmentation Operating System Concepts with Java – 8 th Edition 8. 52

Example of Segmentation Operating System Concepts with Java – 8 th Edition 8. 52 Silberschatz, Galvin and Gagne © 2009

End of Chapter 8 Operating System Concepts with Java – 8 th Edition 8.

End of Chapter 8 Operating System Concepts with Java – 8 th Edition 8. 53 Silberschatz, Galvin and Gagne © 2009