Operating Systems - Memory Management - Paging (Ahmad Yoosofan)

os/img/memory/paging_model.png
os/img/memory/paging_example_32_bytes_memory.png
1
os/img/memory/memory_paging_process.png
2

Paging Hardware

  1. CPU generates Logical Address (virtual address)

  2. Logical Address is divided into: Page Number (p) + Offset (d)
    • Page Number (p): Used as an index into a page table. The page table contains the base address of each page in physical memory.

    • Page Offset (d): Combined with the base address to define the exact physical memory address.

  3. Page Table maps logical pagephysical frame

  4. Final Physical Address = Frame Number + Offset

  5. If the logical address space is 2 m and page size is 2 n bytes:

  6. Page offset (d) = n bits

  7. Page number (p) = m - n bits

  8. Number of bits of Address related to Maximum supported memory by this computer(cpu and motherboard)

  9. Number of bits of Address = log2(Maximum supported memory)

  10. If max supported memory = 32 words then number of bits needed for address?

  11. 32 = 2 ^ 5, n = l o g 2 ( m ) , m is number of bytes or words

  12. 5 = log2(32)

  13. if p = 2, d = 3 then the size of each frame or page is?

  14. 2 ^ 3 = 8

  15. Maximum number of Frames?

  16. Maximum number of precesses

os/img/memory/page_number_offset.png os/img/memory/paging_hardware.png os/img/memory/memory_paging_logical_address_to_physical_address_detail.png
3

Page-Table Base Register (PTBR)

PagingHW CPU CPU Logical Logical Address Page (p) Offset (d) CPU->Logical Generates PageTable Page Table ... Frame (f) ... Logical:p->PageTable:entry Index Physical Physical Address Frame (f) Offset (d) Logical:d->Physical:d Exact Copy PageTable:entry->Physical:f Base Address Memory Physical Memory Physical->Memory Accesses
os/img/memory/memory_paging_address_translation.png
4

Step by Step Sample

Memory with only OS before adding a process

OS

OS

OS

 

 

 

 

 

 

 

 

 

 

 

 

 

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Process P0

A

B

C

D

E

F

P0_0

P0_0

P0_0

P0_0

P0_1

P0_1

0

1

2

3

4

5

0000

0001

0010

0011

0100

0101

Adding Process P0 to Memory

OS

OS

OS

 

E

F

 

 

T_P0

T_P0

T_P0

T_P0

A

B

C

D

OS

OS

OS

 

P0_1

P0_1

 

 

11

01

 

 

P0_0

P0_0

P0_0

P0_0

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Page Table of P0

P0_0

P0_1

 

 

11

01

00

00

00

01

10

11

Page Table of P0

11

01

00

00

00

01

10

11

  1. Logical Address of F = "0101" = "01" "01"

  2. Page Address = "01"

  3. Page Table Cell Index "01" is "01"

  4. Physical Address= concatenation("01", "01")

  5. Physical Address= "01" "01" = "0101"

5

Parts of Address Register in Paging

  1. Frame Size and Limitations

  2. Page size is defined by the hardware architecture (always a power of 2).

  3. Typical sizes range from 4 KB to 8 KB (modern systems also support "Huge Pages" up to 1 GB).

  4. The Trade-off: Internal Fragmentation * Process size rarely perfectly aligns with page boundaries. * The last page allocated may not be completely full.

  5. Page size = 4,096 bytes (4 KB). Process size = 72,766 bytes.

  6. Requires: 17 full pages + 3,134 bytes in the 18th page.

  7. Waste: 4,096 - 3,134 = 962 bytes of internal fragmentation.

6

Translation Look-aside Buffer (TLB)

os/img/memory/paging_hardware_with_TLB.png os/img/memory/paging_hardware_with_TLB_ptbr.png
7

Flowchart of TLB miss

os/img/memory/paging_hardware_TLB_miss.png os/img/memory/memory_paging_page_table_and_cache.jpg
8

Effective Access Time (EAT)

  1. EAT = table + memory

  2. t a b l e = h t * t t + ( 1 h t ) * ( t t + t m )

  3. m e m o r y = h c * t c + ( 1 h c ) * ( t c + t m )

زمان دسترسی مؤثر = زمان دسترسی به جدول صفحه + زمان دسترسی به حافظه

زمان دسترسی مؤثر را برای پردازنده‌ای با حافظهٔ صفحه‌بندی شده حساب کنید اگر زمان دسترسی به حافظهٔ نهان جدول صفحه برابر ۱ نانو ثانیه باشد و زمان دسترسی به حافظهٔ نهان ۵ نانوثانیه باشد و زمان دسترسی به حافظه برابر ۱۰۰ نانوثانیه باشد و ضریب اصابت حافظهٔ نهان جدول صفحه برابر با ۹۵ درصد و ضریب اصابت به حافظهٔ نهان ۹۰ درصد باشد.

با فرض برابر بودن نسبت‌های اصابت و زمان‌های یکسان برای دسترسی به حافظهٔ نهان و حافظهٔ TLB خواهیم داشت

9

Memory Protection

  1. PTLR (Page-Table Length Register): Indicates the size of the page table to prevent out-of-bounds access.

  2. Valid/Invalid bit.
    • Valid = page is in memory and accessible.

    • Invalid = page not in memory or access violation → Trap to OS.

Page

Frame

Status

0

4

v

1

7

v

2

i

3

i

os/img/memory/memory_paging_typical_page_table_entry.jpg
os/img/memory/paging_valid_invalid.png
10

Page Sharing

os/img/memory/memory_paging_share_pages.png os/img/memory/paging_sharing_code.png
11

Frame Size & Single-Level Paging Limitations

12

Advantages & Disadvantages of Paging

13

References

14