31
Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physica l Memory

Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Embed Size (px)

Citation preview

Page 1: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Virtual Memory

Why?The need of memory more than the available physical memory.

Process 1

Process 3

Process 4Process 2

PhysicalMemory

Page 2: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Memory Limits for Windows Releaseshttp://msdn.microsoft.com/en-us/library/windows/desktop/aa366778(v=vs.85).aspx

Page 3: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Max. memory per a process

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366778(v=vs.85).aspx#memory_limits

Page 4: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Max. memory per a VM (.NET or Java)

Max. memory per a single objectmax 2GB (.NET) ไม่�ว่�าจะเป็น 32-bit หรื�อ 64-bit version

สรื�าง object Array เกิ�น 2GB ไม่�ได้�

With .NET 4.5, 10 years memory limit of 2 GB is over

ส่�วนใหญ่�การเขี ยนโปรแกรมด้�วย Java หร�อ .NET จะติ�ด้ memory limit ที่ � virtual machine ก�อน

ถ้�าจะที่ะลุ� limit น ไปก"ติ�องเขี ยน C/C++ หร�อเร ยก system call

Page 5: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

No corresponding

physical memory

Virtual memory(a process)

codedataheap

stack

Virtual Memory

pp. 359

As stack and heap grow, more pages will be allocated and mapped to physical memory.

Page 6: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Virtual Memorykernel

Virtual memory(a process)

page 1page 2page 3

Physical memory ม่�จ�ากิ�ด้ RAM แพง

Storage (HD)

page 4page 5

page 1page 2

page 3page 4page 5

Page 7: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

SummaryVirtual memory = mapping + paging + swapping

pp. 362

ม logical ก$บ physical

แบ�งเป&น page ไม�ใช่�

contiguous

swap ก$บด้�ส่ก(

valid/invalid bit ในร)ปน ใช่�บอกว�าpage น$ น อย)�บน memory หร�อไม�

Page 8: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Demand PagingDemand paging = lazy swapping

pp. 361

The OS only swaps a page into memorywhen it is required by a process

Swap-in algorithm

ถ้�า swap-in ไม่�ได้� (memory เต็�ม่) กิ�ต็�องทำ�า swap-out กิ�อน

แบบ lazy จะต็รืงข้�าม่กิ�บแบบintelligent หรื�อ speculative

Page 9: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Page Fault

pp. 363

Locality of Reference เพรืาะธรืรืม่ชาต็�ข้อง memory access ไม่�

random ถ้#งใช� page และ virtual memory ได้�

Page 10: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Copy-on-Write

pp. 368Make another copy of page C when a process writes.

After forking. Parent and child share the same copy.

Page 11: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Demand paging requires1) frame-allocation algorithm2) page-replacement algorithm

Reference string

To know the number of page faults,you need to know the number of max frames.

จะให�กิ�& max. frame per process

page ไหนจะเป็น victim

จะเกิ�ด้ page fault กิ�&ครื�(ง

ม่� maximum frame เทำ�าใด้max = 2 เกิ�ด้ page fault, max = 3 ไม่�เกิ�ด้

Page 12: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

ช�อง load M ค�อ executable code

2 frame แรืกิส�าหรื�บจอภาพ

เต็�ม่หม่ด้ทำ*กิframe

ไม่�ม่�ทำ�&ว่�างส�าหรื�บM

Page 13: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

ช�องน�(กิ�อน หน�าคงเป็น

f

Page 14: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Max. frame per process

Frequency ข้อง กิารืเกิ�ด้ page

fault

Page 15: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Page Replacement Algo. (summary)1) FIFO page replacement

- Belady’s anomaly2) Optimal page replacement- Replace the page that will not be used

for the longest period of time.- Similar to SJF, requiring future knowledge.

3) Least-recently-used (LRU) page replacement- Counter, equip a counter for each entry in page table- Stack, move the referenced page to TOS4) LRU-approximation page replacement

- Additional-reference-bits algorithm- Second-chance algorithm- Enhanced second-chance algorithm

Page 16: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

5) Counting-based page replacement- Least frequently used (LFU) page-replacement algorithm

- Most frequently used (MFU) page-replacement algorithm

6) Page-buffering algorithms (เป็นเทำคน�คเสรื�ม่)

Page Replacement (cont.)

A processMax. #frames = 3

Pool offree frames

HD

frame

frame

frame

frameframe

frame

frameframe

Step 1

Step 2

Delay writing out ordo it when CPU is idle.

Increase response time.

not recently as the previous

Page fault, not choose a victim, borrow a frame

Page 17: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory
Page 18: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Belady’s anomaly(undesired characteristics)

Page 19: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory
Page 20: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory
Page 21: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Using frame number “n”

if n is in stackmove n to TOS

elsepush n

Page 22: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

LSB

Additional-reference-bits algorithm

MSB

0

• Each page has a corresponding 8-bit register.• If the page is accessed, MSB is set to 1.• Every 100 ms, shift-right (÷2) all registers.• The page with the lowest number is the LRU page.

Shift right (less registers than counter)

มองไปในอด้ ติได้�ไกลุมาก เที่�าไรก"ได้� ( ปร$บ 100ms ให�มากขี* น) แติ�ใช่�ที่ � 8 บ�ที่เส่มอ

เหม�อนการใช่� counter แติ�เป&นการประมาณ ค่�า เช่�น page A 10000000 → 10000000 …. → 01000000 page B 00000000 → 10000000 …. → 01000000Page A is older but as time passed no difference between A and B.

เพรืาะ access A กิ�บ access B ม่�นเกิ�ด้ใกิล�กิ�นม่ากิ กิารืป็รืะม่าณค�าทำ�าให�แยกิไม่�ได้� แต็� ผิ�ด้ไม่�เกิ�น 100ms

Example1000 0000100ms0100 0000100ms0010 0000access1010 0000 100ms0101 0000

Counting is more expensive then shifting

access A access B

Page 23: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

FIFO + Second ChanceReferenced Set ref. bit to 11 Give the second chance, clear0 Replace

Step1. ใช�หล�กิ FIFO เรื�&ม่จากิห�ว่ค�ว่2. ถ้�า ref bit = 0, ได้� victim แล�ว่ ถ้�า ref bit = 1, clear ไป็ต็�ว่ถ้�ด้ไป็3. อาจจะต็�องว่นม่าเรื�&ม่ใหม่� ( ได้� second chance กิ�นหม่ด้)4. ถ้�าได้� victim แล�ว่ ให�โหลด้new page ม่าทำ�บ victim และให�ไป็ ต็�อทำ�ายค�ว่ ต็าม่หล�กิกิารื FIFOIf a page is used often enough to keep its reference bit set, it will never been replaced.

Adva

nce

poin

ter a

ndcl

ear r

efer

ence

bits

Current pointer

Victim

Clear

Clear

Load new page,set ref bit to 1move to the end of Q

Page 24: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Enhanced second-chance algorithm (modify bit)modify bit 0 = not modified 1 = modified

(0, 0) neither recently used nor modified – best page to replace(0, 1) not recently used but modified – not quite as good and need writing disk(1, 0) recently used but clean – probably will be used again soon(1, 1) recently used and modified – used again soon and need writing disk

last choice

first choice

We may have to scan the circular queue several times.

The major diff. with second chance algo. isthe preference to reduce I/O traffic.

Page 25: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Allocation of Frames1) Minimum number of frames- Instruction set architecture:

add a1 a2 a3 min = 3ld r1 a4 min = 1

2) Allocation algorithms- Equal allocation ให�ทำ*กิ process เทำ�า ๆ กิ�น- Proportional allocation เกิล�&ยให�ต็าม่คว่าม่ต็�องกิารืใช�

memory ข้องแต็�ละ process

Page 26: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Global vs. Local allocation1) Local allocation

- a process uses max frames. - when requesting a free frame, choose a victim from its own set of allocated frame.

2) Global allocation- choose a victim from the set of all frames,

even if that frame is currently allocated tosome other process.

Page 27: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

ThrashingDefinition: high paging activity.

#process ใน ready queue ม 1 process CPU จะ idle เพราะรอ

I/O แลุะไม�ม process

อ��น ให� execute

ม หลุาย process เก�น ไป แย�ง frame ก$น

OS ติ�อง predict ได้�ว�าจะเก�ด้thrashing

ให�ลุด้ degree of multiprogramming การ predict ติ�องใช่� working set

model

Page 28: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Working-Set Model Working set = set of pages in the most recent ∆ page references.

Demand

เช่�น P1 ใช่� WSS = 8, P2 ใช่� WSS = 6

= 8 + 6 =14

ถ้�า demand > supply (allocated frames) ก"เพ��ม frame ให�แติ�ลุะ process (equal or proportional)

แติ�ถ้�าไม�ม frame เหลุ�อแลุ�ว ให�ลุด้ degree of multiprogramming หร�อลุด้จ.านวน process ในready queue

Page 29: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Page-Fault Frequency

Page 30: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

Working sets and page fault rates

ถ้�าว่�ด้ในช�ว่งเว่ลาน�( จะได้�working set

น�(เสม่อ หรื�อ เกิ�อบๆ

เรื�&ม่เกิ�ด้ page fault เน�&องจากิprocess ย�ายไป็ working set ใหม่�

page fault ต็กิ โหลด้working set เข้�าม่าทำ*กิ frame แล�ว่

พฟื0transition จาก working set น*งไปย$งอ กworking น*งธรืรืม่ชาต็� ขีอง process จะ

เป&นแบบน

1 1 2 1 3 1 2 3 1 2 3 2 1 2 3 2 1 4 5 4 6 5 5 6 6 4 5 4 4 4

Page 31: Virtual Memory Why? The need of memory more than the available physical memory. Process 1 Process 3 Process 4 Process 2 Physical Memory

HomeworkPage faults / sec

time

1. Thrashing or2. Moving from a working set to another working set ?

Hint: ติ�องด้)ขี�อม)ลุอะไรเพ��มบ�าง? assume ว�าเรา ไม่�รื0� page fault rate ที่ �เวลุา < t1 แลุะ > t2

t1 t2

อ�ต็รืากิารืเกิ�ด้ page fault

เพ�&ม่ข้#(นอย�างรืว่ด้เรื�ว่