Lecture 7 Memory Management 1 Lecture Contents 1. Memory Management Requirements 2. Memory Partitioning 3. Paging 4. Segmentation 2 Memory • Memory is an array of words or bytes, each with its own address. • Main memory and the registers built into the processor itself are the only storage that the CPU can access directly. 3 1. Memory Management Requirements Five requirements of memory management are 1. Relocation 2. Protection 3. Sharing 4. Logical organisation 5. Physical organisation 4 1. Relocation • In a multiprogramming system, the available main memory is generally shared among a number of processes. • The programmer does not know where the program will be placed in memory when it is executed. - It may be swapped to disk and return to main memory at a different location (relocated). • Memory references in program code must be translated to the actual physical memory address 5 Memory Management Terms 6 Addressing 7 2. Protection • We need to make sure that each process has a separate memory space. • Processes should not be able to reference memory locations in another process for reading or writing purposes without permission • It is impossible to check absolute addresses at compile time to assure protection because location of a program in main memory is unpredictable 8 2. Protection • Hence all memory references generated by a process must be checked at run time (i.e., at the time of execution) to ensure that they refer only to memory space allocated to that process • Note that the memory protection requirement must be satisfied by the processor (hardware) rather than the operating system (software) because the OS cannot anticipate all of the memory references that a program will make. 9 2. Protection • We can provide protection by using two registers: - base register: specifies starting address - limit register: specifies the memory size. 10 3. Sharing • Any protection mechanism must have the flexibility to allow several processes to access the same portion of main memory • Processes that are cooperating on some task may need to share access to the same data structure • The memory management system must therefore allow controlled access to shared areas of memory without compromising essential protection 11 4. Logical Organization • Main memory is organized as a linear (or onedimensional) address space consisting of a sequence of bytes or words • Most programs are organized into modules • Advantages of modularization - Modules can be written and compiled independently, with all references from one module to another resolved by the system at run time 12 4. Logical Organization - Different degrees of protection (e.g., read-only, execute-only, read-write) can be given to different modules. - Modules can be shared among processes 13 5. Physical Organization • Cannot leave the programmer with the responsibility to manage memory • The main memory available for a program plus its data may be insufficient - Overlaying allows various modules to be assigned the same region of memory but overlaying is time-consuming to program • A programmer does not know at the time of coding how much space will be available or where that space will be 14 Lecture Contents 1. Memory Management Requirements 2. Memory Partitioning 3. Paging 4. Segmentation 15 Memory Management Techniques Six memory management techniques are 1. Fixed Partitioning 2. Dynamic Partitioning 3. Simple (or pure) Paging 4. Simple (or pure) Segmentation 5. Virtual Memory Paging 6. Virtual Memory Segmentation 16 2. Memory Partitioning 17 1. Fixed-Partitioning Scheme • Equal-size partitions - Any process whose size is less than or equal to the partition size can be loaded into an available partition 18 Equal-size Partitions 19 Two Problems of Equal-size Partitions • A program may too big to fit into a partition. - The programmer must design the program with the use of overlays so that only a portion of the program needs be in main memory at any one time • Main memory utilization is extremely inefficient - Any program, no matter how small, occupies an entire partition. This results in internal fragmentation (i.e., unused memory internal to a partition) 20 1. Fixed-Partitioning Scheme • Unequal-size partitions - Lessens both problems (i.e., program too big or small) - but doesn’t solve the problems completely 21 Unequal-size Partitions • In this figure, - Programs up to 16 Mbytes can be accommodated without overlay - Smaller programs can be placed in smaller partitions, reducing internal fragmentation (i.e., wasted memory internal to a partition). 22 Q&A • Q: In a fixed-partitioning scheme, what are the advantages of using unequal-size partitions? • A: The advantages of using unequal-size fixed partitions are 1. It is possible to provide one or two quite large partitions and still have a large number of partitions. The large partitions can allow the entire loading of large programs. 2. Internal fragmentation is reduced because a small program can be put into a small partition. 23 Placement Algorithm • For equal-size partitions - Placement of processes in memory is trivial (no options) • For unequal-size partitions - can assign each process to the smallest partition within which it will fit - advantage of this approach is that processes are always assigned in such a way as to minimize internal fragmentation (i.e., wasted memory within a partition). 24 Remaining Problems with Fixed Partitions • The number of active processes is limited by the system (i.e., limited by the pre-determined number of partitions) • Small processes will not use the partition space efficiently (i.e., internal fragmentation still exists) • The use of fixed partitioning is almost unknown today 25 2. Dynamic Partitioning (DP) • With dynamic partitioning, the partitions are of variable length and number • When a process is brought into main memory, it is allocated exactly as much memory as it requires and no more 26 Dynamic Partitioning Example OS (8M) P2 P1 (14M) (20M) Empty (6M) Empty P4(8M) P2 (56M) (14M) Empty (6M) P3 (18M) Empty (4M) Refer to Figure 7.4 27 2. Dynamic Partitioning (DP) • DP starts out well, but eventually it leads to a situation in which there are a lot of small discontiguous holes in memory. • As time goes on, memory becomes more and more fragmented, and memory utilization declines. This phenomenon is referred to as external fragmentation (i.e., wasted memory external to all partitions). That is, there is enough total memory space to satisfy a request but the available spaces are not contiguous. Refer to Figure 7.4 28 2. Dynamic Partitioning (DP) • External fragmentation can be resolved using compaction (e.g., defragmenter). - From time to time, OS moves processes so that they are contiguous and so that all of free memory is together in one block - Compaction is a time consuming procedure and wasteful of processor time Refer to Figure 7.4 29 Q&A • Q: What is the difference between internal and external fragmentation? • A: - Internal fragmentation is a phenomenon associated with fixed partitioning. Internal fragmentation refers to the wasted/unused space/memory internal to a partition (i.e., wasted memory within a partition) due to the fact that the block of data loaded is smaller than the partition. 30 Q&A - External fragmentation is a phenomenon associated with dynamic partitioning. External fragmentation refers to the fact that many small discontiguous areas of main memory external to any partition are wasted (i.e., wasted spaces external to all partitions, or wasted memory outside all partitions). That is, there is enough total memory space to satisfy a request but the available spaces are not contiguous. 31 Placement Algorithm • When it is time to load or swap a process into main memory, and if there is more than one free block of memory of sufficient size, then OS must decide which free block to allocate to the process •Placement algorithms are best-fit, worst-fit, nextfit, and first-fit. 32 Placement Algorithm • Best-fit placement algorithm - searches entire list and chooses the block that is closest in size to the request - is usually the worst performer because leftover hole is small - since smallest block is found for process, main memory is quickly segmented by blocks too small to satisfy memory allocation requests - memory compaction must be done more frequently than with the other algorithms 33 Placement Algorithm • Worst-fit placement algorithm - Searches entire list and chooses the block that is largest in size to the request - produces largest leftover hole, which may be more useful than smaller leftover hole created by best-fit approach. 34 Placement Algorithm • Next-fit placement algorithm - scans memory from the location of the last placement, and chooses the next available block that is large enough - tends to produce slightly worse results than the first-fit replacement algorithm - will more frequently lead to an allocation from a free block at the end of memory 35 Placement Algorithm • Next-fit placement algorithm - largest block of free memory, which usually appears at the end of the memory space, is quickly broken up into small fragments - compaction may be required more frequently with next-fit 36 Placement Algorithm • First-fit placement algorithm - scans memory from the beginning and chooses the first available block that is large enough - is not only the simplest but usually the best and fastest as well 37 Placement Algorithm ? 19 M ← → 19 M Next fit Worst fit 38 Buddy System • Both fixed and dynamic partitioning schemes have drawbacks. - fixed partitioning scheme limits the number of active processes and may use space inefficiently if there is a poor match between available partition sizes and process sizes. - dynamic partitioning scheme is more complex to maintain and includes overhead of compaction. • An interesting compromise is buddy system 39 Buddy System • Entire space available for allocation is treated as a single block of size 2n (e.g., n = 10, 2n = 1024) • If a request of size m, where 2n-1 < m ≤ 2n, is made, then entire block is allocated • Otherwise, block is split into two equal buddies - Process continues until smallest block greater than or equal to m is generated 40 Example of Buddy System 41 Tree Representation of Buddy System B= 42 Buddy System • Buddy system is a reasonable compromise to overcome the disadvantages of both the fixed and variable partitioning schemes • In contemporary OSs, virtual memory based on paging and segmentation is superior. 43 Buddy System • Buddy system has found application in parallel systems as an efficient means of allocation and release for parallel programs. A modified form of the buddy system is used for UNIX kernel memory allocation 44 Relocation • When program is loaded into memory, the actual (or absolute) memory locations are determined • A process may occupy different partitions which means different absolute memory locations during execution. This is due to - swapping out and in (by swapper - swaps entire process, not pager - swap a page) - compaction 45 Types of Addresses • Logical address (virtual address): is a reference to a memory location independent of the current assignment of data to memory - A translation from a logical address to a physical address must be made before the memory access can be achieved. - Logical address is generated by CPU. 46 Types of Addresses • Physical address (or absolute address): is an actual location in main memory. - Physical address is managed by memorymanagement unit (MMU) • Relative address: is expressed as a location relative to some known point, usually a value in a processor register. 47 Types of Addresses • Base register is also called relocation register. 48 Address Translation > ≤ 49 Address Translation • Base register indicates starting address in main memory of the program • Bounds register indicates ending address in main memory of the program • Value in base register is added to relative address to produce absolute address. • Resulting address is compared to value in bounds register. If the address is within bounds, then instruction execution may proceed. Otherwise, an interrupt is generated to OS for error processing 50 Lecture Contents 1. Memory Management Requirements 2. Memory Partitioning 3. Paging 4. Segmentation 51 3. Paging • Main memory is divided into equal, fixed-size blocks. Each block is called a frame. • Each program stored on disk is divided into equal, fixed-size blocks. Each block is called a page. • Page size equals frame size. • In paging scheme, wasted space in memory for each process is due to internal fragmentation consisting of only a fraction of the last page of a process. There is no external fragmentation. 52 3. Paging 53 3. Paging • Traditionally, support for paging has been handled by hardware. However, recent designs have implemented paging by closely integrating the hardware and operating system, especially on 64bit microprocessors. • OS maintains a page table for each process. Page table contains the frame location for each page in the process. That is, the page table contains the base address of each page in physical memory 54 3. Paging • A logical address (generated by CPU) is a reference to a memory location independent of the current assignment of data to memory. - A translation to a physical address must be made before the memory access can be achieved. - Logical address consists of a page number p and page offset within the page d (page#, offset) = (p, d). 55 3. Paging • A physical address (or absolute address managed by MMU) is an actual location in main memory. Physical address consists of a frame number f and frame offset d within the frame (frame#, offset) = (f, d). • A relative address is a particular example of logical address. That is, it is the (n + m)-bit decimal equivalent of p2||d2 or s2||d2. 56 Paging Hardware . 57 Page Table 58 Assignment of Processes and Free Frames A.0 A.1 A.2 A.3 D.0 B.0 D.1 B.1 D.2 B.2 C.0 C.1 C.2 C.3 D.3 D.4 59 3. Paging • Paging is similar to fixed partitioning. • The differences are - partitions are rather small - a program may occupy more than one partition - and these partitions need not be contiguous 60 16-bit Logical and Relative Addresses Page size is 1K (byte) = 210 = 1024 bytes So m = 10 bits are needed for offset and n = 6 bits are needed for page number 61 Logical-to-Physical Translation in Paging = 1502 0111011110 478 frame 6 6622 = 62 Logical-to-Physical Translation in Paging • Consider an address of n + m bits, where leftmost n bits are page number, rightmost m bits are offset. In our example, n = 6 and m = 10. 63 Logical-to-Physical Translation in Paging • The following steps are needed for address translation: - Extract the page number as the leftmost n bits of the logical address. - Use the page number as an index into the process page table to find the frame number k. 64 Logical-to-Physical Translation in Paging - The starting physical address of the frame is k × 2m, and the physical address of the referenced byte is that number plus the offset. This physical address needs not be calculated; it is easily constructed by appending the offset to the frame number. • Example: logical address = 0000010111011110, frame number k = 6 = 000110. → physical address = 6 × 210 + 478 = 6144 + 478 = 6622 = 0001100111011110 65 Lecture Contents 1. Memory Management Requirements 2. Memory Partitioning 3. Paging 4. Segmentation 66 4. Segmentation • A program can be divided into segments - Segments may vary in length - There is a maximum segment length (2m) • Logical address consist of two parts: a segment number s and segment offset d (i.e., offset within the segment) (segment#, offset) = (s, d). 67 4. Segmentation • Segmentation is similar to dynamic partitioning • The difference is that with segmentation a program may occupy more than one partition, and these partitions need not be contiguous • Segmentation eliminates internal fragmentation but, like dynamic partitioning, it suffers from external fragmentation. However, because a process is broken up into a number of smaller pieces, the external fragmentation should be less 68 4. Segmentation 69 Segmentation 70 Segmentation = Relative address: 4848 752 8224 1950 > 752 maximum segment size is 212 = 4096 bytes. Thus, m = 12 bits, n = 4 bits 8976 71 Logical-to-Physical Translation in Segmentation • Consider an address of n + m bits, where leftmost n bits are segment number and rightmost m bits are the offset. In our example, n = 4 and m = 12. Thus the maximum segment size is 212 = 4096. • The following steps are needed for address translation: - Extract segment number as leftmost n bits of logical address (e.g., 0001|001011110000). 72 Logical-to-Physical Translation in Segmentation - Use segment number as an index into process segment table to find starting physical address of the segment (i.e., the value of base). - Compare the offset, expressed in rightmost m bits, to the length of the segment. If offset is greater than or equal to the segment length, the address is invalid. - Desired physical address is sum of starting physical address of the segment and the offset. 73 Summary • One of the most important and complex tasks of OS is memory management. • Main memory is a resource to be allocated to and shared among a number of active processes. • To use processor and I/O facilities efficiently, it is desirable to maintain as many processes in main memory as possible. • It is desirable to free programmers from memory restrictions in program development. 74 Summary • The basic tools of memory management are paging and segmentation. - With paging, each process is divided into relatively small, fixed-size pages. - Segmentation provides for the use of pieces of varying size. • It is also possible to combine paging and segmentation in a single memory management scheme. 75 Review Questions Q. What are the differences among a frame, a page, and a segment? A. • In a paging system, main memory is divided into equal, fixed-size blocks. Each block is called a frame. 76 Review Questions • In a paging system, a program stored on disk is divided into equal, fixed-size blocks. Each block is called a page. The sizes of a page and a frame are equal. Thus, one page can be loaded into one frame exactly. 77 Review Questions • In a segmentation system, a program stored on disk is divided into variable-size blocks. Each block is called a segment. Although there is a maximum segment length, it is not required that all segments of all programs be of the same length. 78 chmod Command • Assume that the access permissions of the file hello.exe are -rw-------. • How to set permissions r (read), w (write), and x (execute) on the file hello.exe for a user - chmod u=rwx hello.exe - chmod u+x hello.exe - chmod 700 hello.exe 79 chmod Command 80
© Copyright 2026 Paperzz