29
1 C eng 334 -O perating System s 3.2-1 Chapter 3.2 : V irtual Memory • W hatisvirtualm em ory? V irtualm em ory m anagem entschem es •Paging •Segm entation •Segm entation w ith paging Page table m anagem ent Chapter 3.2: Virtual Memory

Op Sy 03 Ch 32

  • Upload
    google

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Op Sy 03 Ch 32

1

Ceng 334 - Operating Systems 3.2-1

Chapter 3.2 : Virtual Memory

• What is virtual memory?

• Virtual memory management schemes• Paging

• Segmentation

• Segmentation with paging

• Page table management

Chapter 3.2: Virtual Memory

Page 2: Op Sy 03 Ch 32

2

Ceng 334 - Operating Systems 3.2-2

Problems with Memory Management Techniques so far

1. Unused (wasted) memory due to fragmentation

2. Memory may contain parts of program which are not used during a run (ie., some routines may not be accessed in that particular run)

3. Process size is limited with the size of physical memory

Problems With Memory Mang.

Page 3: Op Sy 03 Ch 32

3

Ceng 334 - Operating Systems 3.2-3

Virtual Memory (VM)

Virtual memory of process on disk

Real memory of system

Map (translate)

virtual address to real

Virtual Memory

Page 4: Op Sy 03 Ch 32

4

Ceng 334 - Operating Systems 3.2-4

Virtual Memory (VM)

• VM is conceptual

• It is constructed on disk

• Size of VM is not limited (usually larger than real memory)

• All process addresses refer to the VM image

• When the process executes all VM addresses are mapped on to real memory

Virtual Memory

Page 5: Op Sy 03 Ch 32

5

Ceng 334 - Operating Systems 3.2-5

Did We Solve the “Problems”?

1. Unused (wasted) memory due to fragmentation (We’ll see!)

2. Memory may contain parts of program which are not used during a run

– YES! Virtual memory contents are loaded into memory on demand

3. Process size is limited with the size of physical memory

– YES! Process size can be larger than real memory

Problems Solved?

Page 6: Op Sy 03 Ch 32

6

Ceng 334 - Operating Systems 3.2-6

(Pure) Paging

• Virtual and real memory are divided into fixed sized pages

• Programs are divided into pages

• A process address (both in virtual & real memory) has two components

Page # Offset within page

Pagging

Page 7: Op Sy 03 Ch 32

7

Ceng 334 - Operating Systems 3.2-8

Paging (Cont.)

• When process pages are transferred from VM to real memory, page numbers must be mapped from virtual to real memory addresses

• This mapping is done by software & hardware

• When the process is started only the first page (main) is loaded. Other pages are loaded on demand

Pagging

Page 8: Op Sy 03 Ch 32

8

Ceng 334 - Operating Systems 3.2-7

Interpretation of an Address

• 16 bits for addressing means that the memory addressed is 64K bytes (0000 -FFFF)

• Suppose page size is 4K bytes (12 bits)

• The memory has 16 pages (4 bits)

• Example : address : 7 F B C

111 1111 1011 1100

Page # Offset

Intyerpretation Of an Address

Page 9: Op Sy 03 Ch 32

9

Ceng 334 - Operating Systems 3.2-9

Page Tables

• Index of page table is the virtual page #

Page Table

0 Page frame #

Page protection

Reference bit

Modification bit

Validity bit

Page Table Entry

1

8

234567

Page Tables

Page 10: Op Sy 03 Ch 32

10

Ceng 334 - Operating Systems 3.2-10

Page Table Entry Fields

• Validity bit is set when the page is in memory

• Reference bit is set set by the hardware whenever the page is referred

• Modified bit is set whenever the page is modified

• Page-protection bits set the access rights (eg., read, write restrictions)

Page Table Entry Fields

Page 11: Op Sy 03 Ch 32

11

Ceng 334 - Operating Systems 3.2-11

Address Mapping in Paging

Real memory address

Page tables of processes

PTn

PT 1

PT2. .

Page # Offset within pagePage Table Register

+

Catenate

Virtual memory address

Adrress Mapping

Page 12: Op Sy 03 Ch 32

12

Ceng 334 - Operating Systems 3.2-12

Address Mapping in Paging (Cont.)

• During the execution every page reference is checked against the page map table entry

• If the validity bit is set (ie., page is in memory) execution continues

• If the page is not in memory a page fault (interrupt - trap) occurs and the page is fetched into memory

• If the memory is full, pages are written back using a page replacement algorithm

Address Mapping

Page 13: Op Sy 03 Ch 32

13

Ceng 334 - Operating Systems 3.2-13

Memory Management Problems : Re-visit due to paging

1. Unused (wasted) memory due to fragmentation– ONLY on the last page (Page Break)

2. Memory may contain parts of program which are not used during a run – Virtual memory contents are loaded into memory on demand

3. Process size is limited with the size of physical memory– Process size can be larger than real memory

• Furthermore, program does not occupy contiguous locations in memory (virtual pages are scattered in real memory)

Re-visst Due to Pagging

Page 14: Op Sy 03 Ch 32

14

Ceng 334 - Operating Systems 3.2-14

Segmentation

• Pages are fixed in size, segments are variable sized

• A segment can be a logical entity such as– Main program

– Some routines

– Data of program

– File

– Stack

Segmentation

Page 15: Op Sy 03 Ch 32

15

Ceng 334 - Operating Systems 3.2-15

Segmentation (Cont.)• Process addresses are now in the form

Segment # Offset within segment

• Segment Map Table has one entry for each segment and each entry consist of

– Segment number

– Physical segment starting address

– Segment length

Segmentation

Page 16: Op Sy 03 Ch 32

16

Ceng 334 - Operating Systems 3.2-16

Problems with Segmentation

• Similar problems in dynamic partitioning

– Fragmentation in real memory

– Relocation is necessary for compaction

Problems with Segmentation

Page 17: Op Sy 03 Ch 32

17

Ceng 334 - Operating Systems 3.2-17

Segmentation with Paging

• Segmentation in virtual memory, paging in real memory

• A segment is composed of pages

• An address has three components

Page # Offset within pageSegment #

• The real memory contains only the demanded pages of a segment, not the full segment

Segmentation with Paging

Page 18: Op Sy 03 Ch 32

18

Ceng 334 - Operating Systems 3.2-18

Addressing in Segmentation with Paging

Segment Table Page Tables Pages

Page # Offset within pageSegment #

Addressing in Segmentation

Page 19: Op Sy 03 Ch 32

19

Ceng 334 - Operating Systems 3.2-19

How Big is a Page Table?

• Consider a full 2 32 byte (4GB) address space

– Assume 4096 byte (2 12 byte) pages

– 4 bytes per page table entry

– The page table has 2 32/2 12 (= 2 20 ) entries (one for each page)

– Page table size would be 2 22 bytes (or 4 megabytes)

Page Table

Page 20: Op Sy 03 Ch 32

20

Ceng 334 - Operating Systems 3.2-20

Problems with Direct Mapping?

• Although a page table is of variable length depending on the size of process, we can not keep them in registers

• Page table must be in memory for fast access

• Since a page table can be very large (4MB), page tables are stored in virtual memory and be subjected to paging like process pages

Direct Mapping

Page 21: Op Sy 03 Ch 32

21

Ceng 334 - Operating Systems 3.2-21

How to Solve?

• Two-level Lookup

• Inverted Page Tables

• Translation Lookaside Buffers

How to Solve?

Page 22: Op Sy 03 Ch 32

22

Ceng 334 - Operating Systems 3.2-22

Two-Level Lookup

Directory Page Offset

12 bits - 4096 Byte pages

10 bits - 1024 pages

10 bits - 1024 directories

Virtual Address - 32 bits (4 GB)

Two-Level Lookup

Page 23: Op Sy 03 Ch 32

23

Ceng 334 - Operating Systems 3.2-23

Two-Level Lookup (Cont.)

Dir Page Offset

+

+

Physical Address

Page Directory

Page Table

Two-Level Lookup

Page 24: Op Sy 03 Ch 32

24

Ceng 334 - Operating Systems 3.2-24

Two-Level Lookup (Cont.)

• A process is represented by one or more entries of the page directory (ie., several page tables)

• Typically a page table size is set as one page which makes swapping easier

• This is one of the addressing schemes used by Intel family of chips (Intel chips can use paging, segmentation or a combination of both)

Two-Level Lookup

Page 25: Op Sy 03 Ch 32

25

Ceng 334 - Operating Systems 3.2-25

Inverted Page Tables

Page # Offset within page

Virtual Address

hash

Hash TablePage Frame Table

Inverted Page Tables

Page 26: Op Sy 03 Ch 32

26

Inverted Page Tables

Ceng 334 - Operating Systems 3.2-26

Inverted Page Tables (Cont.)

• The virtual page number is hashed to point to a hash table

• The hash table contains a pointer to the inverted page table

• Inverted page table contains page table entries (one for each real memory page)

• Entries having the same hash codes are chained

Page 27: Op Sy 03 Ch 32

27

Inverted Page Tables

Ceng 334 - Operating Systems 3.2-27

Inverted Page Tables (Cont.)

• A fixed portion of memory is used for mapping regardless of the number of processes or virtual pages

• This approach is used by IBM’s AS/400 and RISC System/6000 computers

Page 28: Op Sy 03 Ch 32

28

Translation Lookaside Buffer

Ceng 334 - Operating Systems 3.2-28

Translation Lookaside Buffer

Frame # Offset

TLB Hit

TLB Miss

TLB

Page Table

Page # Offset

Virtual Address

Real AddressPage Fault

Page 29: Op Sy 03 Ch 32

29

Translation Lookaside Buffer

Ceng 334 - Operating Systems 3.2-29

Translation Lookaside Buffer (Cont.)

• Translation lookaside buffer (TLB) is a cache for page table entries

• TLB contains page table entries that have been most recently used

• Whenever the page table is referred (TLB miss), the page table entry is also copied to the TLB

• TLB is usually an associative memory (content addressable memory)