Memory Management 2 CS 342 Operating Systems Ibrahim
Memory Management - 2 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University Department of Computer Engineering CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 1
Relocation and Protection n Multiprogramming introduces two problems: q q n Relocation Protection Relocation q When a program is linked n User-written program and library procedures are compiled into a single address space. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 2
Linking Library C code. c file(s) Program C code. c file(s) Compiler Library Object Code. o file(s) Program Object Code. o file(s) Linker Executable Program (a. out) CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 3
Compiler Input/Output main. c stdio library C code …. int scanf(char *…. , …. ) { /* C code of scanf procedure */ …. } int printf(char *. . , …, …) { } /* C code of print procedure */ …… #include <stdio. h> void main() { int i, s; scanf(“input the number: %d n”, &i); s = i * i; printf(“square of %d = %d”, i, s); } …. Compiler main. o stdio library object code CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 4
Linker Input stdio library object code (/usr/libstdio. a) main. o …. main: scanf: -----------call scanf -------call printf ----------------printf: ---------- Machine code Linker a. out CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 5
Linker Output a. out main: -----------call scanf -------call printf -------scanf: -----------printf: ----------- Machine code CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 6
Loader Hard-Disk Program (a. out) Loader Main Memory Program OS CS 342 – Operating Systems Spring 2003 Start address where program is loaded © Ibrahim Korpeoglu Bilkent University 7
Linker and Relocation n Linker produces an executable file which memory references for variables and procedures q n Example: variables i and s in the previous slides procedures scanf and printf The linker does not know in advance where actually a program will be loaded in memory q Linker produces output that assumes that the program will be loaded to a place in memory starting with memory address zero. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 8
Relocation Problem n n But due to multiprogramming, the physical start address in memory where program will be loaded into may be different than zero. In this case all memory references (based on zero start address) in program will be wrong when program is loaded. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 9
Relocation Solutions n 1) Modify the instructions of the program that make memory reference when a program is first loaded into memory q q n Linker must include a list together with the binary program telling which program words are memory addresses. Does not solve the protection problem. Protection Problem is: q a user program should not be able to access the part of memory that belongs to some other user program or OS CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 10
Relocation Solutions n 2) q q q Divide memory into blocks of 2 KB bytes and assign a 4 -bit protection code to each block. The PSW register contains a 4 -bit protection code for the running program The running program can access only the blocks of memory where the protection code in PSW and protection codes of blocks match CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 11
Relocation Solutions n 3) q q A hardware solution Have 2 extra special registers: n Limit and Base registers q q q Base register contains the start address of program in memory Limit register contains the size of the program in memory When a memory reference is generated by CPU, the base address is added to the memory reference to find the actual physical memory address. When a memory reference is made beyond base+limit, and error is returned. This solution also solves the problem of protection. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 12
Swapping and Paging n n Sometimes there not enough memory space to keep all the running programs in memory Two strategies to deal with this case q q n Swapping: q n 1) Swapping 2) Paging Bring each process as a whole into memory, run it for a while, then put it back on the disk. Paging: q A process does not have to brought up to the memory as a whole when it needs to be executed. It allows the programs to run even through they are partially in memory. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 13
Swapping Empty regions C C B B B A A A Operating System Only A is running B is loaded C is loaded also A is swapped out CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 14
Swapping C C C B A D D D Operating System D is loaded B is swapped out A is Swapped In again CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 15
Swapping n n This causes variable partitions to be made dynamically in memory. May provide better CPU utilization Requires data structures to keep track of which parts of memory are used and which parts are free (holes). Memory compaction can be used to combine small sized holes into big ones. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 16
Processes and memory n A process should reside in consecutive memory addresses q i. e some part of program can not be loaded to a different address n n Otherwise relocation would be very difficult Memory required to load a program q Depends on whether the program has a fixed size or variable size (may allocate more memory during execution). CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 17
Processes and Memory Requirements n Processes data segments may grow n n n by allocate more memory dynamically from a heap C, Pascal allows this for example. If a process wants to grow, there are two strategies to follow: q q 1) An adjacent hole can be used to grow. 2) Program can be relocated to a different larger hole CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 18
Processes and Memory Requirements n A process may have two growing segments q q n Data segment: variables dynamically created. Stack segment: local variable, etc. When a process is loaded into memory, it is a good idea to allocate more space for the process than it initially needs. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 19
Room for growth B-stack Room for growth B Actually in use B-data B-program A-stack Room for growth A-data A Operating System Allocating space for growing data segment CS 342 – Operating Systems Spring 2003 Actually in use A-program Operating System Allocating space for growing data segment and stack segment © Ibrahim Korpeoglu Bilkent University 20
Keeping Track of Memory Usage n We need to manage the allocated and free memory space q Bitmaps q Free Lists CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 21
Bitmaps n Memory is divided into allocation units n Units can be q q q n n A few words …. Several kilobytes We use a bit for each unit, telling if unit is free or allocated. Size of allocation unit q Affects the size of the bitmap n n q Each unit is represented with a bit Number of Bits Required = (Size of Memory) / (Allocation Size) Affects the unusable memory space n n The last unit allocated to a process may not be filled up. The larger the allocation unit, the bigger is the unused space in the last unit. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 22
Bitmaps 1 unit of allocation MEMORY B A D C …. . 24 16 8 E B 11111000 1111 11000011 Bitmap 11111110 …. . CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 23
Linked Lists n Linked list of allocated and free memory segments q q n n A segment is a process as a whole, or A segment is a hole (free) Segment list could be kept sorted with respect to the address Needs to be updated when processes come and go. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 24
Linked Lists 1 unit of allocation MEMORY B A D C …. . 24 16 8 E B P 0 5 H 5 P 22 6 CS 342 – Operating Systems Spring 2003 3 P 8 6 P 14 4 H 18 4 P 28 3 © Ibrahim Korpeoglu Bilkent University 25
Linked Lists – process termination n When a process terminated (or swapped out) the corresponding entry must be deleted from linked list. 4 cases to consider Before X terminates a) A X b) A X c) X d) X CS 342 – Operating Systems Spring 2003 After X terminates B A B B © Ibrahim Korpeoglu Bilkent University 26
Linked Lists – process creation n n When a program is to be loaded into memory, a hole must be selected for the program There are various strategies q q First Fit Next Fit Best Fit Worst Fit CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 27
First Fit n n Select the first whole from start of the list, that can hold the program The hole is broken into pieces q q n An allocated piece for the program A new hole (remaining of the original hole) It is a fast algorithm CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 28
Next Fit n Similar to first fit, except, q q n It remembers where it stopped in the previous search for a fit. It starts from that point for the next search of a fit for a new program Performs slightly worse than first fit. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 29
Best Fit n n n Search the entire list for a hole that is smallest and that can hold the new program Slower than the previous methods Causes more waste of memory than first or next fits. q q Best fit generated tiny holes that are useless. First fit generated larger holes. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 30
Worst Fit n Searched the entire list for a hole that has the largest size. q Simulation shows that this is not a very good idea either. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 31
Speeding up the algorithms n n n Keep separate lists of allocated segments and holes. Search for a hole will be done on a smaller list. De-allocation of a used segment is more difficult. q n Requires searching the neighbors of segment which can be holes or processes. The hole list could be sorted according to the hole size. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 32
Speeding up the algorithms n If hole list is separate, a further optimization is possible. q q Do not keep an explicit separate list for keeping holes. Instead implement the list as part of the holes themselves. A B size of hole CS 342 – Operating Systems Spring 2003 C D MAIN MEMORY © Ibrahim Korpeoglu Bilkent University 33
Quick Fit n n Again separate lists are maintained for allocates spaces and holes. The list for holes can be further refined. q q n Keep separate lists of holes for common size. Assume there are n different hole sizes, such as 4 KB, 8 KB, 12 KB, etc. When a program is to be loaded, the list closest to the size of the program is selected. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 34
Virtual Memory n n Execute programs whose size is greater than physical memory size. The idea is: q q q OS keeps only the necessary part of the program in the memory, the rest in the hard disk When a memory reference is made to the other part in hard-disk, that part is brought into memory. An unused part is stored back to disk (if it is modified). CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 35
Paging n n Virtual Memory systems use a technique called paging to realize this idea. There are two type addresses q Virtual addresses n n q Physical address n n n Memory addresses that are generated by a program This addresses is not used directly to retrieve a word from physical memory Addresses that are used actually to address a word in physical memory. This is the address that is put on the bus between CPU and Memory. Virtual addresses are generated by program instruction like the following: q MOV REG, 1000 /* move into register REG the content of memory address 1000 */ CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 36
Memory Management Unit n Virtual Memory systems have a unit called Memory Management Unit, that does the address translation from virtual addresses to physical addresses. Virtual Address CPU Package CPU Memory MMU CS 342 – Operating Systems Spring 2003 Disk Controller Hard Disk Physical Address © Ibrahim Korpeoglu Bilkent University 37
Page Table n n n Memory Management Unit uses a table called page table to map virtual addresses into physical memory addresses. Page table is index by virtual address (or by some portion of it). A page table entry contains information to reach the word stored in physical memory CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 38
Page Table n A Virtual address space is divided into fixed units called pages. q Page size is fixed n n For each virtual page that should be a corresponding physical page in memory if that page is used. q n Can change from 512 bytes to 64 KB The physical page is called page frame. Page and page frames have always the same size q When a corresponding page frame for a page is not found in memory, it is brought from hard-disk. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 39
Example n Assume a system q Virtual addresses are 16 bits n q q n Virtual address space is 64 KB Physical memory size is 32 KB Page size is 4 KB. Given these values q q The memory can hold at most 8 page frames The number pages in virtual address space is n q 64 KB / 4 KB = 16 pages. Therefore, the page table size is 16: we need to have 16 entries in the page table. One for each page. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 40
Page Table 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 page number Frame number Main Memory 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K 7 6 5 4 3 2 1 0 Page Table CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 41
Example mappings 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 MOV REG, 0 virtual address Virtual address 0 is in page 0 (range 0 K-4 K Page 0 is mapped to frame 2 (range-8 K-12 K) Virtual address 0 is mapped to 8 K = 8192 All virtual addresses in range 0 -4 K is mapped to Physical addresses in range 8 K-12 K. 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 42
Example mappings 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 MOV REG, 8192 virtual address Virtual address 8192 is in page 2 (range 8 K-12 K) Page 2 is mapped to frame 6 (range-24 K-28 K) Virtual address 8192 is mapped to 24 K = 24576 All virtual addresses in range 8 K-12 K are mapped to Physical addresses in range 24 K-28 K. 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 43
Example mappings 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 MOV REG, 20500 virtual address 20500 = 20480 + 20 = 20 K + 20 It is page 5 with offset 20. 5 is mapped to 3. (12 K-16 K) New address is = 12 K + 20 = 12308. 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 44
Example mappings 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 MOV REG, 32780 virtual address 32780 = 32768 + 12 = 32 K + 12 It is in page 8 with offset 12. But page table entry 8 does not contain any frame number. Need a trap to OS to load the corresponding page. This trap is called a page fault! 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 45
Handling of Page Faults MOV REG, 32780 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 virtual address Select a victim frame! Assume we select frame 1 Move frame 1 to disk. 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 46
Handling of Page Faults MOV REG, 32780 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x x x 3 4 0 6 1 2 CS 342 – Operating Systems Spring 2003 virtual address Load the corresponding page (for page 8) from Hard-disk, into the space of the victim page. 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 47
Handling of Page Faults MOV REG, 32780 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 60 K-64 K 56 K-60 K 52 K-56 K 48 K-52 K 44 K-48 K 40 K-44 K 36 K-40 K 32 K-36 K 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K x x 7 x 5 x 1 x x 3 4 0 6 1 X 2 CS 342 – Operating Systems Spring 2003 virtual address Update the page table 28 K-32 K 24 K-28 K 20 K-24 K 16 K-20 K 12 K-16 K 8 K-12 K 4 K-8 K 0 K-4 K © Ibrahim Korpeoglu Bilkent University 7 6 5 4 3 2 1 0 48
Handling of Page Faults 32780 MMU 8 is mapped to frame 1 (4 K-8 K) 4 K + 12 = 4096+12 = 4108 After page fault is handles, the instruction can be continued executing. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 49
MMU Internal Operation 8196 MMU 8196 is in page number 2 (8196 = 8192 + 4) 2 is mapped to frame number 6 (24 K-28 K). 24 K is 24576 New address = 24576+4 = 24580 CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 50
Page Table 0 1 0 0 0 0 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 000 000 111 000 101 000 000 011 100 000 110 001 010 0 0 1 1 1 = 24590 MMU present/absent bit 110 0 0 1 0 0 = 8196 CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 51
- Slides: 51