TDDB68/TDDE47 Concurrent Programming and Operating Systems - - PowerPoint PPT Presentation

tddb68 tdde47 concurrent programming and operating
SMART_READER_LITE
LIVE PREVIEW

TDDB68/TDDE47 Concurrent Programming and Operating Systems - - PowerPoint PPT Presentation

TDDB68/TDDE47 Concurrent Programming and Operating Systems Lecture: Memory management I Mikael Asplund Real-time Systems Laboratory Department of Computer and Information Science Thanks to Simin Nadjm-Tehrani and Christoph Kessler for much


slide-1
SLIDE 1

1

TDDB68/TDDE47 Concurrent Programming and Operating Systems Lecture: Memory management I

Mikael Asplund Real-time Systems Laboratory Department of Computer and Information Science

Thanks to Simin Nadjm-Tehrani and Christoph Kessler for much of the material behind these slides.

slide-2
SLIDE 2

2

Reading guidelines

  • Sliberschatz et al.

– 9th edition: Ch. 8, 9.1-9.3 – 10th edition: Ch. 9, 10.1-10.3

slide-3
SLIDE 3

3

Why do we need memory management?

slide-4
SLIDE 4

4

CPU

Simple microprocessor

CU

...

CU = Control Unit

Memory

slide-5
SLIDE 5

5

Problems

  • Process separation
  • Dynamic processes
  • Memory allocation
slide-6
SLIDE 6

6

CPU

High-end CPU

CU

...

MMU

CU = Control Unit, MMU = Memory Management Unit

Memory

slide-7
SLIDE 7

7

CPU

Logical address != physical address

CU

...

MMU

CU = Control Unit, MMU = Memory Management Unit

Memory

slide-8
SLIDE 8

8

CPU

Can put data elsewhere

CU

...

MMU

CU = Control Unit, MMU = Memory Management Unit

Memory

slide-9
SLIDE 9

9

CPU

Does not even have to be real memory

CU

...

MMU

CU = Control Unit, MMU = Memory Management Unit

slide-10
SLIDE 10

10

Due to the separation between logical address and physical address the MMU can provide the process with virtual memory

slide-11
SLIDE 11

Memory-Management Unit (MMU)

  • Hardware device that maps virtual to physical

address

  • The user program deals with logical addresses;

it never sees the real physical addresses

  • The MMU provides translation between logical

and physical addresses

slide-12
SLIDE 12

Simple MMU idea

Dynamic relocation using a relocation register

slide-13
SLIDE 13

13

Granularity

  • The ”chunks” of memory that can be given

different locations by the MMU can be

– Process level – Segmentation – Paging

slide-14
SLIDE 14

Process-level Memory Management

slide-15
SLIDE 15

Memory

Memsize

slide-16
SLIDE 16

Memory

Kernel Memsize

slide-17
SLIDE 17

Memory

Kernel Memsize Process 1

slide-18
SLIDE 18

Memory

Kernel Memsize Process 1 Process 2

slide-19
SLIDE 19

Memory

Kernel Memsize Process 1 Process 2 Process 3

slide-20
SLIDE 20

Memory

Kernel Memsize Process 1 Process 2 Process 3

Multi-partition allocation

  • Each process gets one

partition

  • Protects processes from

each other

slide-21
SLIDE 21

Ensuring protection

slide-22
SLIDE 22

Providing translation

slide-23
SLIDE 23

Memory

Kernel Memsize Process 1 Process 2 Process 3

slide-24
SLIDE 24

Memory

Kernel Memsize Process 1 Process 2 Process 3

slide-25
SLIDE 25

Memory

Kernel Memsize Process 1 Process 2 Process 3

External fragmentation!

slide-26
SLIDE 26

Memory

Kernel Memsize Process 1 Process 2 Process 3

slide-27
SLIDE 27

Memory

Kernel Memsize Process 1 Process 2 Process 3

Unused memory

Internal fragmentation!

slide-28
SLIDE 28

When allocating memory to a process, how big should the partition be?

slide-29
SLIDE 29

Allocation schemes

Fixed partition size

  • One size fits all
  • Simple
  • Internal fragmentation

Variable partition size

  • Size of program

decides partition size

  • More scalable in

number of processes

  • External fragmentation
slide-30
SLIDE 30

Dynamic storage-allocation problem:

How to satisfy a request of size n from a list of free holes?

slide-31
SLIDE 31

Allocation schemes

  • First-fit: Allocate the first hole that is big enough
  • Best-fit: Allocate the smallest hole that is big

enough;

– must search entire list, unless ordered by size. – Produces the smallest leftover hole.

  • Worst-fit: Allocate the largest hole;

– must also search entire list. – Produces the largest leftover hole.

slide-32
SLIDE 32

Compaction

  • Reduce external fragmentation
  • Compaction is possible only if relocation is

dynamic, and is done at execution time

  • I/O problem
slide-33
SLIDE 33

Example of Compacting

p1 p3 p4 p2 pnew

slide-34
SLIDE 34

Example of Compacting: Solution 1

p1 p3 p4 p2 p1 p3 p4 p2 pnew Move all occupied areas to one side until there is a hole large enough for pnew

slide-35
SLIDE 35

Example of Compacting: Solution 2

p1 p3 p4 p2 p1 p3 p4 p2 pnew Search and select one (or a few) processes to move to free a hole large enough…

slide-36
SLIDE 36

Still not enough space for process?

slide-37
SLIDE 37

Still not enough space for process? Try swapping!

slide-38
SLIDE 38

Swapping

slide-39
SLIDE 39

Swapping

Costly!

slide-40
SLIDE 40

Segmentation

slide-41
SLIDE 41

Segmentation

  • Memory-management scheme

that supports a user view of memory:

  • A program is a collection of segments.
  • A segment is a logical unit such as:

– main program, procedure,

function, method,

– object, local variables, global variables, – common block, stack, – symbol table, arrays

  • Idea: allocate memory according to such segments
slide-42
SLIDE 42

Logical View of Segmentation

1: stack

3: main

2: subroutine 4: symbol table

1 4 2 3

user space physical memory space

slide-43
SLIDE 43

Segmentation Architecture

  • Logical address is a pair <segment-number, offset>
  • Segment table – maps two-dimensional physical addresses; each

table entry has:

– base – physical starting address where the segment resides in memory – limit – specifies the length of the segment

  • 2 registers (part of PCB):

– 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 program; Segment number s is legal if s < STLR

slide-44
SLIDE 44

Segmentation Architecture: Address Translation

slide-45
SLIDE 45

Pros and cons of segmentation

  • More fine grained than process-level memory

management

  • Minimal internal fragmentation
  • External fragmentation
  • Allocation potentially difficult
slide-46
SLIDE 46

Paging

slide-47
SLIDE 47

Physical memory

slide-48
SLIDE 48

Physical memory

1 2 3 4 5 6 7 8 Frame number

slide-49
SLIDE 49

Physical memory

1 2 3 4 5 6 7 8 Frame number

Logical memory

slide-50
SLIDE 50

Logical memory Physical memory

1 2 3 4 5 6 7 8 Frame number page 0 page 1 page 2 page 3

slide-51
SLIDE 51

Logical memory Physical memory

1 2 3 4 5 6 7 8 Frame number page 0 page 1 page 2 page 3 page 0 page 1 page 2 page 3

slide-52
SLIDE 52

Logical memory Physical memory

1 2 3 4 5 6 7 8 Frame number page 0 page 1 page 2 page 3 page 0 page 1 page 2 page 3 Page nr Frame nr 2 1 5 2 3 3 7

Page table

slide-53
SLIDE 53

Paging

 Physical address space of a process can be noncontiguous  Process is allocated physical memory whenever the latter is available – no external fragmentation  Internal fragmentation

slide-54
SLIDE 54

Address Translation Scheme

  • Address generated by CPU is divided into:

– Page number (p) –index into a page table which

contains the base address of each page in physical memory

– Page offset (d) – combined with base address to

define the physical memory address that is sent to the memory unit

slide-55
SLIDE 55

Paging: Address Translation Scheme

slide-56
SLIDE 56

Paging Example

1 character = 1 byte Frame/page size = 4 bytes = 22 bytes Number of pages = 4 = 22 Logical addr. space size = 16 = 22+2 Physical address space size = 32 bytes

slide-57
SLIDE 57

Menti question (43 75 8)

Assume:

– 32bit architecture, single level paging – 4GB of main memory (2^32 Bytes) – Page size of 4KB (2^12 Bytes) – 200 running processes

What is the required size for all the page tables?

A) 200MB B) 400MB C) 800MB D) 1600MB

slide-58
SLIDE 58

Page Table Structure

  • The “large page table problem”
  • Page table structures:

– Hierarchical Paging: “page the page table” – Hashed Page Tables – Inverted Page Tables

slide-59
SLIDE 59

Hierarchical Page Tables

slide-60
SLIDE 60

Address-Translation Scheme

slide-61
SLIDE 61

Can we address a 64-bit memory space?

slide-62
SLIDE 62

Hashed Page Table

slide-63
SLIDE 63

Inverted Page Table Architecture

slide-64
SLIDE 64

Implementation of the Page Table

  • Page table is kept in main memory
  • Page-table base register (PTBR)

points to the page table

  • Page-table length register (PRLR)

indicates size of the page table

  • Every data/instruction access requires n+1 memory accesses (for n-level

paging).

– One for the page table and one for the data/instruction.

  • Solve the (n+1)-memory-access problem

– by using a special fast-lookup cache (in hardware):

translation look-aside buffer (TLB)

  • Implements an associative memory
slide-65
SLIDE 65

Paging Hardware With TLB

TLB: fast, small, and expensive,

  • Typ. 64…1024 TLB entries

in main memory

slide-66
SLIDE 66

Effective Access Time

  • Memory cycle time: t
  • Time for associative lookup: 
  • TLB hit ratio 

– percentage of times that a page number is found in TLB

slide-67
SLIDE 67

Effective Access Time

  • Memory cycle time: t
  • Time for associative lookup: 
  • TLB hit ratio 

– percentage of times that a page number is found in TLB

  • Effective Access Time (EAT):

EAT = (t + )  + (2t + )(1 – )

slide-68
SLIDE 68

Effective Access Time

  • Memory cycle time: t
  • Time for associative lookup: 
  • TLB hit ratio 

– percentage of times that a page number is found in TLB

  • Effective Access Time (EAT):

EAT = (t + )  + (2t + )(1 – ) = 2t +  –  t

slide-69
SLIDE 69

Effective Access Time

  • Memory cycle time: t
  • Time for associative lookup: 
  • TLB hit ratio 

– percentage of times that a page number is found in TLB

  • Effective Access Time (EAT):

EAT = (t + )  + (2t + )(1 – ) = 2t +  –  t Example: For t =100 ns,  = 20 ns,  = 0.8: EAT = 140 ns

slide-70
SLIDE 70

Memory Protection

  • Implemented by associating protection bit with each

frame

  • Valid-invalid bit attached to each entry in the page table:

– “valid”: the associated page is in the process’ logical address

space, and is thus a legal page

– “invalid”: the page is not in the process’ logical address

space

  • Allows dynamically sized page tables
slide-71
SLIDE 71

Logical memory Physical memory

1 2 3 4 5 6 7 8 Frame number page 0 page 1 page 2 page 3 page 0 page 1 page 2 page 3

Memory Protection

Page table

2 v 1 5 v 2 3 v 3 7 v 4 X i 5 X i 6 X i valid/invalid page frame

slide-72
SLIDE 72

Shared memory – Easy with paged memory!

slide-73
SLIDE 73

Combining Segmentation and Paging

  • Each segment is organized as a set of pages.
  • Segment table entries refer to a page table for

each segment.

  • TLB used to speed up effective access time.
slide-74
SLIDE 74

Combining Segmentation and Paging

s d p

segment number page number displacement

virtual address:

segment table

  • rigin

register

s

s’ p f d f physical address

segment table for this process page table for this segment if TLB hit for (s,p), get f,

  • therwise:
slide-75
SLIDE 75

Demand Paging

slide-76
SLIDE 76

Virtual Memory That is Larger Than Physical Memory

slide-77
SLIDE 77

Demand Paging

  • Bring a page into memory only when it is needed

– Less I/O needed – Less memory needed – Faster response – More users

  • Page is needed if referenced (load/store, data/instructions)

– invalid reference  abort – not-in-memory  bring to memory

[Kilburn et al. 1961]

slide-78
SLIDE 78

Rather than swapping entire processes (cf. swapping), we page their pages from/to disk only when first referenced.

slide-79
SLIDE 79

Steps in Handling a Page Fault

(Case: a free frame exists)

Free frame Free frame Occupied

Page table Main memory

1 X i page 1 Occupied Occupied Occupied Occupied 1.

  • 1. Memory reference
  • 2. Page fault! →Interrupt
  • 3. OS moves page into memory
  • 4. Update page table
  • 5. Restart memory access instruction

2. page 1 3. 4. … … Load from memory … ...

Program Disk

5. 1 3 v

slide-80
SLIDE 80

What happens if there is no free frame?

slide-81
SLIDE 81

Page replacement

  • Find some page in memory, but not really in use,

swap it out

– Write-back only necessary if victim page was modified – Same page may be brought into memory several times

  • More details next lecture...
slide-82
SLIDE 82

Menti question (43 75 8)

Which of the following memory management tasks can be performed by the MMU: A) Memory protection B) Page table lookup C) Page replacement D) TLB lookup

slide-83
SLIDE 83

Performance of Demand Paging

slide-84
SLIDE 84

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
slide-85
SLIDE 85

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)
slide-86
SLIDE 86

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t +

slide-87
SLIDE 87

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p (

slide-88
SLIDE 88

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p ( page fault overhead

slide-89
SLIDE 89

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p ( page fault overhead + w ( time to swap page out )

slide-90
SLIDE 90

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p ( page fault overhead + w ( time to swap page out ) + time to swap new page in

slide-91
SLIDE 91

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p ( page fault overhead + w ( time to swap page out ) + time to swap new page in + restart overhead

slide-92
SLIDE 92

Performance of Demand Paging

  • Page Fault Rate p 0  p  1.0

– if p = 0: no page faults – if p = 1, every reference is a fault

  • Write-back rate w 0 <= w <= 1
  • Memory access time t
  • Effective Access Time (EAT)

EAT = (1 – p) t + p ( page fault overhead + w ( time to swap page out ) + time to swap new page in + restart overhead + t )