CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management - 2 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.

Slides:



Advertisements
Similar presentations
Chapter 4 Memory Management Basic memory management Swapping
Advertisements

Memory.
Part IV: Memory Management
Memory management In a multiprogramming system, in order to share the processor, a number of processes must be kept in memory. Memory management is achieved.
Chapter 6: Memory Management
CSS430 Memory Management Textbook Ch8
Memory Management Chapter 7.
Allocating Memory.
CS 311 – Lecture 21 Outline Memory management in UNIX
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management -3 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management.
CMPT 300 Operating System I
Memory Management (continued) CS-3013 C-term Memory Management CS-3013 Operating Systems C-term 2008 (Slides include materials from Operating System.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory ManagementCS-502 Fall Memory Management CS-502 Operating Systems Fall 2006 (Slides include materials from Operating System Concepts, 7 th.
Memory Management Chapter 5.
CS 333 Introduction to Operating Systems Class 9 - Memory Management
Memory ManagementCS-3013 C-term Memory Management CS-3013 Operating Systems C-term 2008 (Slides include materials from Operating System Concepts,
03/17/2008CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
03/05/2008CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
Memory Management By: Omar A. Cruz Salgado ICOM 5007 Sec. 121.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally MEMORYMANAGEMNT.
MEMORY MANAGEMENT Presented By:- Lect. Puneet Gupta G.P.C.G. Patiala.
CS333 Intro to Operating Systems Jonathan Walpole.
Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”
Cosc 2150: Computer Organization Chapter 6, Part 2 Virtual Memory.
1 Memory Management Chapter Basic memory management 4.2 Swapping (εναλλαγή) 4.3 Virtual memory (εικονική/ιδεατή μνήμη) 4.4 Page replacement algorithms.
Chapter 4 Memory Management.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Memory Management 1 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Chapter 10 Memory Management Introduction Process must be loaded into memory before being executed. Input queue – collection of processes on the.
CE Operating Systems Lecture 14 Memory management.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Memory Management. Introduction To improve both the utilization of the CPU and the speed of its response to users, the computer must keep several processes.
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
1 ADVANCED OPERATING SYSTEMS Lecture 8 (Week 10) Resource Management – II (Memory Management) by: Syed Imtiaz Ali.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
Chapter 8: Main Memory. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Memory and Addressing It all starts.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management Chapter 5 Advanced Operating System.
Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”
Memory management The main purpose of a computer system is to execute programs. These programs, together with the data they access, must be in main memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 8: Main Memory.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Memory Management.
CSC 322 Operating Systems Concepts Lecture - 12: by
Main Memory Management
Operating System Concepts
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
So far… Text RO …. printf() RW link printf Linking, loading
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Lecture 3: Main Memory.
Chapter 8: Memory Management strategies
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
Operating Systems: Internals and Design Principles, 6/E
CSE 542: Operating Systems
Page Main Memory.
Presentation transcript:

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 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 2 Relocation and Protection Multiprogramming introduces two problems:  Relocation  Protection Relocation  When a program is linked User-written program and library procedures are compiled into a single address space.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 3 Linking Library C code.c file(s) Program C code.c file(s) Library Object Code.o file(s) Program Object Code.o file(s) Executable Program (a.out) Linker Compiler

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 4 Compiler Input/Output #include void main() { int i, s; scanf(“input the number:%d \n”, &i); s = i * i; printf(“square of %d = %d”, i, s); } …. int scanf(char *….,….,….) { /* C code of scanf procedure */ …. } int printf(char *..,…,…) { /* C code of print procedure */ …… } …. main.c stdio library C code Compiler stdio library object code main.o

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 5 Linker Input main: call scanf call printf …. scanf: printf: main.o stdio library object code (/usr/lib/libstdio.a) Machine code Linker a.out

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 6 Linker Output main: call scanf call printf scanf: printf: a.out Machine code

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 7 Loader OS Program Program (a.out) Start address where program is loaded Hard-Disk Main Memory

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 8 Linker and Relocation Linker produces an executable file which memory references for variables and procedures  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  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 9 Relocation Problem 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 10 Relocation Solutions 1) Modify the instructions of the program that make memory reference when a program is first loaded into memory  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:  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 11 Relocation Solutions 2)  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 12 Relocation Solutions 3)  A hardware solution  Have 2 extra special registers: Limit and Base registers  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 13 Swapping and Paging Sometimes there not enough memory space to keep all the running programs in memory Two strategies to deal with this case  1) Swapping  2) Paging Swapping:  Bring each process as a whole into memory, run it for a while, then put it back on the disk. Paging:  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 14 Swapping Operating System A Operating System A B Operating System A B C Operating System B C Only A is running B is loaded C is loaded also A is swapped out Empty regions

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 15 Swapping Operating System D C Operating System D C D is loaded B B is swapped out Operating System D C A is Swapped In again A

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 16 Swapping 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 17 Processes and memory A process should reside in consecutive memory addresses  i.e some part of program can not be loaded to a different address Otherwise relocation would be very difficult Memory required to load a program  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 18 Processes and Memory Requirements Processes data segments may grow 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:  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 19 Processes and Memory Requirements A process may have two growing segments  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 20 Operating System Operating System A B Actually in use Room for growth Actually in use Room for growth A-program A-data A-stack B-program B-data B-stack Room for growth Allocating space for growing data segment Allocating space for growing data segment and stack segment

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 21 Keeping Track of Memory Usage We need to manage the allocated and free memory space  Bitmaps  Free Lists

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 22 Bitmaps Memory is divided into allocation units Units can be  A few words  ….  Several kilobytes We use a bit for each unit, telling if unit is free or allocated. Size of allocation unit  Affects the size of the bitmap Each unit is represented with a bit Number of Bits Required = (Size of Memory) / (Allocation Size)  Affects the unusable memory space 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 23 Bitmaps … A B B D E C ….. Bitmap MEMORY 1 unit of allocation

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 24 Linked Lists Linked list of allocated and free memory segments  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 25 Linked Lists … A B B D E C MEMORY 1 unit of allocation P05 H53P86P144H184 P226P283

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 26 Linked Lists – process termination When a process terminated (or swapped out) the corresponding entry must be deleted from linked list. AXBAB AXA XBB X Before X terminates After X terminates a) b) c) d) 4 cases to consider

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 27 Linked Lists – process creation When a program is to be loaded into memory, a hole must be selected for the program There are various strategies  First Fit  Next Fit  Best Fit  Worst Fit

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 28 First Fit Select the first whole from start of the list, that can hold the program The hole is broken into pieces  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 29 Next Fit Similar to first fit, except,  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 30 Best Fit 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.  Best fit generated tiny holes that are useless.  First fit generated larger holes.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 31 Worst Fit Searched the entire list for a hole that has the largest size.  Simulation shows that this is not a very good idea either.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 32 Speeding up the algorithms 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.  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 33 Speeding up the algorithms If hole list is separate, a further optimization is possible.  Do not keep an explicit separate list for keeping holes.  Instead implement the list as part of the holes themselves. ABCD MAIN MEMORY size of hole

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 34 Quick Fit Again separate lists are maintained for allocates spaces and holes. The list for holes can be further refined.  Keep separate lists of holes for common size.  Assume there are n different hole sizes, such as 4KB, 8KB, 12KB, 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 35 Virtual Memory Execute programs whose size is greater than physical memory size. The idea is:  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 36 Paging Virtual Memory systems use a technique called paging to realize this idea. There are two type addresses  Virtual addresses Memory addresses that are generated by a program This addresses is not used directly to retrieve a word from physical memory  Physical address 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:  MOV REG, 1000 /* move into register REG the content of memory address 1000 */

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 37 Memory Management Unit Virtual Memory systems have a unit called Memory Management Unit, that does the address translation from virtual addresses to physical addresses. CPU MMU Memory Disk Controller Disk Controller Hard Disk CPU Package Virtual Address Physical Address

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 38 Page Table 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 39 Page Table A Virtual address space is divided into fixed units called pages.  Page size is fixed Can change from 512 bytes to 64 KB For each virtual page that should be a corresponding physical page in memory if that page is used.  The physical page is called page frame. Page and page frames have always the same size  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 40 Example Assume a system  Virtual addresses are 16 bits Virtual address space is 64 KB  Physical memory size is 32 KB  Page size is 4 KB. Given these values  The memory can hold at most 8 page frames  The number pages in virtual address space is 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 41 Page Table x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K Page Table Main Memory Frame number page number

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 42 Example mappings x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, 0 virtual address Virtual address 0 is in page 0 (range 0K-4K Page 0 is mapped to frame 2 (range-8K-12K) Virtual address 0 is mapped to 8K = 8192 All virtual addresses in range 0-4K is mapped to Physical addresses in range 8K-12K.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 43 Example mappings x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, 8192 virtual address Virtual address 8192 is in page 2 (range 8K-12K) Page 2 is mapped to frame 6 (range-24K-28K) Virtual address 8192 is mapped to 24K = All virtual addresses in range 8K-12K are mapped to Physical addresses in range 24K-28K.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 44 Example mappings x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, virtual address = = 20K + 20 It is page 5 with offset is mapped to 3. (12K-16K) New address is = 12K + 20 =

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 45 Example mappings x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, virtual address = = 32K + 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!

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 46 Handling of Page Faults x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, virtual address Select a victim frame! Assume we select frame 1 Move frame 1 to disk.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 47 Handling of Page Faults x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, virtual address Load the corresponding page (for page 8) from Hard-disk, into the space of the victim page.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 48 Handling of Page Faults x x x 5 x 7 x x x x 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K 32K-36K 36K-40K 40K-44K 44K-48K 48K-52K 52K-56K 56K-60K 60K-64K 0K-4K 4K-8K 8K-12K 12K-16K 16K-20K 20K-24K 24K-28K 28K-32K MOV REG, virtual address Update the page table X 1

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 49 Handling of Page Faults After page fault is handles, the instruction can be continued executing. MMU is mapped to frame 1 (4K-8K) 4K + 12 = =

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 50 MMU Internal Operation MMU is in page number 2 (8196 = ) 2 is mapped to frame number 6 (24K-28K). 24K is New address = = 24580

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University = = MMU present/absent bit Page Table