chapter 10: virtual memory background background demand paging demand paging process creation...

91
CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame Allocation Frame Allocation Thrashing Thrashing Operating System Examples Operating System Examples

Upload: armani-tag

Post on 14-Dec-2015

236 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

CHAPTER 10: VIRTUAL MEMORY

BackgroundBackground Demand PagingDemand Paging Process CreationProcess Creation Page ReplacementPage Replacement Frame Allocation Frame Allocation ThrashingThrashing Operating System ExamplesOperating System Examples

Page 2: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

BACKGROUND The above discussed memory management methods The above discussed memory management methods

tend to require the entire process to be in memory tend to require the entire process to be in memory before the process can executebefore the process can execute

In many cases, the entire program is not neededIn many cases, the entire program is not needed Programs often have code to handle unusual error Programs often have code to handle unusual error

conditions. conditions. Arrays, lists, and tables are often allocated more Arrays, lists, and tables are often allocated more

memory than they actually need. memory than they actually need. Center options and features of a program may be Center options and features of a program may be

used rarely. used rarely. Even in those cases where the entire program is Even in those cases where the entire program is

needed, it may not all be needed at the same time needed, it may not all be needed at the same time (such is the case with overlays, for example)(such is the case with overlays, for example)

Page 3: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Background The ability to execute a program that is only The ability to execute a program that is only

partially in memory would have many partially in memory would have many benefits:benefits: Allows processes to use extremely large Allows processes to use extremely large

virtual address space, simplifying the virtual address space, simplifying the programming task.programming task.

Allows more processes to be able to run. Allows more processes to be able to run. Less I/O would be needed to load or swap Less I/O would be needed to load or swap

each user program into memory.each user program into memory. virtual memory.virtual memory.

Page 4: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Background Virtual memory management (Virtual memory management ( 虚拟内存管理虚拟内存管理 ) is a t) is a t

erm used to describe a technique whereby the computerm used to describe a technique whereby the computer appears to have much more memory than it actualler appears to have much more memory than it actually does. (Virtual means being in effect but not in fact)y does. (Virtual means being in effect but not in fact)

An analogyAn analogy You run a train as long as a railway is. You run a train as long as a railway is. You can run a train across the country by pulling uYou can run a train across the country by pulling u

p the rails behind it and laying them down again in p the rails behind it and laying them down again in front of it as it rolls. The faster you can relay the trfront of it as it rolls. The faster you can relay the track, the better. It only needs to have rails directly uack, the better. It only needs to have rails directly underneath. nderneath.

Page 5: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Background

Virtual Memory That is Larger Than Physical MemoryVirtual Memory That is Larger Than Physical Memory

Page 6: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Background Virtual memoryVirtual memory – separation of user logical – separation of user logical

memory from physical memory.memory from physical memory. Only part of the program needs to be in Only part of the program needs to be in

memory for execution.memory for execution. Logical address space can therefore be Logical address space can therefore be

much larger than physical address space.much larger than physical address space. Allows address spaces to be shared by Allows address spaces to be shared by

several processes.several processes. Allows for more efficient process creation.Allows for more efficient process creation.

Page 7: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Background Virtual memory is not easy to implement.Virtual memory is not easy to implement. Virtual memory implementation needs Virtual memory implementation needs

hardware support.hardware support. Virtual memory can be implemented via:Virtual memory can be implemented via:

Demand paging. Demand paging. Demand segmentation.Demand segmentation.

Page 8: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

DEMAND PAGING A demand-paging is similar to a paging system A demand-paging is similar to a paging system

with swapping. with swapping. When we want to execute a process, we swap When we want to execute a process, we swap

it into memory. it into memory. We use a lazy swapper. A lazy swapper never We use a lazy swapper. A lazy swapper never

swaps a page into memory unless that page swaps a page into memory unless that page will be needed. will be needed.

Swapper or pager?Swapper or pager?

Page 9: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept

Transfer of a Paged Memory to Contiguous Disk SpaceTransfer of a Paged Memory to Contiguous Disk Space

Page 10: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept Bring a page into memory only when it is needed.Bring a page into memory only when it is needed.

Less I/O neededLess I/O needed Less memory needed Less memory needed Faster responseFaster response More usersMore users

Page is needed Page is needed reference to it reference to it invalid reference invalid reference abort abort not-in-memory not-in-memory bring to memory bring to memory

Page 11: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept With each page table entry a valid–invalid bit With each page table entry a valid–invalid bit

is associatedis associated 1 1 legal and in-memory, legal and in-memory, 0 0 illegal or not in-memory illegal or not in-memory

Initially valid–invalid is set to 0 on all entries.Initially valid–invalid is set to 0 on all entries. During address translation, if valid–invalid bit During address translation, if valid–invalid bit

in page table entry is 0 in page table entry is 0 page fault. page fault. The process will keep page faulting until all The process will keep page faulting until all

the pages it needs is in memory.the pages it needs is in memory. Example of a page table snapshot. =>see nextExample of a page table snapshot. =>see next

Page 12: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept

Page 13: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept

Steps in Handling a Page FaultSteps in Handling a Page Fault

Page 14: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept HardwareHardware

Page table: This table has the ability to mark Page table: This table has the ability to mark an entry invalid through a valid-invalid bit an entry invalid through a valid-invalid bit or special value of protection bits. or special value of protection bits.

Secondary memory: This memory holds Secondary memory: This memory holds those pages that are not present in main those pages that are not present in main memory. The secondary memory is usually memory. The secondary memory is usually a high-speed disk. a high-speed disk.

SoftwareSoftware Page replacement algorithmsPage replacement algorithms Frame allocation algorithmsFrame allocation algorithms

Page 15: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept If there is ever a reference to a page, first If there is ever a reference to a page, first

reference will trap to OS reference will trap to OS page fault page fault OS looks at another table to decide:OS looks at another table to decide:

Invalid reference Invalid reference abort. abort. Just not in memory.Just not in memory.

Get empty frame.Get empty frame. Swap page into frame.Swap page into frame. Reset tables, validation bit = 1.Reset tables, validation bit = 1. Restart instruction:Restart instruction:

Page 16: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept A crucial architectural constraint is the need to restart A crucial architectural constraint is the need to restart

any instruction after a page faultany instruction after a page fault If the page fault occurs on the instruction fetch, we If the page fault occurs on the instruction fetch, we

can restart by fetching the instruction again. can restart by fetching the instruction again. If a page fault occurs while we are fetching an If a page fault occurs while we are fetching an

operand, we must fetch and decode the instruction operand, we must fetch and decode the instruction again, and then fetch the operand. again, and then fetch the operand.

Add A, B, CAdd A, B, C Fetch and decode the instruction (Add)Fetch and decode the instruction (Add) Fetch AFetch A Fetch BFetch B Add A and BAdd A and B Store the sum in CStore the sum in C

Page 17: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept MVC (IBM System 360/370)MVC (IBM System 360/370)

Moves up to 256 bytes from one location to Moves up to 256 bytes from one location to another (possibly overlapping) location. another (possibly overlapping) location.

If either source or destination straddles a page If either source or destination straddles a page boundary, a page fault might occur after the move boundary, a page fault might occur after the move is partially done. In addition, if the source and is partially done. In addition, if the source and destination blocks overlap, the source black may destination blocks overlap, the source black may have been modified, in which case we cannot have been modified, in which case we cannot simply restart the instruction. simply restart the instruction.

Solution 1: the microcode computes and attempts Solution 1: the microcode computes and attempts to access both ends of both blocks. to access both ends of both blocks.

Solution 2: to use temporary registers to hold the Solution 2: to use temporary registers to hold the values of overwritten locations. values of overwritten locations.

Page 18: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept Autodecrement and autoincrement modes (PDP-11)Autodecrement and autoincrement modes (PDP-11)

Autodecrement automatically decrements the regisAutodecrement automatically decrements the register before using its contents as the operand address;ter before using its contents as the operand address;

Autoincrement automatically increments the registAutoincrement automatically increments the register after using its contents as the operand address. er after using its contents as the operand address.

MOV (R2)+, -(R3)MOV (R2)+, -(R3) What will happen if we get a page fault when tryinWhat will happen if we get a page fault when tryin

g to store into the location pointed to by register3?g to store into the location pointed to by register3? Possible solution: to use a status register to indicatPossible solution: to use a status register to indicat

e how much has been done. e how much has been done.

Page 19: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Basic Concept Page fault frequencyPage fault frequency

In theory, multiple page faults per instructionIn theory, multiple page faults per instruction In practice, programs tend to have locality of In practice, programs tend to have locality of

reference, which results in reasonable performance reference, which results in reasonable performance from demand pagingfrom demand paging

Page 20: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance Demand paging can have a significant effect on the Demand paging can have a significant effect on the

performance of a computer system.performance of a computer system. To compute the effective access time for a demand To compute the effective access time for a demand

paged memorypaged memory As long as there is no page fault, the effective As long as there is no page fault, the effective

access time is equal to the memory access time. access time is equal to the memory access time. (about 10 to 200 nanoseconds).(about 10 to 200 nanoseconds).

If there is any page fault, we must first read the If there is any page fault, we must first read the relevant page from disk, and then access the relevant page from disk, and then access the desired word. desired word. the effective access time could be the effective access time could be very large!very large!

Page 21: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance Let p be the probability of a page fault (0 <= p Let p be the probability of a page fault (0 <= p

<= 1), then Effective Access Time (EAT)<= 1), then Effective Access Time (EAT)

EAT = (1 – EAT = (1 – pp) (memory access)) (memory access)

+ + pp (page fault time) (page fault time) if if pp = 0, no page faults. = 0, no page faults. if if pp = 1, every reference is a fault. = 1, every reference is a fault. Most of time, p is very close to 0.Most of time, p is very close to 0.

Page 22: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance To compute the EAT, we must know how much time is To compute the EAT, we must know how much time is

needed to service a page fault. A page fault causes the needed to service a page fault. A page fault causes the following sequence to occur:following sequence to occur:

1.1. Trap to the OS.Trap to the OS.2.2. Save the user registers and process state.Save the user registers and process state.3.3. Determine that the interrupt was a page fault.Determine that the interrupt was a page fault.4.4. Check that the page reference was legal and Check that the page reference was legal and

determine the location of the page on the disk.determine the location of the page on the disk.5.5. Issue a read (from the disk to a free frame): Issue a read (from the disk to a free frame):

Wait in a queue for this device until the read Wait in a queue for this device until the read request is serviced.request is serviced.

Wait for the device seek time and latency time.Wait for the device seek time and latency time. Begin the transfer of the page to a free frame. Begin the transfer of the page to a free frame.

Page 23: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance6.6. While waiting, allocate the CPU to some other user While waiting, allocate the CPU to some other user

(CPU scheduling, optional).(CPU scheduling, optional).7.7. Interrupt from the disk (I/O completed).Interrupt from the disk (I/O completed).8.8. Save the registers and process state for the other Save the registers and process state for the other

user (if step 6 is executed). user (if step 6 is executed). 9.9. Determine that the interrupt was from the disk.Determine that the interrupt was from the disk.10.10. Correct the page table and other tables to show that Correct the page table and other tables to show that

the desired page is now in memory.the desired page is now in memory.11.11. Wait for the CPU to be allocated to this process Wait for the CPU to be allocated to this process

again.again.12.12. Restore the user registers, process state, and new Restore the user registers, process state, and new

page table, then resume the interrupt instruction. page table, then resume the interrupt instruction.

Page 24: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance Three major components of the page-fault service timeThree major components of the page-fault service time

Service the page-fault interruptService the page-fault interrupt Read in the pageRead in the page Restart the processRestart the process

Effective Access Time (EAT)Effective Access Time (EAT)

EAT = (1 – EAT = (1 – pp) x memory access) x memory access

+ + pp ( Service the page-fault interrupt ( Service the page-fault interrupt

+ READ IN THE PAGE+ READ IN THE PAGE

+ Restart the process)+ Restart the process) Seek time (15ms) + latency time (8ms) + transfer time Seek time (15ms) + latency time (8ms) + transfer time

(1ms)(1ms)

25ms25ms

Page 25: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance Assume an average page-fault service time of Assume an average page-fault service time of 25 25

millisecondmillisecond and a memory access time of and a memory access time of 100 100 nanosecondsnanoseconds, then the EAT in nanoseconds is, then the EAT in nanoseconds is

EAT EAT = (1-p) 100 ns + p (25ms)= (1-p) 100 ns + p (25ms)= (1-p) 100 ns + p (25,000,000 ns)= (1-p) 100 ns + p (25,000,000 ns)= 100 + 24,999,900 p (ns)= 100 + 24,999,900 p (ns)

When p = 0.001=1/1000, EAT = 25ms When p = 0.001=1/1000, EAT = 25ms The computer would be slowed down by a The computer would be slowed down by a factor of 250 because of paging. factor of 250 because of paging.

When p = 0.0000004 = 1/2500000, EAT = 110ns. When p = 0.0000004 = 1/2500000, EAT = 110ns. The degradation is less than 10%. The degradation is less than 10%.

Page 26: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Demand Paging: Performance How to reduce the EAT:How to reduce the EAT:

To reduce the page fault rate as much as possible.To reduce the page fault rate as much as possible. Other options: Other options:

Option 1: Swap space is faster. So the OS just copies Option 1: Swap space is faster. So the OS just copies an entire file image into the page space at process an entire file image into the page space at process startup and then performing paging from the swap startup and then performing paging from the swap space. space.

Option 2: to demand pages from the file system Option 2: to demand pages from the file system initially, but to write the pages to swap space as they initially, but to write the pages to swap space as they are replaced. are replaced.

Option 3: to demand pages directly from the file Option 3: to demand pages directly from the file system and to use swap space for process stack and system and to use swap space for process stack and heap.heap.

Page 27: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

PROCESS CREATION

Virtual memory allows other benefits during Virtual memory allows other benefits during process creation:process creation:

- Copy-on-Write- Copy-on-Write

- Memory-Mapped Files- Memory-Mapped Files

Page 28: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Process Creation: Copy-on-Write fork()fork()

To create a copy of the parent’s address space for the chTo create a copy of the parent’s address space for the child, duplicating the pages belonging to the parent. ild, duplicating the pages belonging to the parent.

The copying of the parent’s address space may be unnecThe copying of the parent’s address space may be unnecessary for many child processes invoke the essary for many child processes invoke the exec()exec() syste system call immediately after creation. m call immediately after creation.

vfork()vfork() With vfork() the parent process is suspended and the chiWith vfork() the parent process is suspended and the chi

ld process uses the address space of the parent. ld process uses the address space of the parent. If the child process changes any pages of the parent’s adIf the child process changes any pages of the parent’s ad

dress space, the altered pages will be visible to the parendress space, the altered pages will be visible to the parent once it resumes. t once it resumes.

very efficient, dangerous, very efficient, dangerous, to be used when the child p to be used when the child process calls rocess calls exec()exec() immediately after creation. immediately after creation.

Page 29: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Process Creation: Copy-on-Write fork() with COWfork() with COW

Copy-on-Write (COW) allows both parent and child procCopy-on-Write (COW) allows both parent and child processes to initially esses to initially shareshare the same pages in memory. the same pages in memory.

These shared pages are marked as These shared pages are marked as copy-on-write pagescopy-on-write pages, , meaning that if either process writes to a shared page, a cmeaning that if either process writes to a shared page, a copy of the shared page is created. opy of the shared page is created.

Only the pages modified by either process are copied; all Only the pages modified by either process are copied; all non-modified pages may be shared by the parent and chilnon-modified pages may be shared by the parent and child process. d process.

Free pages are allocated from Free pages are allocated from a pool of zeroed-out pagesa pool of zeroed-out pages.. fork() with COW allows more efficient process creation afork() with COW allows more efficient process creation a

s only modified pages are copied.s only modified pages are copied. fork() with COW is safer than vfork().fork() with COW is safer than vfork().

Page 30: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Process Creation: Memory-Mapped Files Standard file I/O operations require disk access Standard file I/O operations require disk access

directlydirectly. . Memory-mapped file I/O allows file I/O to be treated Memory-mapped file I/O allows file I/O to be treated

as routine memory access by as routine memory access by mappingmapping a disk block to a disk block to a page in memory.a page in memory.

A file is initially read using demand paging. A page-A file is initially read using demand paging. A page-sized portion of the file is read from the file system sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses.the file are treated as ordinary memory accesses.

Simplifies file access by treating file I/O through Simplifies file access by treating file I/O through memory rather than memory rather than read(),read(), write()write() system calls. system calls.

The mapped memory acts as a cache for a fileThe mapped memory acts as a cache for a file

Page 31: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Process Creation: Memory-Mapped Files

OS support for memory-mapped filesOS support for memory-mapped files To provide memory mapping only through a To provide memory mapping only through a

specific system call and treat all other file I/O specific system call and treat all other file I/O using the standard system callsusing the standard system calls

To treat all file I/O as memory-mapped, allowing To treat all file I/O as memory-mapped, allowing file access to take place in memory. file access to take place in memory.

Also allows several processes to map the same file Also allows several processes to map the same file allowing the pages in memory to be shared.allowing the pages in memory to be shared.

Page 32: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Process Creation: Memory-Mapped Files

Page 33: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

PAGE REPLACEMENTDoes each page faults at most once?Does each page faults at most once? Suppose we have 40 frames. A process of 10 pages Suppose we have 40 frames. A process of 10 pages

actually uses only 5 of them. actually uses only 5 of them. we could run 8 we could run 8 processes rather than 4 processes.processes rather than 4 processes.

If we increase our degree of multiprogramming, we If we increase our degree of multiprogramming, we are are over-allocatingover-allocating memory memory If we run 6 processes, higher CPU utilization and If we run 6 processes, higher CPU utilization and

throughput and 10 frames to spare. throughput and 10 frames to spare. Suppose they suddenly try to use all 10 of its Suppose they suddenly try to use all 10 of its

pages, pages, needs 60 frames needs 60 frames What to do?What to do?

Page 34: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement I/O Buffers I/O Buffers

Buffers for I/O consumes a significant amount of Buffers for I/O consumes a significant amount of memory. This use can increase the strain on memory. This use can increase the strain on memory-placement algorithmmemory-placement algorithm

Deciding how much memory to allocate to I/O and Deciding how much memory to allocate to I/O and how much to program pages is a significant how much to program pages is a significant challenge. challenge.

Some systems allocate a fixed percentage of Some systems allocate a fixed percentage of memory for I/O buffers, whereas others allow both memory for I/O buffers, whereas others allow both user processes and the I/O subsystem to compete user processes and the I/O subsystem to compete for all system memory. for all system memory.

Over-allocation could cause one process can not Over-allocation could cause one process can not find the frame for its missing pages. find the frame for its missing pages.

Page 35: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: The Need

Page 36: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Options The options for the OSThe options for the OS

The OS could terminate the user processThe OS could terminate the user process The OS could swap out a process, freeing all The OS could swap out a process, freeing all

its frames, and reducing the level of its frames, and reducing the level of multiprogramming. multiprogramming.

The OS could perform page replacement. The OS could perform page replacement.

Page 37: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic SchemeThe Page-fault service routine with page The Page-fault service routine with page

replacementreplacement1.1. Find the location of the desired page on disk.Find the location of the desired page on disk.2.2. Find a free frame:Find a free frame:

If there is a free frame, use it.If there is a free frame, use it. If there is no free frame, use a page If there is no free frame, use a page

replacement algorithm to select a victim frame. replacement algorithm to select a victim frame. Write the victim page to the disk; change the Write the victim page to the disk; change the page and frame tables accordinglypage and frame tables accordingly;;

3.3. Read the desired page into the (newly) free Read the desired page into the (newly) free frame. Update the page and frame tables.frame. Update the page and frame tables.

4.4. Restart the process.Restart the process.

Page 38: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic Scheme

Page 39: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic Scheme With page replacement, the page-fault service time is With page replacement, the page-fault service time is

doubled. doubled. To use modify bit (or dirty bit) to reduce this To use modify bit (or dirty bit) to reduce this

overhead. Each page or frame may have a modify bit overhead. Each page or frame may have a modify bit associated with it in the hardware. The modify bit for associated with it in the hardware. The modify bit for a page is set by the hardware whenever any word or a page is set by the hardware whenever any word or byte in the page is written into, indicating that the byte in the page is written into, indicating that the page has been modified. page has been modified. If modified, page out and page inIf modified, page out and page in If clean, just page in If clean, just page in

the page-fault service is reasonable for only the page-fault service is reasonable for only modified pages are written to disk.modified pages are written to disk.

Page 40: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic Scheme The algorithms for page replacementThe algorithms for page replacement

The lowest page-fault rateThe lowest page-fault rate How to evaluate page replacementHow to evaluate page replacement

Evaluate it on a particular string of Evaluate it on a particular string of memory references and computing the memory references and computing the number of page faultsnumber of page faults

SimulationSimulationImplementationImplementation

Page 41: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic Scheme How to select a reference string (real recorded How to select a reference string (real recorded

data or simulated data)data or simulated data) How to reduce the number of dataHow to reduce the number of data

Just consider the page number only, rather Just consider the page number only, rather than the entire address.than the entire address.

If we have a reference to a page If we have a reference to a page pp, then any , then any immediately following references to page immediately following references to page pp will never cause a page fault. will never cause a page fault.

An example: 0100, 0432,0101,0612, An example: 0100, 0432,0101,0612, 0102,0103,0104,0611 0102,0103,0104,0611 1,4,1,6,1,6 1,4,1,6,1,6

Page 42: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Basic Scheme

Graph of Page Faults Versus The Number of FramesGraph of Page Faults Versus The Number of Frames

Page 43: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: FIFO FIFO Page ReplacementFIFO Page Replacement

To associate with each page the time when that page To associate with each page the time when that page was brought into memorywas brought into memory

To replace the oldest page if necessaryTo replace the oldest page if necessary Implementation Implementation

A FIFO queue to hold all pages in memory.A FIFO queue to hold all pages in memory. To replace the page the head of queue.To replace the page the head of queue. To append at the tail when a page is brought into To append at the tail when a page is brought into

memory. memory. FeaturesFeatures

Easy to understand and easy to implementEasy to understand and easy to implement Its performance is not always good. Its performance is not always good.

Page 44: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: FIFO

Page 45: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: FIFO

FIFO Illustrating Belady’s AnomalyFIFO Illustrating Belady’s Anomaly

Page 46: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: OPT An optimal page-replacement algorithm An optimal page-replacement algorithm

has the lowest page-fault rate of all algorithms, and has the lowest page-fault rate of all algorithms, and will never suffer from Belady’s anomaly.will never suffer from Belady’s anomaly.

The optimal page-replacement algorithmThe optimal page-replacement algorithm Replace the page that will not be used for the longeReplace the page that will not be used for the longe

st period of time. st period of time. 替换最晚才用的页。替换最晚才用的页。

FeaturesFeatures Lowest page fault rate Lowest page fault rate (No Belady’s anomaly)(No Belady’s anomaly) Difficult to implement. Difficult to implement. Can be used for comparison. Can be used for comparison.

Page 47: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: OPT

Page 48: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU FIFO, OPT, LRUFIFO, OPT, LRU

FIFO, looking at the pastFIFO, looking at the past OPT, looking at the futureOPT, looking at the future LRU, looking at the past, predicting the future. LRU, looking at the past, predicting the future.

LRU: least-recently used LRU: least-recently used To associate with each page the time that page’s To associate with each page the time that page’s

last uselast use To replace the page that has not been used for the To replace the page that has not been used for the

longest period of time. longest period of time.

Page 49: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU

Page 50: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU How to implement the LRUHow to implement the LRU

CountersCountersAdd to the CPU a logical clock or counterAdd to the CPU a logical clock or counterAssociate with each entry a TimeOfUse fieldAssociate with each entry a TimeOfUse fieldWhenever a reference to page is the made, the contents Whenever a reference to page is the made, the contents

of the clock register are copied to the TimeOfUse fieldof the clock register are copied to the TimeOfUse fieldTo replace the page with the smallest time value. To replace the page with the smallest time value.

StackStackTo keep a stack of page numbersTo keep a stack of page numbersWhenever a page is referenced, it is moved to the topWhenever a page is referenced, it is moved to the topTo replace the page at the bottom of the stack which is tTo replace the page at the bottom of the stack which is t

he LRU page. he LRU page.

Page 51: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU

Page 52: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU Approximation Many systems provide some help in the form Many systems provide some help in the form

of a reference bit. of a reference bit. A reference bit is associated with each entry A reference bit is associated with each entry

in the page table.in the page table. The reference bit for a page is set by the The reference bit for a page is set by the

hardware when that page is referenced. hardware when that page is referenced. To replace the page which is not referenced. To replace the page which is not referenced.

Page 53: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU ApproximationAdditional-reference-bits algorithmAdditional-reference-bits algorithm To keep an 8-bit byte for each page in a table in memoryTo keep an 8-bit byte for each page in a table in memory At regular intervals (every 100 ms), a timer interrupt At regular intervals (every 100 ms), a timer interrupt

transfers control to the OS. The OS shifts the reference transfers control to the OS. The OS shifts the reference bit for each page into the high-order bit of its 8-bit byte, bit for each page into the high-order bit of its 8-bit byte, shifting the other bits right 1 bit, discarding the low-shifting the other bits right 1 bit, discarding the low-order bit. These 8-bit bytes contain the history of the order bit. These 8-bit bytes contain the history of the page use for the last eight time periods. page use for the last eight time periods.

If we interpret these 8-bit bytes as unsigned integers, the If we interpret these 8-bit bytes as unsigned integers, the page with the lowest number is the LRU page and it can page with the lowest number is the LRU page and it can be replaced. be replaced.

Page 54: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU Approximation Second-chance algorithmSecond-chance algorithm

When a page has been inspected, we check its When a page has been inspected, we check its reference bitreference bit

If the value is 0, replace itIf the value is 0, replace itIf the value is 1, give it a second chance and try If the value is 1, give it a second chance and try

next. Furthermore, set it to 0next. Furthermore, set it to 0 To implement the second-chance algorithmTo implement the second-chance algorithm

A circular queue of pages.A circular queue of pages.

Page 55: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU Approximation

Page 56: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: LRU Approximation Enhanced second-chance algorithmEnhanced second-chance algorithm

(Reference bit, modified bit)(Reference bit, modified bit)(0,0): (0,0): best page to replacebest page to replace(0,1):(0,1): not quite good for replacementnot quite good for replacement(1,0):(1,0): will be used soon will be used soon (1,1):(1,1): worst page to replace. worst page to replace.

Macintosh VMM.Macintosh VMM.

Page 57: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Counting-Based Page Replacement Keep a counter of the number of references Keep a counter of the number of references

that have been made to each page.that have been made to each page.

LFU (Least Frequently Used ) Algorithm: LFU (Least Frequently Used ) Algorithm: replaces page with smallest count.replaces page with smallest count.

MFU (Most Frequently Used ) Algorithm: MFU (Most Frequently Used ) Algorithm: based on the argument that the page with the based on the argument that the page with the smallest count was probably just brought in smallest count was probably just brought in and has yet to be used.and has yet to be used.

Page 58: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Page-Buffering Algorithm To reduce the number of modified pages as many as To reduce the number of modified pages as many as

possiblepossible To maintain a list of modified pagesTo maintain a list of modified pages When the paging device is idle, a modified page is When the paging device is idle, a modified page is

selected and is written to the disk and its modify selected and is written to the disk and its modify bit is then reset. bit is then reset.

to increase the probability that a page will be to increase the probability that a page will be clean when it is selected for replacement and will clean when it is selected for replacement and will

not need to be written out.not need to be written out.

Page 59: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Page-Buffering Algorithm

To keep a pool of free framesTo keep a pool of free frames When a page fault occurs, a victim frame is chosen When a page fault occurs, a victim frame is chosen

as before. as before. The desired page is read into a free frame from the The desired page is read into a free frame from the

pool before the victim is written out. pool before the victim is written out. To allow the process to restart ASAP. To allow the process to restart ASAP.

Page 60: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Page-Buffering Algorithm To keep a pool of free frames and to remember which To keep a pool of free frames and to remember which

page was in each framepage was in each frame If a page is needed again, check the pool. If a page is needed again, check the pool. Maybe the frame containing the original page is Maybe the frame containing the original page is

not used by someone else. So use this. not used by someone else. So use this.

Caching and Buffering are very, very important Caching and Buffering are very, very important techniques. techniques.

Page 61: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Page Replacement: Global vs. Local Allocation GlobalGlobal replacement replacement

A process selects a replacement frame from the set of A process selects a replacement frame from the set of all frames; one process can take a frame from another.all frames; one process can take a frame from another.

The number of frames allocated to a process changes. The number of frames allocated to a process changes. Affected by external circumstancesAffected by external circumstances

LocalLocal replacement replacement Each process selects from only its own set of allocated Each process selects from only its own set of allocated

frames.frames. The number of frames allocated to a process does not The number of frames allocated to a process does not

changechange Not affected by external circumstancesNot affected by external circumstances

In general, global replacement is better. In general, global replacement is better.

Page 62: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

FRAME ALLOCATION For uniprogramming OSFor uniprogramming OS

Consider a single-user system with 128 KB Consider a single-user system with 128 KB memory with page size as 1KBmemory with page size as 1KB

Suppose the OS takes 35KB, 93 frames are lSuppose the OS takes 35KB, 93 frames are left from the user process.eft from the user process.Have no free-frame list poolHave no free-frame list poolHave a free-frame list pool.Have a free-frame list pool.

The user process is allocated any free frame. The user process is allocated any free frame. For multiprogramming OS?For multiprogramming OS?

Page 63: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Frame Allocation

For multiprogramming OSFor multiprogramming OS What is the minimum number of frames for a What is the minimum number of frames for a

process? process? What is the maximum number of frames for a What is the maximum number of frames for a

process?process? How to split the free frames among the processes?How to split the free frames among the processes?

Page 64: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Frame Allocation Minimum number of frames for a processMinimum number of frames for a process

Defined by the architectureDefined by the architecture We must have enough frames to hold all the different pagWe must have enough frames to hold all the different pag

es that any single instruction can reference. es that any single instruction can reference. A machine in which all memory-reference instructions haA machine in which all memory-reference instructions ha

ve only one memory address. ve only one memory address. 2 + 1 (if indirected)2 + 1 (if indirected)

The IBM 370’s MVCThe IBM 370’s MVCInstruction: 6 bytes Instruction: 6 bytes 2 pages2 pagesSource addresses and destination addresses Source addresses and destination addresses 4 pages 4 pages

Some machines that allow multiple levels of indirection.Some machines that allow multiple levels of indirection. Maximum number of frames for a process?Maximum number of frames for a process?

Defined by the amount of available physical memoryDefined by the amount of available physical memory

Page 65: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Frame Allocation: Fixed Allocation Equal allocation – e.g., if 100 frames and 5 Equal allocation – e.g., if 100 frames and 5

processes, give each 20 pages.processes, give each 20 pages. Proportional allocation – Allocate according Proportional allocation – Allocate according

to the size of process.to the size of process.

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i

Page 66: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Frame Allocation: Priority Allocation Use a proportional allocation scheme using a Use a proportional allocation scheme using a

function of priorities and sizes.function of priorities and sizes. If process If process PPii generates a page fault, generates a page fault,

select for replacement one of its frames.select for replacement one of its frames. select for replacement a frame from a select for replacement a frame from a

process with lower priority number.process with lower priority number.

Page 67: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Frame Allocation: Global versus Local Allocation Global replacement:Global replacement:

To allow a process to select a replacement frame from To allow a process to select a replacement frame from the set of all frames, even if that frame is currently allthe set of all frames, even if that frame is currently allocated to some other process.ocated to some other process.

Can increase the number of frames allocated.Can increase the number of frames allocated. Cannot control its page-fault rate.Cannot control its page-fault rate. Greater system throughput. Greater system throughput.

Local replacement: Local replacement: To allow a process to select from only its own set of aTo allow a process to select from only its own set of a

llocated frames. llocated frames. Cannot increase the number of frames allocated.Cannot increase the number of frames allocated. Canot control its page-fault rate. Canot control its page-fault rate.

Page 68: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

THRASHING A process is thrashing if it is spending more time pagiA process is thrashing if it is spending more time pagi

ng and executing. (A student is thrashing sometimes ang and executing. (A student is thrashing sometimes as well (going to the Libs))s well (going to the Libs)) CauseCause SolutionsSolutions

Working-set modelWorking-set modelPage-fault frequencyPage-fault frequency

Page 69: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Cause Cause of thrashingCause of thrashing

While (CPU utilization: too low)While (CPU utilization: too low)

increase the degree of multiprogramming increase the degree of multiprogramming

Processes will take frames away from other Processes will take frames away from other processes (by global replacement)processes (by global replacement)

processes wait for the paging deviceprocesses wait for the paging device

ThrashingThrashing

Page 70: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Cause

Page 71: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Locality The locality model states that, as a process executes, The locality model states that, as a process executes,

it moves locality to locality. it moves locality to locality. A locality is a set of pages that are actively used A locality is a set of pages that are actively used

together. together. A program is generally composed of several A program is generally composed of several

different localities, which may overlap.different localities, which may overlap. If a process has enough frames to contain its If a process has enough frames to contain its

localities, then it run smoothly. localities, then it run smoothly.

Page 72: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Locality Locality In A Memory-Reference PatternLocality In A Memory-Reference Pattern

Page 73: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Locality How to prevent thrashingHow to prevent thrashing

To allocate enough frames to a process to To allocate enough frames to a process to accommodate its current locality. It will fault for the accommodate its current locality. It will fault for the pages in its locality until all these pages are in pages in its locality until all these pages are in memory; then it will not fault again until it changes memory; then it will not fault again until it changes localities. localities.

If we allocate fewer frames that the size of the current If we allocate fewer frames that the size of the current locality, the process will thrash, since it cannot keep locality, the process will thrash, since it cannot keep in memory all the pages that it is actively using. in memory all the pages that it is actively using.

Two approachesTwo approaches Working-set model Working-set model Page-fault frequency Page-fault frequency

Page 74: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set ModelWorking-set window, working setWorking-set window, working set Working-set window Working-set window is a fixed number of page is a fixed number of page

references references Working set is the set of pages in the most recent Working set is the set of pages in the most recent

page references. page references. If a page is in active use, it will be in the working If a page is in active use, it will be in the working

set. set. If it is no longer being used, it will drop from the If it is no longer being used, it will drop from the

working set working set time units after its last reference. time units after its last reference. The working set is an approximation of the The working set is an approximation of the

program’s locality. program’s locality.

Page 75: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set Model

Page 76: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set Model

Working set Working set An approximation of the program’s localityAn approximation of the program’s locality The accuracy of the working set depends on the selectThe accuracy of the working set depends on the select

ion of ion of if if too small too small will not encompass entire locality.will not encompass entire locality. if if too large too large will encompass several localities. will encompass several localities. if if = = will encompass entire program. will encompass entire program.

Changes dynamically Changes dynamically

Page 77: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set Model How does the working-set model works?How does the working-set model works?

To compute the working-set size for each process iTo compute the working-set size for each process in the system, n the system,

To compute the total demand for frames.To compute the total demand for frames.If the total demand is less than the total availabe If the total demand is less than the total availabe

frames, no thrashing will occur. frames, no thrashing will occur. If the total demand is greater than the total availIf the total demand is greater than the total avail

able frames, thrashing will occur.able frames, thrashing will occur.

Page 78: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set Model If one process doesn’t thrash, it should have If one process doesn’t thrash, it should have

enough frames for its working set.enough frames for its working set. If all process don’t thrash, the OS should have If all process don’t thrash, the OS should have

enough frames for total working sets.enough frames for total working sets. If thrash ever happens, the OS swaps some If thrash ever happens, the OS swaps some

processes out to reduce the degree of processes out to reduce the degree of multiprogramming. multiprogramming.

How to keep track of the working set.How to keep track of the working set.

Page 79: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Working-Set Model Approximate with interval timer + a reference bitApproximate with interval timer + a reference bit Example: Example: = 10,000 = 10,000

Timer interrupts after every 5000 time units.Timer interrupts after every 5000 time units. Keep in memory 2 bits for each page.Keep in memory 2 bits for each page. Whenever a timer interrupts, copy and clear the Whenever a timer interrupts, copy and clear the

values of all reference bits to 0.values of all reference bits to 0. If one of the bits in memory = 1 If one of the bits in memory = 1 its page is in its page is in

working set.working set. not completely accurate?not completely accurate?

Improvement Improvement 10 bits for each page 10 bits for each page interrupt every 1000 time units.interrupt every 1000 time units.

Page 80: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Page-Fault Frequency Scheme The working-set model is successful, knowledThe working-set model is successful, knowled

ge of the working-set can be useful for prepagige of the working-set can be useful for prepaging, but it seems a clumsy way to control thrashng, but it seems a clumsy way to control thrashing. ing.

Establish “acceptable” page-fault rate.Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. (cIf actual rate too low, process loses frame. (c

ould be suspended)ould be suspended) If actual rate too high, process gains frame.If actual rate too high, process gains frame.

Page 81: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Thrashing: Page-Fault Frequency Scheme

Page 82: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

OTHER CONSIDERATIONS Prepaging Prepaging Page size selectionPage size selection TLB ReachTLB Reach Program structureProgram structure I/O InterlockI/O Interlock

Page 83: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: Prepaging PrepagingPrepaging

Pure demand paging Pure demand paging the large number of page f the large number of page faults that occur when a process is started. aults that occur when a process is started.

Prepaging: to prevent this high level of initial pagiPrepaging: to prevent this high level of initial paging. ng.

Prepaging: to try to bring into memory at one time Prepaging: to try to bring into memory at one time all the pages that will be needed. all the pages that will be needed.

To bring in the saved working setTo bring in the saved working set Discussion: whether the cost of using prepaging is Discussion: whether the cost of using prepaging is

less than the cost of servicing the corresponding paless than the cost of servicing the corresponding page faults. ge faults.

Page 84: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: Page size Page size selectionPage size selection

Page table size Page table size Internal fragmentationInternal fragmentation I/O overhead (seek time, latency time, transfer timI/O overhead (seek time, latency time, transfer tim

e)e) LocalityLocality Page fault ratePage fault rate The trend is toward larger and larger page size. The trend is toward larger and larger page size.

Page 85: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: TLB reach TLB ReachTLB Reach - The amount of memory - The amount of memory

accessible from the TLB.accessible from the TLB.

TLB Reach = (TLB Size) X (Page Size)TLB Reach = (TLB Size) X (Page Size)

Ideally, the working set of each process is Ideally, the working set of each process is stored in the TLB. Otherwise there is a high stored in the TLB. Otherwise there is a high degree of page faults.degree of page faults.

Page 86: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: TLB Reach Increase the Page SizeIncrease the Page Size. This may lead to an . This may lead to an

increase in fragmentation as not all increase in fragmentation as not all applications require a large page size.applications require a large page size.

Provide Multiple Page SizesProvide Multiple Page Sizes. This allows . This allows applications that require larger page sizes the applications that require larger page sizes the opportunity to use them without an increase in opportunity to use them without an increase in fragmentation.fragmentation.

Page 87: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: Program structure Program structureProgram structure

int A[][] = new int[1024][1024];int A[][] = new int[1024][1024]; Each row is stored in one page.Each row is stored in one page.

Program 1: 1024 x 1024 page faults Program 1: 1024 x 1024 page faults for (j = 0; j < A.length; j++)for (j = 0; j < A.length; j++)

for (i = 0; i < A.length; i++)for (i = 0; i < A.length; i++)A[i,j] = 0;A[i,j] = 0;

Program 2: 1024 page faults Program 2: 1024 page faults for (i = 0; i < A.length; i++)for (i = 0; i < A.length; i++)

for (j = 0; j < A.length; j++)for (j = 0; j < A.length; j++)A[i,j] = 0;A[i,j] = 0;

Page 88: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: I/O Interlock I/O InterlockI/O Interlock – Pages must sometimes be – Pages must sometimes be

locked into memory.locked into memory.

Consider I/O. Pages that are used for copying Consider I/O. Pages that are used for copying a file from a device must be locked from being a file from a device must be locked from being selected for eviction by a page replacement selected for eviction by a page replacement algorithm.algorithm.

Page 89: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Other Considerations: I/O InterlockReason Why Frames Used For I/O Must Be In MemoryReason Why Frames Used For I/O Must Be In Memory

Page 90: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame

Homework 11 66 99 1010 1111 1616

Page 91: CHAPTER 10: VIRTUAL MEMORY Background Background Demand Paging Demand Paging Process Creation Process Creation Page Replacement Page Replacement Frame