730 likes | 750 Views
Chapter 10 Memory Management Part_1. Overview Basic Concepts. The major tasks of the memory manger are the allocation and deallocation of main memory . Main Memory is an important affect of system performances and functions of the OS
E N D
Overview Basic Concepts • The major tasks of the memory manger are the allocation and deallocation of main memory . • Main Memory is an important affect of system performances and functions of the OS • In early OS with Multiprogramming, memory was divided into a number of partitions/blocks to allocate processes
Memory Management • Swapping • Overlaying – not used any more • Partitioning • Paging • Segmentation • Virtual memory- program can reference more memory than what is available in main (physical) memory. In computing, virtual memory is a memory management technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.
Physical and Virtual Memory • Physical addressing means that your program actually knows the real layout of RAM. When you access a variable at address 0x8746b3, that's where it's really stored in the physical RAM chips. • Virtual addressing, all application memory accesses go to a page table, which then maps from the virtual to the physical address. So every application has its own "private" address space, and no program can read or write to another program's memory. This is called segmentation. • Virtual addressing has many benefits. It protects programs from crashing each other through poor pointer manipulation, etc. Because each program has its own distinct virtual memory set, no program can read another's data - this is both a safety and a security plus.
VM • Virtual memory also enables paging, where a program's physical RAM may be stored on a disk (or, now, slower flash) when not in use, then called back when an application attempts to access the page. • Also, since only one program may be resident at a particular physical page, in a physical paging system, either a) all programs must be compiled to load at different memory addresses or b) every program must use Position-Independent Code, or c) some sets of programs cannot run simultaneously.
Process Address Space • A logical address is a reference to some location of a process • The set of logical addresses that a process references in its code. • When memory is allocated to the process, its set of logical addresses will be bound to physical addresses.
Logical and Physical Addresses High-Level view of the mapping of logical address to the physical address of two process (P1, P2) • Three types of address are used in a program before and after memory is allocated. • Symbolic address • (used in Source code, like variables names, const, etc.) • 2. Relative address • (a compiler converts symbolic address into relative address) • 3. Physical address • (The final address generated when a program is loaded and ready to execute in physical memory)
Binding • The association of instructions and data to memory addresses • Can occur at any of the following steps • Compile time • Load time • Execution time
Phases of a Program • Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes. • Linkage time: Program is combined with other modules. • Load time: Program is loaded into memory. • 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).
Managing the Address Space • The compiler or assembler generates the program as a relocatable object module • The linker combines several modules into a load module • During memory allocation, the loader places the load module in the allocated block of memory • The loader binds the logical address to a physical address
Dynamic Loading • Routines or modules to be used by a program are not loaded until called • All routines are stored on a disk in relocatable form • Better memory-space utilization; unused routines are never loaded. • Useful when large amounts of code are needed to handle infrequently occurring cases. • No special support from the operating system is required to be implemented through program design.
Dynamic Linking • Linking postponed until execution time. • Example: Dynamic Linked Libraries (DLL) • 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 the routine is in the processes’ memory address.
Memory-Management Unit (MMU) • Hardware device that maps virtual 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. • The user program deals with logical addresses; it never directly references the real physical addresses.
Static Relocation • A process executes at a specific address. • Addresses are assigned at load time. • Relocation modifies instructions that address locations in memory. • Static relocation does not solve protection.
Dynamic Relocation • Addresses can be changed at execution time. • Facilitates Protection using: • Addressing via Base (& Limit) Register. • Additional benefit, program may be moved during execution.
Memory Protection Necessary because malicious or incompetent programs may access the program or data space of another program and contaminate it.
Contiguous Memory Allocation • Main memory is divided into several partitions • A partition is a contiguous blockof memory that can be allocated to an individual process • The degree of multiprogramming is determined by the number of partitions in memory.
Multiple Partitions • Fixed partitions (static) – the number and sizes of the partitions do not change • Variable partitions (dynamic) – partitions are created dynamically according to: • available memory • the memory requirements of processes
Fixed Partitions • Memory is divided into fixed-sized partitions. These are not normally of the same size. • The number and the size of the partitions are fixed. • One partition is allocated to each active process in the multiprogramming set. • There is one special partition, the system partition, in which the memory-resident portion of the operating system is always stored.
Fixed Partitions Five active process that have been allocated partitions. Partition 5 from address 850K to address 1100K Partition 4 from address 700K to address 850K Partition 3 from address 450K to address 700K Partition 2 from address 300K to address 450K Partition 1 is configured from address100K to address 300K Fixed partition is configure from address 0 to address 100K
Fragmentation in Fixed Partition Fragmentation problem • Internal fragmentation - A partition is only partially used. • A partition is available, but not large enough for any waiting progress.
Holes in Memory • Hole– means a contiguous block of available memory; holes of various size are scattered throughout memory. • When a process requests memory, it is allocated memory from a hole large enough to accommodate it. • Operating system maintains data about: • allocated partitions • Available memory blocks (holes)
Memory Allocation to P7 Smaller hole of 75K If a hole is sufficiently large, it can be allocated to a process of the same or smaller memory Assume P7 has a size of 200K bytes Assume P5 has a size of 200K
De-allocating Memory to P5 Memory state after the system deallocates the memory from process P5 and the process is removed from the memory. There fore, new hole is created of size 200K
Advantages of Dynamic Partitions • In dynamic partitioning, the holes represent the memory that is available, and if they are too small, they cannot be allocated. They represent external fragmentation. • Memory utilization is generally better for variable-partition schemes. • There is little or no internal fragmentation. • There can be external fragmentation.
Memory Allocation How to satisfy a process 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 the smallest leftover hole. • Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole. First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
Swapping • A process can be swappedtemporarily out of memory to a backing store, and then brought back into memory for continued execution • Total physical memory space of processes can exceed physical memory • A process can be swappedtemporarily out of memory to secondary storage, and then loaded into memory again to resume execution. • Secondary storage – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images.
Pages • A page is a unit of logical memory of a program • A frame is a unit of physical memory • All pages are of the same size • All frames are of the same size • A frame is of the same size as a page
Paging • Physical memory is divided into fixed-sized blocks called frames (size is power of 2 ). • Logical memory is divided into blocks of same size called pages. • Size of process is measured in the number of pages. • Physical memory is divided into small fixed-size blocked of physical memory called frames. • If a 15-page process is waiting for memory, the system needs to find any 15 frames to allocate to this process • The size of a page is a power of two
Example of Page shows an example with paging in which the physical memory is divided into 32 frames. f0 to f31, and not all the frames have been allocated. • The frames with page of a process are not all contiguous (Used). • Example: • f5 allocated to page 0 of P3; • f20 allocated to page 1 of P6
Paging(2) • The OS keeps track of all free (available) frames, and allocated frames in the page table. • To run a program of size n pages, the OS needs n free frames to load program. • The OS sets up a page table for every process • The page table is used for converting logical addresses to physical addresses. • There is a small amount of internal fragmentation.
Logical vs Physical Memory • Logical memory corresponds to the user’s view of memory • Physical memory is the actual contents and addresses of memory • The user’s view of memory is mapped onto physical memory • This mapping is done on every reference to logical memory
Block Mapping Mapping Virtual addresses to real addresses
Logical Address (2) Address generated by the compiler/assembler is divided into: • Page number(p)– used as an index into a pagetable (which contains base address of each page in physical memory). • Page offset(d) – the relative address in the page. • This pair of numbers will be converted to the physical memory address that is sent to the memory unit.
Example of a Logical Address 20 –bit address is a system that used 1k pages Higher 10 bits are used to reference the page number Lower bits of the address 2 power 10 = 1024 Page number = 2 Offset = 478
Physical Address • When the system allocates a frame to a page, it translates this logical address into a physical address that consists of a frame number and the offset. • For this, the system needs to know the correspondence of a page of a process to a frame in physical memory and it uses a page table
Example of a Physical Address hexa decimal = Group each bit in 4 bits Frame number = 4 Offset = 1DEh
Segmentation • Segmentation is a technique for breaking memory up into logical pieces • Each “piece” is a grouping of related information • data segments for each process • code segments for each process • data segments for the OS • etc. • Like paging, use virtual addresses and use disk to make memory look bigger than it really is • Segmentation can be implemented with or without paging
Segmentation P1 data P2 code print function P2 data P1 code OS Code OS data OS stack logical address space
Addressing Segments • Let’s first assume no paging in the system • User generates logical addresses • These addresses consist of a segment number and an offset into the segment • Use segment number to index into a table • Table contains the physical address of the start of the segment • often called the base address • Add the offset to the base and generate the physical address • before doing this, check the offset against a limit • the limit is the size of the segment
Addressing Segments Page number(p)– used as an index into a pagetable (which contains base address of each page in physical memory). Page offset(d) – the relative address in the page. This pair of numbers will be converted to the physical memory address that is sent to the memory unit. Limit Is the size of segment Base Often called physical Address Offset Start User generates logical address segment table Physical Address yes + o < limit no error
Segmentation Hardware • Sounds very similar to paging • Big difference – segments can be variable in size • As with paging, to be effective hardware must be used to translate logical address • Most systems provide segment registers • If a reference isn’t found in one of the segment registers • trap to operating system • OS does lookup in segment table and loads new segment descriptor into the register • return control to the user and resume • Again, similar to paging
Program Modules Segments are modules of a program: • main program • Procedure • function • local variables, global variables • common block • stack • symbol table, arrays
Segmentation and Memory Allocation • Before a program can execute, all its segments need to be loaded into memory. • For every segment, the operating system needs to find a contiguous block of available memory to allocate to the segment.
Example of Segmentation • The logical address of a process consist of two parts: • segment number • offset. Segment table for the processes of 5 segments 5 segments is set up for process