virtual memory operating system

29
Virtual Memory

Upload: shaheen-kousar

Post on 09-Jan-2017

113 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: virtual memory Operating system

Virtual Memory

Page 2: virtual memory Operating system

Virtual Memory

Virtual memory is a technique that is used to execute processes that are not in main memory completely. The remaining part are placed on the backing storage.

Demand paging

It is a technique that is used to bring a page into memory only when it is needed.

Page 3: virtual memory Operating system

Demand paging cycle Page is needed reference to it Referenced page not in real memory Page Fault Just not in memory Demand paging Find free frame Swap page into frame Reset tables to indicate page now in memory Restart the instruction that caused the page fault

Page 4: virtual memory Operating system

Page Replacement

“Page replacement completes separation between logical memory and physical memory large virtual memory can be provided on a smaller physical memory”

Page 5: virtual memory Operating system

How to solve page faults?

It terminates the program immediatelyOr

Swaps out program & frees all of its frames It reduces the degree of multiprogramming

Page 6: virtual memory Operating system

Basic Page ReplacementSteps performed by OS while replacing a page upon a page fault:

1. Find the location of the desired page on disk

2. Find a free frame: If there is a free frame, use it If there is no free frame, use a page replacement algorithm to select a victim frame; if the victim page is modified, write it back to disk.

3. Bring the desired page into the (new) free frame; update the page and frame tables

4. Restart the process

Page 7: virtual memory Operating system

Page-replacement algorithm

Want lowest page-fault rate on both first access and re-access

There is particular string of memory references. String is just page numbers, not full addresses Repeated access to the same page does not

cause a page fault Results depend on number of frames available

In all our examples, the reference string of referenced page numbers is 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

Page 8: virtual memory Operating system

First In First Out (FIFO) Algorithm

Oldest page in main memory is the one which will be selected for replacement

Easy to implement, keep a list, replace pages from the tail and add new pages at the head.

Page 9: virtual memory Operating system

First-In-First-Out (FIFO) Algorithm

Adding more frames can cause more page faults!

Belady’s Anomaly

15 page faults

Page 10: virtual memory Operating system

FIFO Illustrating Belady’s Anomaly

Page 11: virtual memory Operating system

Optimal Algorithm Replace page that will not be used for longest period of time How do you know this?

Used for measuring how well your algorithm performs

Page 12: virtual memory Operating system

Least Recently Used (LRU) Algorithm Use past knowledge rather than future Replace page that has not been used in the most amount of time

Page 13: virtual memory Operating system

12 faults – better than FIFO but worse than OPT Generally good algorithm and frequently used

Page 14: virtual memory Operating system

LRU Approximation Algorithm

Reference bitWith each page associate a bit, initially= 0When page is referenced bit set to 1

Page 15: virtual memory Operating system

Additional Reference Bits We can gain additional ordering information

by recording the reference bits at regular interval

We can keep 8-bit byte for each page in a table in memory

The OS shifts the reference bit for each page into the high-order bit of its 8-bit byte, shifting the other bit right by 1 bit and discard the low-order bit

Page 16: virtual memory Operating system

Second Chance Algorithm

This Algorithm is a FIFO replacement algorithm wit consideration given to the reference bit .when a page has been selected for replacement , its reference bit is inspected .

When a page is given a second chance its reference bit is cleared and its arrival time reset to the current time

Page 17: virtual memory Operating system

Enhanced Second-Chance Algorithm

Improve algorithm by using reference bit and modify bit (if available) in concert

1. (0, 0) neither recently used not modified – best page to replace

2. (0, 1) not recently used but modified – not quite as good, must write out before replacement

3. (1, 0) recently used but clean – probably will be used again soon

4. (1, 1) recently used and modified – probably will be used again soon and need to write out before replacement

Page 18: virtual memory Operating system

Allocation of Frames

Each process needs minimum number of pages Various allocation approaches

› fixed allocation › global allocation› local allocation

Page 19: virtual memory Operating system

Fixed Allocation Equal allocation – For example, if there are 100

frames and 5 processes, give each process 20 frames.

Proportional allocation – Allocate according to the size of process

mSspa

m

sS

ps

iii

i

ii

for allocation

frames ofnumber total

process of size

Example: 5964

137127

564137101271064

2

1

2

a

a

ssm

i

Page 20: virtual memory Operating system

Global versus Local Allocation

When a page fault occurs for a process and we need page replacement, there are two general approaches: › Global replacement – select a victim frame

from the set of all frames; one process can take a frame from another

› Local replacement – select a victim frame only from the frames allocated to the process. A process uses always its allocated frames

Page 21: virtual memory Operating system

Thrashing

At some time , the processor may be spend most of its time swapping pages and doing little productive work, this is known as Thrashing.

Page 22: virtual memory Operating system

Figure:

Page 23: virtual memory Operating system

Thrashing Cause

It is caused by under allocation of minimum no. of pages required by a process , forcing it to continuously page fault.

Page 24: virtual memory Operating system

Working setIt is a collection of pages, which is actively referenced by a process . It should be kept in main memory to run efficiently.

Page 25: virtual memory Operating system
Page 26: virtual memory Operating system

Working sets change during process execution . pages are added or deleted and critical changes may occur.

Page 27: virtual memory Operating system

Page Size:It is a hardware design issue . Small pages reduce the amount of internal fragmentation. Large pages reduce the size of the page table.

Page 28: virtual memory Operating system

In general small pages have better locality than large pages.

Page 29: virtual memory Operating system