Você está na página 1de 48

Bai giang 7 : Bo nh Ao

n n n n

Van e vi Real Memory Y tng Virtual Memory Thc hien Virtual Memory Cac chien lc cua Virtual Memory
n n n

Chien lc nap Chien lc thay the trang Chien lc cap phat khung trang Nguyen nhan Giai phap
Trn Hnh Nhi 1

Hien tng thrashing


n n

5/5/2009

Cac cap bo nh
n

Cho en nay : Nap toan bo tien trnh vao bo nh roi thc hien no...
n

Neu kch thc tien trnh ln hn dung lng bo nh chnh ?


Memory Cache Registers

5/5/2009

Trn Hnh Nhi

Giai phap
n

Tai mot thi iem ch co 1 ch th c thi hanh


n

Tai sao phai nap tat ca tien trnh vao BNC cung 1 luc ? Cho phep nap va thi hanh tng phan tien trnh
n

Y tng
n

Ai ieu khien viec thay oi cac phan c nap va thi hanh ?

Tai mot thi iem ch gi trong BNC cac ch th va d lieu can thiet tai thi iem o
n

Cac phan khac cua tien trnh nam au ?

Giai phap Bo nh ao (virtual memory)

5/5/2009

Trn Hnh Nhi

Virtual Memory
Neu co mot Virtual Memory vi dung lng rat rat ln cho LTV lam viec... Hoan ho !
Virtual Memory Memory Cache Registers

5/5/2009

Trn Hnh Nhi

Y tng
n

Tach biet KGC va KGVL


n n

Giai phap cua HH : Nap tng phan tien trnh n Phan chia KGC thanh cac phan ?
n

LTV : moi tien trnh lam viec vi KGC 2m cua mnh (a ch t 0 (2m -1)) HH : chu trach nhiem nap cac KGC vao mot KGVL chung

M rong BNC e lu tr cac phan cua tien trnh cha c nap


n

Paging/Segmentation

Nhan biet phan nao cua KGC cha c nap ?


n

Dung BNP(disk) e m rong BNC

C che chuyen oi qua lai cac phan cua tien trnh gia BNC va BNP
n

Bo sung bit c hieu e nhan dang tnh trang cua mot page/segment la a c nap vao BNC hay cha

Swapping...

5/5/2009

Trn Hnh Nhi

Virtual Memory vi c che phan trang (Paging)


n n

Phan chia KGC thanh cac page Dung BNP(disk) e m rong BNC, lu tr cac phan cua tien trnh cha c nap Bo sung bit c hieu trong Page Table e nhan dang tnh trang mot page a c nap vao BNC hay cha .

5/5/2009

Cau truc mot phan t trong Page Tables


Trn Hnh Nhi 6

Lu tr KGC au ?
S dung bo nh phu e lu tr tam thi cac trang cha s dung P DISK

RAM

5/5/2009

Trn Hnh Nhi

Virtual Memory

virtual address space

10

11

12

13

14

15

16

17

18

physical memory

13

18 3

5/5/2009

Trn Hnh Nhi

Memory Lookup
present bit Page table
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 000 000 000 000 111 000 101 000 000 000 011 100 000 110 001 010 0 0 0 0 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0

Outgoing physical address (0x6004, 24580)

4-bit index into page table virtual page = 0x0010 = 2

12-bit offset

Incoming virtual address (0x2004, 8196)


5/5/2009

0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0

Trn Hnh Nhi

Memory Lookup
present bit Page table
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 000 000 000 000 111 000 101 000 000 000 011 100 000 110 001 010 0 0 0 0 1 0 1 0 0 0 1 1 1 0 1 1

Outgoing physical address

PAGE FAULT
4-bit index into page table virtual page = 0x0010 = 2

12-bit offset

Incoming virtual address (0x2004, 8196)


5/5/2009

0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0

Trn Hnh Nhi

10

Demand Paging
int i,j; main () { i = 5; j = 2; } i=5 j=2 j i

emacs i j gcc code KGVL


11

KGDC Khi nap mot tien trnh mi, ch nap vao BNC page cha entry code Khi truy xuat en mot ch th hay d lieu, page tng ng mi c nap vao BNC
5/5/2009 Trn Hnh Nhi

Swapping

5/5/2009

Trn Hnh Nhi

12

Demand Paging + Swapping


int i,j; main () { i = 5; j = 2; } i

emacs
j

i j
i=5 j=2

gcc code
Trn Hnh Nhi

KGDC
5/5/2009

KGVL

13

Bo nh ao = True lie
Ngi dung : s hu bo nh vo han, rieng biet He ieu hanh : tham lang thc hien qua trnh swapping
# of references Memory address

10% RAM + 90% DISK

RAM
5/5/2009 Trn Hnh Nhi

DISK
14

Thc hien Bo nh ao
Bang trang : them 1 bit valid/invalid e nhan dien trang a hay cha c nap vao RAM
Frame 17 4183 177 5721 valid/invalid 1 0 1 0 Disk Mem

Truy xuat en mot trang cha c nap vao bo nh :


loi trang (page fault)

5/5/2009

Trn Hnh Nhi

15

Page Tables

X ly loi trang
OS 3

xac nh v tr lu trang tren a loi trang


2

truy xuat
1

nap M

i 6

swap out trang nan nhan

Bo nh ao

tai kch hoat tien trnh

Page Table cap nhat bang trang


5

frame trong
4

Bo nh vat ly

mang trang can truy xuat vao bo nh

5/5/2009

Trn Hnh Nhi

17

Cac bc x ly loi trang


1. 2.

3. 4.

Kiem tra truy xuat en bo nh la hp le hay bat hp le Neu truy xuat bat hp le : ket thuc tien trnh Ngc lai : en bc 3 Tm v tr cha trang muon truy xuat tren a. Tm mot khung trang trong trong bo nh chnh :
a. b.

Neu tm thay : en bc 5 Neu khong con khung trang trong, chon mot khung trang nan nhan e swap out, cap nhat bang trang tng ng roi en bc 5

5.

6.

Chuyen trang muon truy xuat t bo nh phu vao bo nh chnh : nap trang can truy xuat vao khung trang trong a chon (hay va mi lam trong ) ; cap nhat noi dung bang trang, bang khung trang tng ng. Tai kch hoat tien trnh ngi s dung.
Trn Hnh Nhi 18

5/5/2009

Cac cau hoi


1.

Chon trang nao e nap ? => Chien lc nap


Demand Paging / Prepageing

2.

Chon trang nan nhan ? => Chien lc thay the trang


FIFO / OPTIMAL/LRU

3.

Cap phat khung trang => Chien lc cap phat khung trang
Cong bang/ Ty le...

5/5/2009

Trn Hnh Nhi

19

Chien lc nap
n

Quyet nh thi iem nap mot/nhieu page vao BNC


n n

Nap trc : lam sao biet ? =>prepaging Nap sau : tan suat loi trang cao ? => pure demand paging Nap san mot so trang can thiet vao BNC trc khi truy xuat chung Ch nap trang khi c yeu cau truy xuat en trang o

Prepaging :
n

Demand paging :
n

ld init pages

ld page

ld page

ld page

...

init pages =
5/5/2009 Trn Hnh Nhi

?
20

Chien lc thay the trang (Page Replacement)


n

Muc tieu : n thay the trang sao cho tan suat xay ra loi trang thap nhat anh gia n S dung so frame cu the n Gia s co mot chuoi truy xuat cu the
n n

adresse : 0100, 0432, 0101, 0612, 0102, 0103, 0104, 0611 # page : 1, 4, 1, 6, 1, 1, 1, 6,

Thc hien mot thuat toan thay the trang tren chuoi truy xuat nay n em so loi trang phat sinh Chuoi truy xuat n 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 n 3 frames
n

5/5/2009

Trn Hnh Nhi

21

Chien lt thay the trang


n n n

FIFO Optimal LRU (Least Recently Used)

5/5/2009

Trn Hnh Nhi

22

Chien lc thay the trang FIFO


n

Nguyen tac : Nan nhan la trang gia nhat


n

Thc hien
n
n

c nap vao lau nhat trong he thong Lu thi iem nap, so sanh e tm min

victim

add

To chc FIFO cac trang theo th t nap


n

Chi ph cao

Nhan xet
n n n

Trang au danh sac la nan nhan

n gian Cong bang ? Khong xet en tnh s dung !


n

Trang c nap vao lau nhat co the la trang can s dung thng xuyen !

5/5/2009

Trn Hnh Nhi

23

V du : FIFO

7 7

0 7 0

1 7 0 1

2 7 2 0 1 *

0 2 0 1

3 2 3 0 1 *

0 2 3 1 0 *

4 4 2 3 0 *

2 4 3 2 0 *

3 4 2 0 3 *

0 4 0 2 3 *

3 0 2 3

2 0 2 3

1 0 2 1 3 *

2 0 1 2 3 *

0 0 1 2

...

5/5/2009

Trn Hnh Nhi

24

FIFO va hieu ng Belady


n

S dung cang nhieu frame...cang co nhieu loi trang !

5/5/2009

Trn Hnh Nhi

25

Chien lc thay the trang : Optimal


n

Nguyen tac : Nan nhan la trang lau s dung en nhat trong tng lai
n

victim AGBDCABCABCGABC Cur page

Lam sao biet ?

Nhan xet n Bao am tan suat loi trang thap nhat n Khong kha thi !

5/5/2009

Trn Hnh Nhi

26

V du : Optimal

7 7

0 7 0

1 7 0 1

2 7 2 0 1 *

0 2 0 1

3 2 0 3 1 *

0 2 0 3

4 2 4 0 3 *

2 2 4 3

3 2 4 3

0 2 0 4 3 *

3 2 0 3

2 2 0 3

1 2 0 1 3 *

2 2 0 1

0 0 1 2

...

5/5/2009

Trn Hnh Nhi

27

Chien lc thay the trang : LRU


n

Nguyen tac : Nan nhan la trang lau nhat cha s dung en trong qua kh
n

Nhn lui : u thong tin Xap x Optimal Thc hien ?

Nhan xet
n n

victim AGBDCABCABCGABC Cur page

5/5/2009

Trn Hnh Nhi

28

V du : LRU

7 7

0 7 0

1 7 0 1

2 7 2 0 1 *

0 2 0 1

3 2 0 3 1 *

0 2 0 3

4 2 4 0 3 *

2 4 0 3 2 *

3 4 3 0 2 *

0 0 4 3 2 *

3 0 3 2

2 0 3 2

1 0 1 3 2 *

2 1 3 2

0 1 3 0 2 *

...

5/5/2009

Trn Hnh Nhi

29

Thc hien LRU


n

S dung bo em:
n n n

Them trng reference time cho moi phan t trong bang trang Them vao cau truc cua CPU mot bo em counter. moi lan co s truy xuat en mot trang trong bo nh
n n

gia tr cua counter tang len 1. gia tr cua counter c ghi nhan vao reference time cua trang tng ng.

thay the trang co reference time la min . to chc mot stack lu tr cac so hieu trang moi khi thc hien mot truy xuat en mot trang, so hieu cua trang se c xoa khoi v tr hien hanh trong stack va a len au stack. trang nh stack la trang c truy xuat gan nhat, va trang ay stack la trang lau nhat cha c s dung..
Trn Hnh Nhi 30

S dung stack:
n n

5/5/2009

Thc hien LRU vi stack

5/5/2009

Trn Hnh Nhi

31

Thc hien LRU : thc te


n

He thong c ho tr phan cng hoan chnh e cai at LRU ?


n

ng co m ! gan vi mot phan t trong bang trang. c khi gan la 0 c phan cng at gia tr 1 moi lan trang tng ng c truy cap c phan cng gan tr ve 0 sau tng chu ky qui nh trc.

He thong ch c trang b them mot bit reference :


n n n n

Bit reference ch giup xac nh nhng trang co truy cap, khong xac nh th t truy cap
n n

Khong cai at c LRU Xap x LRU...

reference

modify

frame

protect

5/5/2009

Trn Hnh Nhi

32

Xap x LRU : S dung cac bits History


thi gian
0 0 0 0 0 0 1 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0

4-53

bit reference

cac bits history

s dung them N bit history phu tr Sau tng chu ky, bit reference se c chep lai vao mot bit history trc khi b reset
N bit history se lu tr tnh hnh truy xuat en trang trong N chu ky cuoi cung.
5/5/2009 Trn Hnh Nhi 33

Thi gian

0 0 0 0 0

0 1 0 1 0

0 1 0 1 0

Thi gian

0 0 0 0 0

0 1 0 1 0

1 0 1 1 0

1 0 1 1 0

0 1 0 1 0

Thi gian

0 0 0 0 0

0 1 0 1 0

1 0 1 1 0

0 1 0 1 0

0 1 0 1 0

1 0 1 1 0

0 1 0 1 0

Thi gian

0 0 0 0 0

0 1 0 1 0

1 0 1 1 0

0 1 0 1 0

1 1 0 0 0

1 1 0 0 0

0 1 0 1 0

1 0 1 1 0

0 1 0 1 0

Xap x LRU : C hoi th 2 (Clock algorithme)


n n n

S dung mot bit reference duy nhat. Chon c trang nan nhan theo FIFO Kiem tra bit reference cua trang o :
n n

Neu reference = 0, ung la nan nhan roi J Neu reference = 1, cho trang nay mot c hoi th hai
n n

reference = 0 thi iem vao Ready List c cap nhat lai la thi iem hien tai.

n n

Chon trang FIFO tiep theo... Nhan xet :


n

Mot trang a c cho c hoi th hai se khong b thay the trc khi he thong a thay the het nhng trang khac. Neu trang thng xuyen c s dung, bit reference cua no se duy tr c gia tr 1, va trang hau nh khong bao gi b thay the.
Trn Hnh Nhi 38

5/5/2009

Xap x LRU : C hoi th 2 (Clock algorithme)


0 0 page# page# page# page# page# page# page#

4-55

Trang FIFO Trang FIFO Nan nhan

0 1 1 0 1 0 1 0 1 1 0 1 0 1

5/5/2009

Trn Hnh Nhi

39

Xap x LRU : NRU (Not Recently Used)


n n

S dung 2 bit Reference va Modify Vi hai bit nay, co the co 4 to hp tao thanh 4 lp sau :
n n

Priority 4 3 2 1

R 0 0 1 1

M 0 1 0 1

(0,0) khong truy xuat, khong sa oi (0,1) khong truy xuat gan ay, nhng a b sa oi (1,0) c truy xuat gan ay, nhng khong b sa oi (1,1) c truy xuat gan ay, va b sa oi

Chon trang nan nhan la trang co o u tien cao nhat khi ket hp bit R va bit M
Trn Hnh Nhi 40

5/5/2009

Chien lc cap phat frame


n

So frame can cap phat cho moi tien trnh ?


n n

Giai s co m frame va n process Cap phat cong bang: #frame(Pi) = m/n


n

Cong bang ???

Cap phat theo ty le: #frame(pi) = (si / ( si ))* m


n

si = kch thc cua bo nh ao cho tien trnh pi

n n

Loi trang xay ra tiep theo, cap phat them frame cho tien trnh nh the nao ? Tuy thuoc chien lc thay the trang n Cuc bo : ch chon trang nan nhan trong tap cac trang cua tien trnh phat sinh loi trang -> so frame khong tang n Toan cuc: c chon bat ky trang nan nhan nao (du cua tien trnh khac) -> so frame co the tang, loi trang lan truyen

5/5/2009

Trn Hnh Nhi

41

Thay the trang toan cuc va...ket cuc bi tham !

Running

CPU P1 P1 P2 P1 P3

IO

P1, error P2, error P3, error

P1, swap out P2, swap out

Tat ca cac tien trnh ban ron thay the trang !


Trn Hnh Nhi 42

5/5/2009

Thrashing
Tat ca tien trnh au ban ron x ly loi trang ! IO hoat ong 100 %, CPU ranh ! He thong ngng tre
P1 P2 Real mem P3

Virtual Memory = Tha ho xai bo nh Thrashing = ao tng sup o ! Cac tien trnh trong he thong yeu cau bo nh nhieu hn kha nang cung cap cua he thong !
5/5/2009 Trn Hnh Nhi 43

Thrashing Diagram

Why does paging work? Locality model


n

Process migrates from one locality (working set) to another

Why does thrashing occur? size of working sets > total memory size
Trn Hnh Nhi 44

5/5/2009

Nguyen nhan Thrashing


1. Tien trnh khong tai s dung bo nh (qua kh != tng lai)

2. Tien trnh tai s dung bo nh, nhng vi kch thc ln hn 3. Qua nhieu tien trnh trong he thong

Ch co the kiem soat thrashing do nguyen nhan 3.

5/5/2009

Trn Hnh Nhi

45

Working set (1968, Denning)


Working set:
n

Working set = tap hp cac trang tien trnh ang truy xuat tai 1 thi iem
n

n n

Cac pages c truy xuat trong lan cuoi cung se nam trong working set cua tien trnh : working set parameter Kch thc cua WS thay oi theo thi gian tuy vao locality cua tien trnh

5/5/2009

Trn Hnh Nhi

46

Working-Set Model
n

working-set window so lan truy cap VD: 10,000 instruction 261577775162341234443434441323


n n

WSSi (working set of Process Pi) = tong so trang c truy cap trong lan gan ay nhat D = WSSi Tong cac frame can cho N tien trnh trong he thong if D > m Thrashing
n

=10 WS(t1) = {1,2,5,6,7}, WS(t2) = {3,4}

if D > m, chon mot/mot so tien trnh e nh ch tam thi.


Trn Hnh Nhi

5/5/2009

47

Giai quyet thrasing vi mo hnh Working set

S dung Working set Cache partitioning: Cap cho moi tien trnh so frame u cha WS cua no Page replacement: u tien swap out cac non-WS pages. Scheduling: ch thi hanh tien trnh khi u cho e nap WS cua no

5/5/2009

Trn Hnh Nhi

48