Lecture 16 (OS)

62
Operating Systems Lecture 16 Memory Management June 03, 2013 http://web.uettaxila.edu.pk/CMS/SP2013/seOSbs/

Transcript of Lecture 16 (OS)

Page 1: Lecture 16 (OS)

Operating Systems

Lecture 16

Memory Management

June 03, 2013http://web.uettaxila.edu.pk/CMS/SP2013/seOSbs/

Page 2: Lecture 16 (OS)

Agenda for Today

• Implementation of page table • Performance of paging and protection• Structure of page table • Shared pages • Segmentation • Segmentation architecture• Sharing of Segments • Paged Segmentation

Page 3: Lecture 16 (OS)

Address Translation

f

Page 4: Lecture 16 (OS)

Paging Example

Page size = 4 bytes

Process address space = 4 pages

Physical address space = 8 frames

Logical address: (1,3)

= 0111

Physical address: (6,3)

= 11011

0

1

2

3

Page Frame

11011

0111

Page 5: Lecture 16 (OS)

Addressing in Paging

Logical address space of 16 pages of 1024 words each, mapped into a physical memory of 32 frames.Logical address size?Physical address size?Number of bits for p, f, and d?

Page 6: Lecture 16 (OS)

Addressing in Paging

No. of bits for p = 4 bitsNo. of bits for f = 5 bitsNo. of bits for d = 11 bits

Page 7: Lecture 16 (OS)

Addressing in Paging

Logical address size = |p| + |d|= 4+11= 15 bits

Physical address size = |f| + |d|= 5+11= 16 bits

Page 8: Lecture 16 (OS)

Page Table Size

Page table size = NP * PTES where NP is the number of pages in the process address space and PTES is the page table entry size (equal to |f| based on our discussion so far). Page table size = 16 * 5 bits

= 16 bytes

Page 9: Lecture 16 (OS)

Another Example

Logical address = 32-bitProcess address space = 232 B

= 4 GBMain memory = RAM = 512 MBPage size = 4KMaximum pages in a process address space = 232 / 4K

= 1M

Page 10: Lecture 16 (OS)

Another Example

|d| = 12 bits|p| = 32 – 12 = 20 bitsNo. of frames = 512 M / 4 K

= 128 K|f| = 17 bits

Physical address = 17+12 bits

Page 11: Lecture 16 (OS)

Implementation of Page Table

In CPU registersOK for small process address spaces and large page sizesEffective memory access time (Teffective) is about the same as memory access time (Tmem)

Page 12: Lecture 16 (OS)

Implementation of Page Table

Keep page table in the main memory

Page table base register (PTBR)

Teffective = 2Tmem

Teffective is not acceptable

Page 13: Lecture 16 (OS)

Implementation of Page Table

Use a special, small, fast lookup hardware, called translation look-aside buffer (TLB) Typically 64–1024 entriesAn entry is (key, value)Parallel search for key; on a hit, value is returned

Page 14: Lecture 16 (OS)

Implementation of Page Table

(key,value) is (p,f) for pagingFor a logical address, (p,d), TLB is searched for p. If an entry with a key p is found, we have a hit and f is used to form the physical address. Else, page table in the main memory is searched.

Page 15: Lecture 16 (OS)

TLB

Logical address: (p, d)

p

f

Page 16: Lecture 16 (OS)

Implementation of Page Table

The TLB is loaded with the (p,f) pair so that future references to p are found in the TLB, resulting in improved hit ratio.On a context switch, the TLB is flushed and is loaded with values for the scheduled process.

Page 17: Lecture 16 (OS)

Paging Hardware

Page 18: Lecture 16 (OS)

Performance of Paging

Teffective on a hit = Tmem + TTLB

Teffective on a miss = 2Tmem + TTLB

If HR is hit ratio and MR is miss ratio, then

Teffective = HR (TTLB + Tmem) + MR (TTLB + 2Tmem)

Page 19: Lecture 16 (OS)

Example

Tmem = 100 nsecTTLB = 20 nsecHit ratio is 80%Teffective = ?

Teffective = 0.8 (20 + 100) + 0.2 (20 + 2x100)= 140 nanoseconds

Page 20: Lecture 16 (OS)

Example

Tmem = 100 nsecTTLB = 20 nsecHit ratio is 98%Teffective = ?

Teffective = 0.98 (20 + 100) + 0.02 (20 + 2x100)= 122 nanoseconds

Page 21: Lecture 16 (OS)

Structure of the Page Table

Hierarchical / Multilevel PagingInverted Page Table

Page 22: Lecture 16 (OS)

Multilevel Paging

Logical address = 32-bit

Page size = 4K bytes (212 bytes)

Page table entry = 4 bytes

Maximum pages in a process address space = 232 / 4K = 1M

Page 23: Lecture 16 (OS)

Multilevel Paging

Maximum pages in a process address space = 232 / 4K = 1M

Page table size = 4M bytes

This page cannot fit in one page ⇒ Page the page table

Page 24: Lecture 16 (OS)

Multilevel Paging

Page table needed for keeping track of pages of the page table—called the outer page table or page directoryNo. of pages in the page table is 4M / 4K = 1KSize of the outer page table is 1K * 4 bytes = 4K bytes ⇒ outer page will fit in one page

Page 25: Lecture 16 (OS)

Multilevel Paging

2-level paging

Page 26: Lecture 16 (OS)

Multilevel Paging

Addressing and address translation32-bit logical address: p1 used to index the outer page table and p2 to index the inner page table

page number page offset

p1 p2 d

10 10 12

Page 27: Lecture 16 (OS)

Multilevel Paging

p1{

p2{

Page 28: Lecture 16 (OS)

Multilevel Paging

Page 29: Lecture 16 (OS)

Inverted Page Table

One entry per real page in memory Page table size is limited by the number of frames (i.e., the physical memory) and not process address spaceEach entry in the page table contains (pid, p)

Page 30: Lecture 16 (OS)

Inverted Page Table

If a page ‘p’ for a process is loaded in frame ‘f’, its entry is stored at index ‘f’ in the page tableWe effectively index the page table with frame number; hence the name inverted page table

Page 31: Lecture 16 (OS)

Inverted Page Table

Frame Number

Page 32: Lecture 16 (OS)

Shared Pages

• Reentrant (read-only) code pages of a process address space can be shared

• Shared code appears in the same logical address space of all processes

• Multiple invocations of pico or gcc

Page 33: Lecture 16 (OS)

Shared Pages

Page 34: Lecture 16 (OS)

Segmentation

A memory management scheme that supports programmer’s view of memory. A segment is a logical unit such as: main program, procedure, function, method, object, global variables, stack, symbol tableA program is a collection of segments

Page 35: Lecture 16 (OS)

Segmentation

Page 36: Lecture 16 (OS)

Segmentation

1

3

2

4

logical memory physical memory

5

Page 37: Lecture 16 (OS)

Segmentation

1

3

2

4

1

4

2

3

logical memory physical memory

1

4

2

35

5

Page 38: Lecture 16 (OS)

Segmentation

1

3

2

4

1

4

2

3

logical memory physical memory

1

4

2

35

5

segment table

Page 39: Lecture 16 (OS)

Segmentation

Logical address consists of a two tuple:<segment-number, offset>

Segment table – maps two-dimensional logical addresses to physical addresses

Page 40: Lecture 16 (OS)

Segmentation

Each segment table entry has:base – contains the starting physical address where the segments reside in memory.limit – specifies the length of the segment.

Page 41: Lecture 16 (OS)

Segmentation

Segment-table base register (STBR) points to the segment table’s location in memory.Segment-table length register (STLR) indicates number of segments used by a programSegment number s is legal if s < STLR

Page 42: Lecture 16 (OS)

Segmentation

CPU

Page 43: Lecture 16 (OS)

Segmentation Architecture

RelocationDynamicBy segment table

SharingShared segmentsSame segment number

Page 44: Lecture 16 (OS)

Segmentation Architecture

Dynamic Storage AllocationFirst fitBest fitWorst fit

External fragmentation

Page 45: Lecture 16 (OS)

Example

Page 46: Lecture 16 (OS)

Address Translation

Logical and Physical Addresses(2, 399) – PA: 4300+399 = 4699(4, 0) – PA: 4700+0 = 4700(4, 1000) ⇒ trap (3, 1300) ⇒ trap (6, 297) ⇒ trap

Page 47: Lecture 16 (OS)

Sharing of Segments

Sharing at the segment level and not at the fixed-size page levelSharing at the code or data levelSegment table of multiple processes point to the same segment

Page 48: Lecture 16 (OS)

Sharing of Segments

Page 49: Lecture 16 (OS)

Issues with Segmentation

External FragmentationTotal memory space exists to satisfy a space allocation request for a segment, but memory space is not contiguous.

Page 50: Lecture 16 (OS)

Reduce external fragmentation by compactionShuffle segments to place free memory together in one block.Compaction is possible only if relocation is dynamic, and is done at execution time.

Issues with Segmentation

Page 51: Lecture 16 (OS)

I/O problemLatch job in memory while it is involved in I/O.Do I/O only into OS buffers

Very large segments ⇒ page program segments—paged segmentation

Issues with Segmentation

Page 52: Lecture 16 (OS)

Protection

Associate valid/invalid bit with each segment table entry to indicate if the referenced segment is part of the process address space or not Read, write, and execute bits to define legal operations on a segment

Page 53: Lecture 16 (OS)

Paged Segmentation

Divide every segment in a process into fixed size pagesNeed for a page table per segmentCPU’s memory management unit must support both segmentation and paging

Page 54: Lecture 16 (OS)

Paged Segmentation

1

3

2

4

logical memory

5

physical memory

Page 55: Lecture 16 (OS)

Paged Segmentation

1

3

2

4

logical memory

5

physical memory

1

2

0

3

0123...

10

126 127

31

12610

page table

Page 56: Lecture 16 (OS)

Paged Segmentation

Logical address is still <s,d>, with s used to index the segment tableEach segment table entry consist of the tuple

<segment-length, page-table-base>The logical address is legal if

d < segment-length

Page 57: Lecture 16 (OS)

Paged Segmentation

Segment offset, d, is partitioned into two parts: p and d’, where p is used to index the page table associated with segment, s, and d’ is used as offset within a page

Page 58: Lecture 16 (OS)

Paged Segmentation

p indexes the page table to retrieve frame, f, and physical address (f,d’) is formed

s d

p d’

index segment

tableindex page

tableoffset within the

page p

Page 59: Lecture 16 (OS)

Paged Segmentation

Page 60: Lecture 16 (OS)

MULTICS Example

GE 345 processorLogical address = 34 bits Page size = 1 KBs is 18 bits and d is 16 bitsSize of p and d’, largest segment size, and max. number of segments per process?

Page 61: Lecture 16 (OS)

MULTICS Example

Largest segment = 2d bytes= 216 bytes

Maximum number of pages per segment = 216 / 1 K = 64|p| = log2 64 bits = 6 bits|d’| = log2 1 K = 10 bitsMaximum number of segments per process = 2s = 218

Page 62: Lecture 16 (OS)

MULTICS Example

s d

p d’18 bits

6 bits 10 bits