59
IOS103 OPERATING SYSTEM VIRTUAL MEMORY

IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Embed Size (px)

DESCRIPTION

IOS103 OPERATING SYSTEM VIRTUAL MEMORY. Objectives. At the end of the course, the student should be able to: Define virtual memory; Discuss the demand paging; Define page replacement; - PowerPoint PPT Presentation

Citation preview

Page 1: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

IOS103

OPERATING SYSTEMVIRTUAL MEMORY

Page 2: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Objectives

At the end of the course, the student should be able to:•Define virtual memory;•Discuss the demand paging;•Define page replacement;•Discuss different page-replacement algorithms such as FIFO, Optimal Algorithm, LRU Algorithm and Counting-Based Page Algorithm.

Page 3: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryIntroduction

• The various memory management strategies that are used in computer systems have the same goal: to keep many processes in memory simultaneously to allow multiprogramming. However, they tend to require the entire process to be in memory before the process can execute.

Page 4: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Virtual Memory is a technique that allows the execution of processes that may not be completely in memory. One major advantage of this scheme is that programs can be larger than physical memory. 

• Further, virtual memory abstracts main memory into an extremely large, uniform array of storage, separating logical memory as viewed by the user from physical memory. This technique frees programmers from the concerns of memory-storage limitations.

Page 5: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Requiring that the entire process to be in main memory before they can be executed limits the size of a program to the size of physical memory.

• However, an examination of real programs shows that, it need not be in main memory at the same time. For example: 1. Programs often have code to handle unusual error conditions. Since these errors seldom, if ever, occur in practice, this code is almost never executed.

Page 6: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

2. Arrays, lists, and tables are often allocated more memory than they actually need. An array may be declared 100 by 100 elements, even though it is seldom larger than 10 by 10 elements.3. Certain options and features of a program may be used rarely. •Even in those cases where the entire program is needed, it may not all be needed at the same time.

Page 7: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryThe ability of execute a program that is only partially in memory would confer many benefits:1. A program would no longer be constrained by the amount of physical memory that is available. Users would be able to write programs for an extremely large virtual-address space.2. Because each user program could take less physical memory, more programs could run at the same time.3. Less I/O would be needed to load or swap each user program into memory, so each program would run faster.

Page 8: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Virtual memory is the separation of user logical memory from physical memory. This separation allows programmers to have a very large virtual memory when only a small physical memory is available.

Page 9: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memorypage 0

page 1

page 2

page 3

page n

.

.

.

virtualmemory

memorymap

physicalmemory

Page 10: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Virtual memory makes the task of programming much easier, since the programmer no longer needs to worry about the amount of physical memory available but can concentrate instead on the programming problem.

Page 11: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryDemand Paging

• A demand-paging system is similar to a paging system with swapping. However, instead of swapping the entire process into memory, the OS (particular the pager) swaps only the necessary pages into memory (lazy swapping).

Page 12: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryDemand Paging

A

B

C

4

6

9

v A

C

F

A B

C D E

F

logicalmemory

pagetable

physicalmemory

D

E

F

G

H

vi

i

ivii

0

1

2

3

4

5

6

7

01234567

2

3

4

5

6

7

8

9

10

11

1

0

12

13

valid-invalidbit

Page 13: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryDemand Paging

• There is an additional bit in the page table which is the valid-invalid bit. This bit is set to valid to indicate that a corresponding page is in memory. This bit is set to invalid to indicate that the corresponding page is in secondary storage.

• If a process tries to use a page that is not in physical memory, then a page-fault will occur. This will cause a trap to the operating system indicating an invalid address error.

Page 14: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Steps in handling a page fault:

load M

operatingsystem

i

freeframe

reference1

2 trap

3 page is in disk

4bring inmissingpage

5 resetpagetable

page table

physicalmemory

6

restartinstruction

Page 15: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Steps in handling a page fault:

1. Check the internal page table of the process to determine whether the reference was a valid or invalid memory access.2. If the reference is invalid (the page is not in main memory), an illegal address trap is initiated and the process is temporarily terminated.3. Find a free frame in main memory and allocate this to the incoming page.

Page 16: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Steps in handling a page fault:

4. Schedule a disk operation to read the desired page into the newly allocated frame.5. When the disk read is complete, modify the page table of the process to indicate that the page is now in memory.6. Restart the instruction that was interrupted by the illegal address trap. The process can now access the page as if it had always been in memory.

Page 17: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• In the extreme case, the system could start executing a process with no pages in memory. The process would immediately fault for the page with the first instruction.

• After the first page is brought into memory, the process would continue to execute, faulting as necessary until every page that it needed was actually in memory. This is pure demand paging: never bring a page into memory until it is required.

Page 18: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory• The principle of locality of reference ensures

that programs do not access a new page of memory with each instruction execution.

 • The effectiveness of the demand paging is based

on a property of computer programs called the locality of reference. Analysis of programs shows that most of their execution time is spent on routines in which many instructions are executed repeatedly. These instructions may constitute a simple loop, nested loops, or a few procedures or functions that repeatedly call each other.

Page 19: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• In other words, locality of reference states that many instructions in a few localized areas of the program are repeatedly executed during some period of time and that the remainder of the program is accessed relatively infrequently. This results in a reasonable performance from demand paging. 

• Page faults slow down the execution of a process since the page must first be read from secondary storage, and then access the desired word.

Page 20: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• The effective access time for a demand-paged memory is:

effective access time = (1 - p) ma + p page fault time

 where:

 p is the probability of a page faultma is the memory access time

Page 21: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Typical values for the page fault time would be about several milliseconds.Example:

 p = 0.10ma = 100 nspage fault time = 25 ms

 effective access time = (1 - 0.10) (100 x 10-9)

+ 0.10 (25 x 10-3) = 2.5 ms

Page 22: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• The page fault time is the time necessary to service a page fault. A page fault causes the following sequence to occur:

1. Trap to the operating system.2. Save the user registers and process state.3. Determine that the interrupt was a page fault.4. Check that the page reference was legal and

determine the location of the page on the disk.

Page 23: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

5. Issue a read from the disk to a free frame:5.1. Wait in queue for this device until the read request is serviced.5.2. Wait for the device seek and/or latency time.5.3. Begin transfer of the page to the free frame.6. Allocate the CPU to some other user while waiting.7. Interrupt from the disk (I/O completed).

Page 24: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

8. Save the registers and process state for the other user.9. Determine that the interrupt was from the disk.10. Correct the page table and other tables to show that the desired page is now in memory.11. Wait for the CPU to be allocated to this process again.12. Restore the user registers, process state, and new page table, then resume the interrupted instruction.

Page 25: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• A good demand-paging system should have values for effective access time close to the actual memory access time:

effective access time ma  • However, this would require that the

probability of a page fault p should be very, very small (almost zero). 

Page 26: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Example:

100 x 10-9 = (1 - p) (100 x 10-9) + p (25 x 10-3)

p = 0.000004

• This implies that the probability of a page fault should be close to 0.00004% or 1 page fault out of 2,500,000 memory accesses.

Page 27: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• If is important to keep the page-fault rate low in a demand-paging system. Otherwise, the effective access time increases, slowing down process execution dramatically.

Page 28: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryPage Replacement• A problem occurs if there is a need to transfer a page

from disk to memory but there is no memory space available (there are no free frames). In other words, memory is over-allocated. 

• The operating system has several options at this point. It could terminate the user process. However, demand paging is the operating system’s attempt to improve the computer system’s utilization and throughput. Users should not be aware their processes are running on a paged system – paging should be logically transparent to the user.

Page 29: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryPage Replacement

• Another possibility is page replacement. In this scheme, the operating system removes or replaces one of the existing pages in memory to give way for the incoming page.

 • A page replacement algorithm is necessary to

select which among the pages currently residing in memory will be replaced.

Page 30: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryPage Replacement

• Page replacement takes the following approach. If no frame is free, the system finds one that is currently being used and frees it. Freeing a frame means transferring its contents to the disk and changing the page table (and all other tables) to indicate that the page is no longer in memory.

Page 31: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryPage Replacement

f 0 v i

f v

victimf

page table

physicalmemory

1 swap outvictim page2

change toinvalid

3 swap desiredpage in

4 reset pagetable for newpage

Page 32: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryThe page fault service routine is now modified to include page replacement:1. Find the location of the desired page on the disk.2. Find a free frame:2.1. If there is a free frame, use it.2.2. Otherwise, use a page-replacement algorithm to select a victim frame.2.3. Write the victim page to the disk; change the page and frame tables accordingly.3. Read the desired page into the (newly) free frame; change the page and frame tables.4. Restart the user process.

Page 33: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• Notice that, if no frames are free, two page transfers (one out and one in) are required. This situation effectively doubles the page-fault service time and will increase the effective access time accordingly. 

• To reduce this overhead, a modify or dirty bit is necessary for each page or frame. The modify bit for a page is set whenever any word or byte is written into, indicating that the page has been modified.

Page 34: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

• It is no longer necessary to swap out pages whose modify bit is 0 (there was no modification). An incoming page may simply overwrite an unchanged page.

• There are two major problems in the implementation of demand paging:

1. Frame AllocationHow many frames will the operating system

allocate to a process, particularly if there are multiple processes?

Page 35: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

2. Page Replacement How will the operating system select pages that are to be removed from memory to give way for incoming pages?

Page 36: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryPage-Replacement Algorithms

• A good page-replacement algorithm is one with a low page-fault rate.

• An algorithm is evaluated by running it on a particular string of memory references and computing the number of page faults. The string of memory references is called the reference string.

Page 37: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm•This is the simplest page-replacement algorithm. When a page must be replaced, the oldest page is chosen.  Example: Assume that there are three frames in memory and that the following is the page reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

Page 38: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual Memory

77 0

7

0

17

0

1

22

0

1

0 32

3

1

02

3

0

44

3

0

24

2

0

34

2

3

00

2

3

30

1

3

2 1 20

1

2

07

1

2

1 7 07

0

2

17

0

1

• There are 15 faults altogether.

Page 39: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

•It is not strictly necessary to record the time when a page is brought in. The operating system simply creates a FIFO queue to hold all pages in memory. The page at the head of the queue is replaced if a frame is needed. When a page is brought into memory, it is inserted at the tail of the queue.

Page 40: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

•The FIFO page-replacement algorithm is easy to understand and program. However, its performance of is not always good. The page replaced may be an initialization module that was used a long time ago and is no longer needed. On the other hand, it could contain a heavily used variable that was initialized early and is in constant use.

Page 41: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

•Notice that, even if algorithm selects for replacement a page that is in active use, everything still works correctly. After an active page is removed to bring in a new one, a fault occurs almost immediately to retrieve the active page. Thus, a bad replacement choice increases the page-fault rate and slow process execution, but does not cause incorrect execution.

Page 42: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

•As a general rule, the more frames available in physical memory, the lower the page-fault rate. •However, there are some instances where the page-fault rate may increase as the number of physical memory frames increases. This is known as Belady’s Anomaly.

Page 43: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

•The FIFO algorithm suffers from Belady’s Anomaly.  

Example:  Consider the reference string:  1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Page 44: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

Number of Frames Page Faults 

1 12 2 12 3 9 4 10 5 5 6 5

 

Page 45: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryFirst-In First-Out (FIFO) Algorithm

1 2 3 4 5 6

2

4

6

8

10

12

14

number of frames

num

ber o

f pag

e fau

lts

Page 46: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryOptimal Algorithm

• An optimal algorithm has the lowest page-fault rate of all algorithms.

 • In an optimal algorithm, the page that will be

replaced is the one that will not be used for the longest period of time.

Page 47: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryOptimal Algorithm

Example:

77 0

7

0

17

0

1

22

0

1

0 32

0

3

0 42

4

3

2 3 02

0

3

32

0

1

2 1 2 07

0

1

1 7 0 1

Page 48: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryOptimal Algorithm

• In the example, the optimal page-replacement algorithm produced only 9 page faults as compared to the FIFO algorithm which produced 15 page faults. If the first three page faults were ignored (since all algorithms suffer from these initialization page faults), then the optimal algorithm is twice as good as FIFO.

Page 49: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryOptimal Algorithm

• Unfortunately, this algorithm is difficult to implement, since it requires future knowledge of the reference string. The optimal algorithm is used mainly for comparison studies.

Page 50: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

• The LRU algorithm uses the recent past to approximate the near future. It simply replaces the page that has not been used for the longest period of time.

Example:

77 0

7

0

17

0

1

22

0

1

0 32

0

3

0 44

0

3

24

0

2

34

3

2

00

3

2

31

3

2

2 1 21

0

2

01

0

7

1 7 0 1

Page 51: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

• The LRU algorithm produced 12 page faults. Although it is not as good as the 9 of the optimal algorithm (in fact, no other algorithm will produce less than 9 page faults for this example), it is much better than the 15 of the FIFO algorithm. 

• The LRU policy is often used as a page replacement algorithm and is considered to be quite good. The major problem is how to implement LRU replacement.

Page 52: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

• The LRU algorithm produced 12 page faults. Although it is not as good as the 9 of the optimal algorithm (in fact, no other algorithm will produce less than 9 page faults for this example), it is much better than the 15 of the FIFO algorithm. 

• The LRU policy is often used as a page replacement algorithm and is considered to be quite good. The major problem is how to implement LRU replacement.

Page 53: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

• An LRU page-replacement algorithm may require substantial hardware assistance. The problem is to determine an order for the frames defined by the time of last use. Two implementations are feasible:

Page 54: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

1. Counters. In the simplest case, the operating system associates with each page-entry table a time-of-use field, and add to the CPU a logical clock or counter. Whenever a process references a page, the system copies the contents of CPU’s clock register to the time-of-use field. In this way, the system has the “time” of the last reference to each page. The system replaces the page with the smallest time value. This solution obviously requires hardware support.

Page 55: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryLeast Recently Used (LRU) Algorithm

2. Stack. Another implementation method is through the use of a stack. The system maintains a stack of page numbers. Whenever a process references a page, the system places the page number on top of the stack. This ensures that the page number at the bottom of the stack is for the least recently used page. This approach is particularly appropriate for software or microcode implementations of the LRU algorithm.

Page 56: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryCounting-Based Page Replacement

• Counting-based Page Replacement keeps a counter of the number of references that have been made to each page.

 • Two examples of Counter-Based Page

Replacement algorithms:

Page 57: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryCounting-Based Page Replacement

1. The Least Frequently Used (LFU) Algorithm requires that the page with the smallest count be replaced. The reason for this selection is that an actively used page should have a large reference count.

Page 58: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryCounting-Based Page Replacement

• This algorithm suffers from the situation in which a page is used heavily during the initial phase of a process, but then is never used again. Since it was used heavily, it has a large count and remains in memory even though it is no longer needed.

Page 59: IOS103 OPERATING SYSTEM VIRTUAL MEMORY

Virtual MemoryCounting-Based Page Replacement

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