CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16...

39
CSCI 156: Lab 11 Paging

description

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.

Transcript of CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16...

Page 1: 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.

CSCI 156: Lab 11 Paging

Page 2: 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.

Our Simple ArchitectureLogical memory space for a process consists

of 16 pages of 4k bytes each.

Your program thinks it has65536 bytes (64KB) of contiguousmemory that starts at address 0and runs up to 65535.

This memory page holdsbytes 0-4095 in localmemory space

Local Address 65535

Page 15

Page 0

Where is the logical memory located?

Page 3: 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.

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 growin to this area.

Page 4: 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.

Physical Memory

Page 15

Page 0

We have 8 frames of physicalmemory (for programs)Each frame is 4096 bytes.

Frame 0

Frame 7It is pretty typical for the logical(local) address space that your program can use to exceed theactual amount of physicalmemory present in the machine.

On Pentium series computersyou can access 4GB of logical address space (see pgs 310-311 of the Dinosaur Book for more info)

Logical Address Space

Page 5: 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.

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

Page 6: 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.

What Data Structures do we have?

Page 15

Page 0

Frame 0

Frame 7

Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Page 7: 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.

What Data Structures do we have?

Page 15

Page 0

Frame 0

Frame 7

Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

In our system, every process has its own page table.

Valid Bit Physical Frame or Disk Sector

Process ID

EntryforPage 0

EntryforPage 15

Page 8: 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.

Page 15

Page 0

Frame 0

Frame 7

Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Valid Bit Physical Frame(in memory?) or Disk Sector

Process ID

EntryforPage 0

EntryforPage 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

Page 9: 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.

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

theDisk

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 10: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 31

Physical Memory

theDisk

TRUETRUE

FALSETRUETRUEFALSETRUETRUE

Frame 0

Frame 7

Sector #11Sector #3

Sector #5Sector #13

Sector #21Sector #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 11: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 31

Physical Memory

theDisk

In use?

TRUETRUE

FALSETRUETRUEFALSETRUETRUE

Frame 0

Frame 7

Who is using it?We’ll store apointer to thatprocess’spage table

&(process #3 page table)

Where was itor where shouldit be stored onthe disk?

Sector #11Sector #3

Sector #5Sector #13

Sector #21Sector #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 12: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 31

Physical Memory

theDisk

What if all the physical memory frames are full but the process needs a frame for one of its pages...

TRUETRUE

TRUETRUETRUETRUETRUETRUE

Frame 0

Frame 7

Sector #11Sector #3

Sector #5Sector #13

Sector #21Sector #6

We can write the memory frame backto disk and free up the memory.

How do we know which sector towrite it back to on the disk?

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

&(process #4 page table)

Sector #1

Sector #4

The Memory Map

Page 13: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Valid Bit Physical Frame(in memory?) or Disk Sector

Process ID

EntryforPage 0

EntryforPage 15

3

TRUE 0

The memory manager evicted frame 0 and wrote itto sector 11 on the disk.Is the page table for that process still accurate?

tosector 11

Page 14: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Valid Bit Physical Frame(in memory?) or Disk Sector

Process ID

EntryforPage 0

EntryforPage 15

3

TRUE 0

Is the page table for that process still accurate?

Woops. It looks like the page table thinksthat its logical page is still stored in memoryframe 0.

tosector 11

Page 15: 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.

Page 15

Page 0

Frame 0Logical Address Space

Sector 31

Physical Memory

theDisk

TRUETRUE

TRUETRUETRUETRUETRUETRUE

Frame 0

Frame 7

Sector #11Sector #3

Sector #5Sector #13

Sector #21Sector #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.

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

&(process #4 page table)

Sector #1

Sector #4

The Memory Map

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

Page 15

Page 0

Frame 0Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Valid Bit Physical Frame(in memory?) or Disk Sector

Process ID

EntryforPage 0

EntryforPage 15

3

TRUE 0

tosector 11

We follow the pointer from the memory map backto that process’s page table. We’ll change theValid bit from TRUE to FALSE. We’ll replace framenumber 0 with the sector number (11) in theframe number / sector block.

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

Page 15

Page 0

Frame 0Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

Valid Bit Physical Frame(in memory?) or Disk Sector

Process ID

EntryforPage 0

EntryforPage 15

3

FALSE 11

tosector 11

We follow the pointer from the memory map backto that process’s page table. We’ll change theValid bit from TRUE to FALSE. We’ll replace framenumber 0 with the sector number (11) in theframe number / sector block.

Page 18: 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.

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

theDisk

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 framelogical page 0 corresponds to in your page table.

Page 19: 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.

Page 15

Page 0

Frame 0

Frame 7

Logical Address Space

Sector 0

Sector 31

Physical Memory

theDisk

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”

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

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.

Page 21: 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.

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 growin to this area.

The Memory Manager knows wherethese portions of your program are…and thus knows if the address you aretrying 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.

Page 22: 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.

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

theDisk

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 3rd data structure:The “Disk Map” to tell us what sectors are being used on the disk and which are free.

Entry forSector 0

Entry forSector 31

Disk Map:In use?

FALSETRUEFALSETRUETRUE

TRUEFALSETRUETRUETRUE

Page 23: 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.

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

theDisk

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.

TRUETRUE

FALSETRUETRUEFALSETRUETRUE

Sector #11Sector #3

Sector #5Sector #13

Sector #21Sector #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)

Page 24: 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.

On Demand Page LoadingWe’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.)

Page 25: 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.

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

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

1st Function: LoadProgramThe 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.

Page 27: 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.

1st Function: LoadProgramint 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];}

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

2nd Function: MemoryAccessBig 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.

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

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.

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

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.

Page 31: 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.

/* Simple case it is in memory */if(ourProcess->valid[logicalPage] == TRUE){

/* Calculate and return address */return ourProcess->currentLocation[logicalPage]*PAGE_SIZE

+ offset;}

Page 32: 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.

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

Page 33: 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.

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

}}

Page 34: 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.

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

Page 35: 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.

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

}}

Page 36: 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.

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

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

EvictionYou’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-7Random Frame to Evict.physicalFrame = rand() % 8;

Now we can call sendFromMemoryToDisk to actually send the frame to disk.

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

Eviction continuedNow 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.

Page 39: 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.

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

}