Memory Management CSE-331 Operating System and System Programming Tonmoy Sarkar Lecturer Varendra University Department of Computer Science of Engineering Varendra University
Program must be brought (from disk) into memory and placed within a process for it to be run Memory management is the functionality of an operating system which handles or manages primary memory. Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free. It checks how much memory is to be allocated to processes. 2 Memory Management
Base and Limit Register To protect processes from each other each process has its own separate memory space Base register holds the smallest legal physical memory address Limit register specifies the size of the range of accessible addresses 3
Base and Limit Register(cont.) CPU must check every memory access generated in user mode to be sure it is between base and limit for that user; to protect a process’s memory space Base and limit registers loaded only by the OS through a privileged instruction To prevent users from changing these registers’ contents 4
Fig.1- Base and limit registers 5
Hardware Address Protection 6
Swapping A process can be swapped temporarily 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 Hence, swapping increases the degree of multiprogramming Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images 7
Swapping (Cont.) 8
Contiguous Memory Allocation Contiguous memory allocation is one early method for allocating memory is to divide memory into fixed size of partitions. When a partition free a process is selected from the input queue and loaded into the free partition. Used by IBM OS/360 Operating System 9
Contiguous Memory Allocation 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 search entire list, unless ordered by size Produces the largest leftover hole 10
Example Suppose you have given some free memory partitions of 100MB, 500MB, 200MB, 300MB, and 600MB (in order), how would each of the First-fit, Best-fit, and Worst-fit algorithms place processes of 212MB, 417MB, 112MB, and 426MB (in order)? 11
Example(First Fit) Allocate the first hole that is big enough. P1- 212 MB P2-417 MB P3-112 MB P4-426 MB 12 100 MB 500 MB (P1) 200 MB 300 MB 600 MB Main memory
Example(First Fit) Allocate the first hole that is big enough. P1- 212 MB P2-417 MB P3-112 MB P4-426 MB 13 100 MB 500 MB (P1) 200 MB 300 MB 600 MB (P2) Main memory
Example(First Fit) Allocate the first hole that is big enough. P1-212 MB P2-417 MB P3-112 MB P4-426 MB 14 100 MB 500 MB (P1) 200 MB(P3) 300 MB 600 MB (P2) Main memory
Example(Best Fit) Allocate the smallest hole that is big enough P1-212 MB P2-417 MB P3-112 MB P4-426 MB 16 100 MB 500 MB 200 MB 300 MB(P1) 600 MB Main memory
Example(Best Fit) Allocate the smallest hole that is big enough P1- 212 MB P2-417 MB P3-112 MB P4-426 MB 17 100 MB 500 MB (P2) 200 MB 300 MB(P1) 600 MB Main memory
Example(Best Fit) Allocate the smallest hole that is big enough P1- 212 MB P2-417 MB P3-112 MB P4-426 MB 18 100 MB 500 MB (P2) 200 MB(P3) 300 MB(P1) 600 MB Main memory
Example(Best Fit) Allocate the smallest hole that is big enough P1- 212 MB P2-417 MB P3-112 MB P4-426 MB 19 100 MB 500 MB (P2) 200 MB(P3) 300 MB(P1) 600 MB(P4) Main memory
Example(Worst Fit) Allocate the largest hole P1- 212 MB P2-417 MB P3-112 MB P4-426 MB(Waiting) 20 100 MB 500 MB (P2) 200 MB 300 MB(P3) 600 MB (P1) Main memory
Fragmentation External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous Both, first-fit and best-fit strategies suffer from external fragmentation Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. 21
Internal Fragmentation(Example) Example: a process requests 462MB and is allocated memory hole of 464MB; we are then left with a hole of 2 MB 23 200 MB 462 MB 2 MB 500 MB 300 MB
Paging Technique Divide physical memory into fixed-sized blocks called frames Divide logical memory into blocks of same size as the frames called pages Divide backing store into [clusters of] blocks of same size as the frames Keep track of all free frames To run a program of size N pages, need to find N free frames and load program 24
Paging Technique(Cont.) Each address generated by CPU is divided into two parts: Page number ( p ) – used as an index into a page table Page table contains the base address of each page in physical memory Page offset ( d ) – combined with the base address 25
Paging Example Example: using a 32-byte memory (8 pages) with 4-byte pages ( ) = 4 bytes Logical address 0 is page p =0, offset d =0; Page 0 is in frame f =5; [ ( f × m) + d ] Physical Address= (frame no × page size) + offset Thus logical address maps to physical address 20 [= (5 × 4) + 0]; 26
If we have m-bit processor than logical address space will also be m-bits long. Let the size of logical address space is 2^m and page size is 2^n. Q-1: Consider the logical address space of 8 pages of 1024 words mapped onto physical memory of 32 frames. How many bits are there in logical address How many bits there in physical address Numerical on Paging