CS211: Programming and Operating Systems Lectures 21 : Memory Management (ii) Thu 19 Nov 2009 CS211 Lectures 21 : Memory Management (ii) 1/23
This afternoon... 1 Recall... Memory management 2 Contiguous Memory Allocation 3 Fragmentation 4 Paging Implementation of Page Table 5 Virtual Memory 6 Demand Paging 7 Page Fault CS211 Lectures 21 : Memory Management (ii) 2/23
Recall... Memory management On Monday we discussed 1 Logical versus Physical Address Space 2 Swapping Today: 1 Contiguous Memory Allocation 2 Fragmentation 3 Paging CS211 Lectures 21 : Memory Management (ii) 3/23
Contiguous Memory Allocation In a multiprogramming environment, memory space is occupied by the operating system and a collection of user processes. In order to maximise the degree of multiprogramming on the system, the OS will load as many procs into memory as there is space for. When new procs arrive in the Input queue, it picks the first one and loads it into memory unless there is not enough room to accommodate it. In this case, it may search through to procs in the Input queue and load the first one for which there is a large enough space to accommodate. CS211 Lectures 21 : Memory Management (ii) 4/23
Contiguous Memory Allocation When user procs terminate, memory holes are created. The operating system must allocated one of these hole to a new proc that is starting. If the hole it too large, only part of it is allocated and a new smaller hole is created. Also, when a proc terminates, if its space is adjacent to a free hole, they are amalgamated to create a larger one. The scheduler then checks if they new hole is large enough to accommodate the next job in the Input queue. If this procedure of splitting and amalgamating of holes continues for a while, we end up with blocks of available memory of various size are scattered throughout memory. This is called Fragmentation and is to be avoided. Strategies/Algorithms for allocating memory to procs is a crucial part of memory management. CS211 Lectures 21 : Memory Management (ii) 5/23
Contiguous Memory Allocation To summarise: Goal: In a multiprogramming environment, we wish to maximise the degree of multiprogramming on the system, by loading as many procs into memory as there is space for. Whats the problem? When user procs terminate, memory holes (or pockets ) are created. If the hole it too large, only part of it is allocated to the next proc., creating a smaller hole. When a proc terminates, if its space is adjacent to a free hole, they are amalgamated to create a larger one. How does the OS deal with this? Use strategies/algorithms/policy for allocating memory to procs. Try to maintain efficiency while minimising fragmentation. CS211 Lectures 21 : Memory Management (ii) 6/23
Contiguous Memory Allocation Three different strategies are: 1 First Fit (FF): allocate the first hole that is large enough to accommodate the new proc. This is the fastest method, but may cause the most fragmentation i.e, the most small pockets of unused non-contiguous memory. 2 Best Fit (BF): search all available holes and allocate the smallest hole that is big enough to accommodate the new proc. This is slower than best-fit, but leads to the smallest pocket sizes. 3 Worst Fit (WF): search all available holes and assign part of the the largest available hole. This is the slowest method but may leave a smaller number of larger holes than either first or best fit. CS211 Lectures 21 : Memory Management (ii) 7/23
Contiguous Memory Allocation Example Suppose that a system has four free memory holes orders as follows: H 1 = 100k, H 2 = 200k, H 3 = 500k, H 4 = 600k. Four procs arrive in the following order: P 1 (210k), P 2 (450k), P 3 (120k), P 4 (200k). Show how these would be allocated by the FF, BF, and WF strategies. CS211 Lectures 21 : Memory Management (ii) 8/23
Fragmentation Memory is partitioned into contiguous segments and allocated to different processes. The methods for contiguous memory allocation described above can lead to External fragmentation: total memory space exists to satisfy a request, but it is not contiguous. That is, memory is available but is broken up into pockets between partitions that are too small to be used. Internal fragmentation: Suppose there is a free hole of size 12,100 bytes and we require a partition of size 12,080 bytes. The OS may require more that 20 bytes to keep track of the unused portion, so it can be more economical to allocate all 12,100 bytes even though this is slightly larger than requested memory; this is called internal fragmentation CS211 Lectures 21 : Memory Management (ii) 9/23
Fragmentation One solution to the fragmentation problem is compaction, i.e., shuffle memory contents to place all free memory together in one large block. This is not always possible requires dynamic memory relocation. Also it can be time-consuming. Furthermore, fragmentation may happen in the swap space. However, compaction is certainly not feasible in that instance. (Why?) The other alternative is to allow for non-contiguous memory allocation. This is done by paging or segmentation, or some hybrid of these two approaches. CS211 Lectures 21 : Memory Management (ii) 10/23
Paging Basic idea: Logical address space of a process can be non-contiguous; process is allocated physical memory whenever the latter is available. Divide physical memory into fixed-sized blocks called frames. All frames on the system are of the same size, some power of 2, between 512 bytes and 8192 bytes (determined by the hardware). CS211 Lectures 21 : Memory Management (ii) 11/23
Paging To find out the page size on a Linux system, run this program: 1 / / Try this for Windows 2 #include <s t d i o. h> 3 # include <windows. h> From http://en.wikipedia.org/wiki/page size link! 5 i n t main ( void ) 6 { 7 SYSTEM INFO s i ; 8 GetSystemInfo (& s i ) ; 10 p r i n t f ( The page size f o r t h i s system i s %u bytes.\ n, 11 s i. dwpagesize ) ; 12 return 0; 13 } CS211 Lectures 21 : Memory Management (ii) 12/23
Paging From http://en.wikipedia.org/wiki/page size link! 1 / / Try this for Linux 2 #include <s t d i o. h> 3 #include <u n i s t d. h> / / sysconf(3) 4 i n t main ( void ) 5 { 6 p r i n t f ( The page size f o r t h i s system i s %l d bytes.\ n, 7 sysconf ( SC PAGESIZE ) ) ; 8 return ( 0 ) ; 9 } CS211 Lectures 21 : Memory Management (ii) 13/23
Paging Logical memory into blocks called pages. Frames and Pages are of the same size. the OS keeps track of all free frames in memory. To run a program of size n pages, we need to find n free frames and load program. Page Table maintained to translate logical to physical addresses. CS211 Lectures 21 : Memory Management (ii) 14/23
Paging Address Translation Scheme An address generated by CPU is divided into: Page number (p), used as an index into a page table which contains base address of each page in physical memory; Page offset (d), combined with base address to define the physical memory address that is sent to the memory unit. CS211 Lectures 21 : Memory Management (ii) 15/23
Paging Implementation of Page Table The Page table is kept in main memory. Page-table base register (PTBR) points to the page table. Page-table length register (PRLR) indicates size of the page table. Every data/instruction access requires two memory accesses one for the page table and one for the data/instruction. The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative registers or translation look-aside buffers (TLBs) Example: Suppose we have 128kb of physical memory, pages/frames of size 8kb, and a process that requires 32kb... By using paging we avoid external fragmentation. A problem with paging is that, on average, internal fragmentation accounts for one half page per process. CS211 Lectures 21 : Memory Management (ii) 16/23
Virtual Memory We have already considered using some hard-drive space as temporary storage for data that should reside in main memory but which belongs to a process that is not currently running in the CPU (e.g., it is ready or waiting ). We extend this idea to allow part of the address space of a process to be swapped out into secondary ( backing ) storage even though the process is in the CPU. We will look at a system that allows the total addressable logical memory to include physical memory and part of the backing storage. Key concepts include 1 Introduction 2 Demand paging 3 Page faults 4 the dirty bit 5 Page fault handling 6 page replacement algorithms 7 Thrashing CS211 Lectures 21 : Memory Management (ii) 17/23
Virtual Memory CS211 Lectures 21 : Memory Management (ii) 18/23
Virtual Memory Basic Idea: Only part of the program needs to be in memory for execution. Why? Often a process does not have to access all of its memory at once. For example, if the process has error handling routines, these will rarely be required. Logical address space can therefore be much larger than physical address space. Hence we can run procs that require more memory than is available on the system. Also, we can increase the degree of multi-programming in the system. (the number of procs that are ready to be brought into the CPU at any time.) VM requires that pages to be swapped/paged in and out (of swap space) in a procedure known as demand paging CS211 Lectures 21 : Memory Management (ii) 19/23
Demand Paging Basic ideas: Bring a page into memory only when it is needed (lazy paging). Pages belonging to a proc may be memory resident or not. The system needs some mechanism for distinguishing between resident and non-resident pages. Therefore the page table associates a valid/invalid bit with each page. 1 resident 0 not in physical memory. If a proc wishes to access a valid page it can do so. If a proc wishes to access an invalid page then the paging hardware generates a Page Fault. CS211 Lectures 21 : Memory Management (ii) 20/23
Page Fault If there is a reference to an invalid page, reference will trap to OS. reference to nonexisting page abort Just not in memory must bring page into memory. 1 Reference is made to invalid page 2 Page fault generated trap to Operating system (PCB of user process is saved and OS given access to CPU) 3 Locate empty frame in physical memory. 4 Swap page into frame. 5 Reset tables, flip validation bit (change to 1) 6 Restart user process (load PCB and perform next instruction) CS211 Lectures 21 : Memory Management (ii) 21/23
Page Fault CS211 Lectures 21 : Memory Management (ii) 22/23
Page Fault What happens if there is no free frame? Page replacement find some page in memory, but not really in use, and swap it out. We need an algorithm for selecting such a page. The algorithm will be evaluated on the basis of the generation of a minimum number of page faults. (More about this later...) CS211 Lectures 21 : Memory Management (ii) 23/23