Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSCI 156: Lab 11 Paging

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

3 What does the logical memory space for your process look like? Logical Address 0 Logical Address 65535 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.

4 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 310-311 of the Dinosaur Book for more info) Logical Address Space

5 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

6 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

7 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

8 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

9 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?

10 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)

11 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

12 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

13 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

14 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

15 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

16 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.

17 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.

18 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 2048. 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.

19 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”

20 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.

21 What does logical memory space for your process look like? Logical Address 0 Address 65535 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.

22 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

23 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)

24 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.)

25 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

26 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.

27 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]; }

28 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.

29 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.

30 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.

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

32 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

33 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; }

34 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

35 /* 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; }

36 /* 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;

37 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.

38 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.

39 /* 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; }


Download ppt "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."

Similar presentations


Ads by Google