Allocating Memory 1 Readings Silberschatz: 8.3, 8.4 2 Outline Contiguous Memory Allocation Paging Concepts 3 Introduction In this section we will discuss approaches to allocating memory to processes Contiguous Paging 4 Contiguous Memory Allocation 5 Contiguous Memory Allocation We will start out with the most basic method used that allows multiple processes to reside in memory With contiguous memory allocation each process is contained in a single section of memory that is contiguous 6 Fixed Partitioning Any program, no matter how small, occupies an entire partition. This represents internal fragmentation. Operating System 8M 8M 8M 8M 8M 7 Fixed Partitioning Unequal-size partitions Lessens the problem with equal-size partitions Operating System 8M 2M 4M 6M 8M 8M 12 M 8 Placement Algorithm with Partitions Equal-size partitions Since all partitions are of equal size, it does not matter which partition is used Unequal-size partitions Each process can be assigned to the smallest partition within which it will fit There is a queue for each partition or a queue for all partitions Processes are assigned in such a way as to minimize wasted memory within a partition 9 One Process Queue per Partition Operating System New Processes 10 One Common Process Queue When its time to load a process into main memory the smallest available partition that will hold the process is selected Operating System New Processes 11 Simple MMU for Address Mapping The base register holds the smallest physical memory address The limit register specifies the range These registers can be loaded only by the operating system 12 Simple MMU for Address Mapping Simple mapping from logical addresses to physical Relocation register: Value is added to every address generated by a user process 13 Questions Single Queue: What is the problem with taking the first job that fits into the partition? What is the problem with taking the largest job that fits into the partition? Multiple Queues: What is the disadvantage? 14 Multiprogramming with Fixed Partitions Was used by OS/360 on large IBM mainframes for many years Incoming jobs were queued until a suitable partition was available Today no modern OS uses fixed partitions 15 Dynamic Partitioning 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 Compaction is required to obtain a large block at the end of memory Shift processes so they are contiguous and all free memory is in one block 16 Example Dynamic Partitioning Operating System Operating System Process 1 320 K Process 2 224 K Process 3 288 K Process 1 Operating System 320 K 224 K Process 1 Process 4 320 K 128 K 96 K 64 K Process 3 288 K Process 3 288 K 64 K 64 K 17 Example Dynamic Partitioning Operating System Operating System 320 K Process 2 224 k 96 K Process 4 128 K Process 4 96 K Process 3 288 K 64 K 128 K 96 K Process 3 288 K 64 K 18 Dynamic Partitioning Placement Algorithm Operating system must decide which free block to allocate to a process Best-fit algorithm Choose the block that is closest in size to the request This has the worst overall performance The smallest block is found for a process • The smallest amount of fragmentation is left • Memory compaction must be done more often 19 Dynamic Partitioning Placement Algorithm First-fit algorithm Starts scanning memory from the beginning and chooses the first available block that is large enough. Next-fit Starts scanning memory from the location of the last placement and chooses the next available block that is large enough 20 Paging 21 Introduction Previous memory schemes were characterized by Need for compaction External fragmentation Paging is a memory management technique that avoids compaction and external fragmentation 22 Paging Partition physical memory into small equal- size chunks and divide a process’s logical memory into the same size chunks The chunks of a process are called pages and chunks of memory are called frames When a process is to be executed, its pages are loaded (from a file system) into any available memory frames A page/frame is associated with a set of memory addresses 23 Paging An operating system maintains a page table for each process Contains the frame location for each page in a process 24 Page Example Frame Number 0 1 0 0 0 7 1 1 1 8 2 2 2 9 3 3 Process B Process A Page Tables •Each page table entry consists of a frame number •The contents of the page fill that frame number 2 3 A0 A1 A2 A3 4 5 6 7 8 9 B0 B1 B2 10 11 12 13 14 25 Address Translation Scheme Address generated by CPU is divided into: Page number (p) – used as an index into a page table which contains start address of each page in physical memory Page offset (d) – combined with start address of the page to define the physical memory address that is sent to the memory unit 26 Address Translation Scheme Assume that the number of logical address bits is m Organization of an address is the following page number page offset p d m-n n 27 Address Translation Scheme If the number of logical address bits is m and the number of bits in the offset is n then Logical address space is 2m Page size (number of entries in page) is 2n Example: m = 4, n = 2 Number of addresses is 16 Number of entries in a page is 4 28 Paging Hardware – Memory Management Unit (MMU) 29 Paging Hardware The CPU issues a logical address (remember that all addresses are binary) The hardware extracts the page number, p, and the page offset d The page number, p, is used to index the page table The entry in the page table consists of the frame number, f The actual address is the concatenation of the bits that make up f and d. 30 Paging Example m=4 n=2 Page size of 4 bytes Physical memory of 32 bytes (8 pages) Binary representations: 0 00 1 01 2 10 3 11 31 Paging Example First a little reminder. All addresses are in bits. The example on the previous page gives the base 10 equivalent of binary numbers 0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111 1011 32 Example Logical address in binary form is 0000 From the bits we extract the page number as 00 and the offset as 00 Page number 00 corresponds to the first entry of the page table. There we find that the corresponding frame is 5 (or 0101) The physical address produced is the concatenation of the bits of the frame number and the offset • 010100 In base 10 we find that the physical address is 20 33 Example Logical address in binary form is 0101 From the bits we extract the page number as 01 and the offset as 01 Page number 01 corresponds to the second entry of the page table. There we find that the corresponding frame is 6 (or 0110) The physical address produced is the concatenation of the bits of the frame number and the offset • 011001 In base 10 we find that the physical address is 25(6*4+1) 34 Exercise What do you think logical address 13 maps to? 35 Observations Let’s look at logical addresses: 0000 (0), 0001 (1), 0010(2) and 0011(3) The first two bits (page number) are the same This means that these logical addresses will be in the same frame. The position in the frame is determined by the offset For a process its pages can be in any order For our example page 1 is in a frame that appears “later” than the frame associated with page 2 36 Observations The logical address space and the physical address space DO NOT have to be the same size The logical address space can be larger then the physical address space (more on this later) 37 Paging No external fragmentation Any free frame can be allocated to a process that needs it There may be some internal fragmentation The memory requirements may not coincide with page boundaries The last frame allocated may not be completely full 38 Paging Internal fragmentation is on average one half page per process Larger page sizes means more wasted space Smaller page sizes means larger tables 39 Summary This section introduced the concept of paging in memory 40