CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.

Slides:



Advertisements
Similar presentations
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Advertisements

HW/Study Guide. Synchronization Make sure you understand the HW problems!
CS4432: Database Systems II Buffer Manager 1. 2 Covered in week 1.
Virtual Memory Introduction to Operating Systems: Module 9.
Segmentation and Paging Considerations
COMP 3221: Microprocessors and Embedded Systems Lectures 27: Virtual Memory - III Lecturer: Hui Wu Session 2, 2005 Modified.
Virtual Memory Adapted from lecture notes of Dr. Patterson and Dr. Kubiatowicz of UC Berkeley.
Jaishankar Sundararaman
Chapter 8.3: Memory Management
Memory Management (II)
CS 536 Spring Run-time organization Lecture 19.
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
Chapter 3.2 : Virtual Memory
Chapter 9 Virtual Memory Produced by Lemlem Kebede Monday, July 16, 2001.
CSI 400/500 Operating Systems Spring 2009 Lecture #9 – Paging and Segmentation in Virtual Memory Monday, March 2 nd and Wednesday, March 4 th, 2009.
Run-time Environment and Program Organization
03/22/2004CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Chapter 91 Translation Lookaside Buffer (described later with virtual memory) Frame.
CS 61C: Great Ideas in Computer Architecture
Segmentation CS 537 – Introduction to Operating Systems.
CSE378 Virtual memory.1 Evolution in memory management techniques In early days, single program ran on the whole machine –used all the memory available.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
CS 346 – Chapter 8 Main memory –Addressing –Swapping –Allocation and fragmentation –Paging –Segmentation Commitment –Please finish chapter 8.
Lecture 21 Last lecture Today’s lecture Cache Memory Virtual memory
Chapter 8 Memory Management Dr. Yingwu Zhu. Outline Background Basic Concepts Memory Allocation.
CMPE 421 Parallel Computer Architecture
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
Virtual Memory CS Introduction to Operating Systems.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
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.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
Paging Example What is the data corresponding to the logical address below:
Lecture 11 Page 1 CS 111 Online Memory Management: Paging and Virtual Memory CS 111 On-Line MS Program Operating Systems Peter Reiher.
VIRTUAL MEMORY By Thi Nguyen. Motivation  In early time, the main memory was not large enough to store and execute complex program as higher level languages.
8.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Paging Physical address space of a process can be noncontiguous Avoids.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
CE Operating Systems Lecture 14 Memory management.
Chapter 4 Memory Management Virtual Memory.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Virtual Memory 1 1.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
Pintos project 3: Virtual Memory Management
Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
CHAPTER 3-3: PAGE MAPPING MEMORY MANAGEMENT. VIRTUAL MEMORY Key Idea Disassociate addresses referenced in a running process from addresses available in.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory: Page Table Structure CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
Memory Management Virtual Memory.
Memory Management.
Virtual Memory CSSE 332 Operating Systems
CE 454 Computer Architecture
Virtual Memory - Part II
Outline Paging Swapping and demand paging Virtual memory.
Paging COMP 755.
Swapping Segmented paging allows us to have non-contiguous allocations
Virtual Memory Hardware
Lecture 3: Main Memory.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Paging and Segmentation
CS703 - Advanced Operating Systems
Virtual Memory and Paging
Virtual Memory 1 1.
Presentation transcript:

CSCI 156: Lab 11 Paging

Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has bytes (64KB) of contiguous memory that starts at address 0 and runs up to This memory page holds bytes in local memory space Local Address Page 15 Page 0 Where is the logical memory located?

What does the logical memory space for your process look like? Logical Address 0 Logical Address Code Segment (your code in machine language) Constant Data Segment (constants) Variable Data Segment (global variables) The Heap (could be fragmented, can grow) The Stack (grows that way) Not Currently in Use – but Heap or Stack can grow in to this area.

Physical Memory Page 15 Page 0 We have 8 frames of physical memory (for programs) Each frame is 4096 bytes. Frame 0 Frame 7 It is pretty typical for the logical (local) address space that your program can use to exceed the actual amount of physical memory present in the machine. On Pentium series computers you can access 4GB of logical address space (see pgs of the Dinosaur Book for more info) Logical Address Space

Our Disk Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Our simple disk holds 32 sectors of 4096 bytes each. Since our disk sector size matches our page/frame size.. moving page/frames in and out of physical memory is simplified. Physical Memory Sector 0 is used for housekeeping data (directories) and is unavailable to store a page. the Disk

What Data Structures do we have? Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk

What Data Structures do we have? Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk In our system, every process has its own page table. Valid Bit Physical Frame or Disk Sector Process ID Entry for Page 0 Entry for Page 15

Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID Entry for Page 0 Entry for Page 15 How do know if a process is using one of its virtual pages? If a process isn’t using this virtual page- the valid bit is set to false and Disk Sector is set to 0 If a process is using the page but it is currently on disk… then the valid bit will be false and Disk Sector will be set to disk sector FALSE 0 FALSE 30 (Sector on Disk) TRUE 6 TRUE 0 3

Why do we need to track what physical memory frames are being used for? Page 15 Page 0 Frame 0 Frame 7 Logical Addr Space Sector 0 Sector 31 Physical Memory the Disk How can we tell if a physical memory frame is being used? Who is using it? What if we run out of free frames?

Page 15 Page 0 Frame 0 Logical Address Space Sector 31 Physical Memory the Disk TRUE FALSE TRUE FALSE TRUE Frame 0 Frame 7 Sector #11 Sector #3 Sector #5 Sector #13 Sector #21 Sector #6 Do we need to track what memory frames are being used for? Yes. We’ll call this the “Memory Map” &(process #3 page table) &(process #4 page table) &(process #3 page table) &(process #7 page table) &(process #4 page table) &(process #2 page table)

Page 15 Page 0 Frame 0 Logical Address Space Sector 31 Physical Memory the Disk In use? TRUE FALSE TRUE FALSE TRUE Frame 0 Frame 7 Who is using it? We’ll store a pointer to that process’s page table &(process #3 page table) Where was it or where should it be stored on the disk? Sector #11 Sector #3 Sector #5 Sector #13 Sector #21 Sector #6 Do we need to track what memory frames are being used for? &(process #4 page table) &(process #3 page table) &(process #7 page table) &(process #4 page table) &(process #2 page table) NOTE: We use a pointer to page table to simplify design. Some real systems store the process ID here… as the location of the process’s page table can change. The Memory Map

Page 15 Page 0 Frame 0 Logical Address Space Sector 31 Physical Memory the Disk What if all the physical memory frames are full but the process needs a frame for one of its pages... TRUE Frame 0 Frame 7 Sector #11 Sector #3 Sector #5 Sector #13 Sector #21 Sector #6 We can write the memory frame back to disk and free up the memory. How do we know which sector to write it back to on the disk? to sector 11 &(process #3 page table) &(process #4 page table) &(process #3 page table) &(process #7 page table) &(process #4 page table) &(process #2 page table) &(process #4 page table) Sector #1 Sector #4 The Memory Map

Page 15 Page 0 Frame 0 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID Entry for Page 0 Entry for Page 15 3 TRUE 0 The memory manager evicted frame 0 and wrote it to sector 11 on the disk. Is the page table for that process still accurate? to sector 11

Page 15 Page 0 Frame 0 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID Entry for Page 0 Entry for Page 15 3 TRUE 0 Is the page table for that process still accurate? Woops. It looks like the page table thinks that its logical page is still stored in memory frame 0. to sector 11

Page 15 Page 0 Frame 0 Logical Address Space Sector 31 Physical Memory the Disk TRUE Frame 0 Frame 7 Sector #11 Sector #3 Sector #5 Sector #13 Sector #21 Sector #6 When we “evict” a physical frame from memory… we have to go to that process’s page table and update the appropriate entry. That’s why we keep the pointer to the process’s page table in memory map. to sector 11 &(process #3 page table) &(process #4 page table) &(process #3 page table) &(process #7 page table) &(process #4 page table) &(process #2 page table) &(process #4 page table) Sector #1 Sector #4 The Memory Map

Page 15 Page 0 Frame 0 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID Entry for Page 0 Entry for Page 15 3 TRUE 0 to sector 11 We follow the pointer from the memory map back to that process’s page table. We’ll change the Valid bit from TRUE to FALSE. We’ll replace frame number 0 with the sector number (11) in the frame number / sector block.

Page 15 Page 0 Frame 0 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID Entry for Page 0 Entry for Page 15 3 FALSE 11 to sector 11 We follow the pointer from the memory map back to that process’s page table. We’ll change the Valid bit from TRUE to FALSE. We’ll replace frame number 0 with the sector number (11) in the frame number / sector block.

What if the process accesses a logical page that isn’t in memory or on disk? (i.e. Invalid with 0) Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID FALSE 0 Process: I’d like to write to memory address Memory Manager: Let’s see- that is on logical page 0. Let me check if it is valid and see which frame logical page 0 corresponds to in your page table.

Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Valid Bit Physical Frame (in memory?) or Disk Sector Process ID FALSE 0 Memory Manager has 2 choices: 1. It can accept the memory access request to use a new part of memory and set up a new page for the process: a. Find a sector on the disk that it can use to store the page if has to be “evicted” from memory. (“Backing Store”). b. Must find an empty frame in Physical Memory for the page 2. It can deny the request: “SEGMENTATION FAULT”

How does memory manager decide whether to crash or allow the access? Really varies from system to system… That’s why your code may “seem” to work at home and crash in the lab.. or work on Hobbes and crash in Windows, etc. The Memory Manager looks at your logical address space and makes sure you’re accessing something that you’re “allowed” to access.

What does logical memory space for your process look like? Logical Address 0 Address Code (your code in machine language) Constant Data (constants) Variable Data (global variables) The Heap (all over in this area) The Stack (grows that way) Not Currently in Use – but Heap or Stack can grow in to this area. The Memory Manager knows where these portions of your program are… and thus knows if the address you are trying to access is in one of these parts or in just some empty space. Your address For our program- we’ll assume that all accesses are legit.

What if the process accesses a logical page that isn’t in memory or on disk? (i.e. Invalid with 0) Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 0 Sector 31 Physical Memory the Disk Since we’ll always accept users access: a. Find a sector on the disk that we can use to store the page if it has to be “evicted”. (“Backing Store”). So we’ll actually need a 3 rd data structure: The “Disk Map” to tell us what sectors are being used on the disk and which are free. Entry for Sector 0 Entry for Sector 31 Disk Map: In use? FALSE TRUE FALSE TRUE FALSE TRUE

What if the process accesses a logical page that isn’t in memory or on disk? (i.e. Invalid with 0) Page 15 Page 0 Frame 0 Frame 7 Logical Address Space Sector 31 Physical Memory the Disk Since we’ll accept: We must find an empty frame in physical memory for the page. We’ll look through the memory map for an empty frame. If we can’t find an empty frame- we’ll choose one to evict. TRUE FALSE TRUE FALSE TRUE Sector #11 Sector #3 Sector #5 Sector #13 Sector #21 Sector #6 &(process #3 page table) &(process #4 page table) &(process #3 page table) &(process #7 page table) &(process #4 page table) &(process #2 page table)

On Demand Page Loading We’re going to use “On Demand” page loading. When we start a program- we’re not going to load any of the code “pages” from disk in to the physical memory frames. But you will set up the page table so that we know where to get the code from disk when there is a logical address accessed in that logical page. (i.e. for each page entry in the page table you will set the disk sector.)

Your tasks: You’re going to write some code to handle starting a process and processing memory accesses for that process tar -xvf paging.tar file Look at the code, especially the data structures. The BIG GOAL: Understand paging Write the code for LoadProgram Write the code for MemoryAccess assuming there is always free physical memory (no eviction required) If you have time- add the code to handle eviction to MemoryAccess

1 st Function: LoadProgram The file directory service is going to send you an array of 16 integers. These 16 integers are the disk sector numbers that contain the code corresponding to each of the 16 logical pages. Not all the logical pages are used by programs… if there is nothing to be loaded for a page- the disk sector will be 0 (remember sector 0 is the signal to the page table that the page isn’t on disk or memory). So you’ll need to set the page table’s valid bits and sector numbers.

1 st Function: LoadProgram int count; /* You need to set up the page table for operation */ for(count = 0; count < LOGICAL_MEM_BLOCKS; count++) { ourProcess->valid[count]=FALSE; ourProcess->currentLocation[count] =programDiskLocations[count]; }

2 nd Function: MemoryAccess Big picutre: MemoryAccess takes in a logical memory address. It converts the logical address to a physical address. Case 1: the requesting page is in a physical memory frame Case 2: the requesting page is not either in memory or on disk. Case 3: the requesting page is not in memory, but on disk.

Case 1 If the page is in a physical memory frame then its just a matter of replacing the logical page number with the physical frame number.

How do we find which logical page and address is on? logical page number= logical address/4096; So check the page table entry for the logical page and see if the valid bit is set. If it is valid- we can get the physical frame number and calculate the physical address as follows: The “offset” are the 10 least significant bits of the memory address: offset=logical address % 4096; physical memory address= ( physical frame number * 4096 ) + offset; The function returns the physical address.

/* Simple case it is in memory */ if(ourProcess->valid[logicalPage] == TRUE) { /* Calculate and return address */ return ourProcess->currentLocation[logicalPage]*PAGE_SIZE + offset; }

This request is for a page that isn’t in memory or on disk. So we need to find a sector on the disk to use as a “backing store” to save this “new” page to. So look through disk sectors for an empty one (be sure to set its inUse variable). Update page table -- set currentLocation as the sector number. Now the logical page is not in physical memory but is on disk, which is case 3. Case 2

if(ourProcess->valid[logicalPage] == FALSE && ourProcess->currentLocation[logicalPage] == 0) { /* enter the disk information in to our page table and continue */ for(index = 1; index < DISK_SECTORS; index++) { if(diskMap.inUse[index] == FALSE) { diskMap.inUse[index] = TRUE; ourProcess->currentLocation[logicalPage] = index; break; }

The logical page is not in physical memory but is on disk. Then you’ll need to find a free physical memory frame. (Don’t worry about eviction right now… just go through the memory list.) Call loadFromDiskToMemory when you find an empty physical memory frame. You’ll need to set the memory map’s inUse variable, the sector number and the pointer to your page table ( ourProcess is a pointer to your page table). You’ll need to set the page table’s valid bit and set the physical frame number to the frame selected. You can then calculate and return the physical memory address as in the previous case. Case 3

/* Now, it is not in momory, but is on disk */ /* Look for a free frame in memory */ physicalFrame = -1; for(index = 0; index < PHYSICAL_MEM_FRAMES; index++) { if(memoryMap.inUse[index] == FALSE) { physicalFrame = index; break; }

/* Call loadFromDiskToMemory */ loadFromDiskToMemory(ourProcess->currentLocation[logicalPage], physicalFrame); /* Update memory map */ memoryMap.inUse[physicalFrame] = TRUE; memoryMap.processUsing[physicalFrame] = ourProcess; memoryMap.locationOnDisk[physicalFrame] = ourProcess->currentLocation[logicalPage]; /* Update page table */ ourProcess->valid[logicalPage] = TRUE; ourProcess->currentLocation[logicalPage] = physicalFrame; /* Calculate and return address */ return physicalFrame*PAGE_SIZE + offset;

Eviction You’re doing great! But after awhile the physical memory frames are full. In order for us to get an empty physical memory frame- we’ll have to send a page from memory to disk. So we’ll have to add to our MemoryAccess code… This will give us a random frame between 0-7 Random Frame to Evict. physicalFrame = rand() % 8; Now we can call sendFromMemoryToDisk to actually send the frame to disk.

Eviction continued Now we need to update the old frame’s page table and memory map to reflect that the sector has been moved from memory to disk. We’ll go to the old page table (via memory map’s pointer) and set the page table entry’s valid bit to false and the disk sector to the sector we just wrote the frame to. You have to find the entry first.

/* If there's no free memory */ if(physicalFrame < 0) { procControlBlock* oldProcess; /* then we'll have to evict: save for last! */ physicalFrame = rand() % 8; sendFromMemoryToDisk(physicalFrame, memoryMap.locationOnDisk[physicalFrame]); for(index = 0; index < LOGICAL_MEM_BLOCKS; index++) { oldProcess = memoryMap.processUsing[physicalFrame]; if(oldProcess->valid[index] == TRUE && oldProcess->currentLocation[index] == physicalFrame) { oldProcess->valid[index] = FALSE; oldProcess->currentLocation[index] = memoryMap.locationOnDisk[physicalFrame]; break; }