Docsity

Operating System Principles: Memory Management and Segmentation vs. Paging - Prof. Dorian , Study notes of Operating Systems

Toggle sidebar
bg1
1
THE UNIVERSITY of
NEW MEXICO
Main Memory
Prof. Dorian Arnold
CS 481: Operating System Principles
Spring ‘09
2
Background
• CPU can only access registers or main memory
– All instructions or data must be in register or main memory
• Instruction execution cycle
– CPU fetch instruction from main-memory (via PC register)
– CPU decodes instruction
– CPU executes instruction
• load data from memory to registe r
• store data from register to memo ry
• modify register value (e.g. increm ent)
• …
• Register access fast (typically one cycle)
• Memory access slow (many cycles, CPU stalls)
– Cache: fast memory between CPU and main memory
3
Memory Management Considerations
• Performance
– memory devices have different latencies
• Protection
– OS must be protected
– Processes must be protected from each other
• Correctness
– Processes must have consistent view of data
4
Address Space Protection
• A process has own, disjoint address space
• Hardware validates every memory access
– Base register: smallest legal physical address
– Limit register: size of process’ address space
Should user have write-access to base and limit registers?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Operating System Principles: Memory Management and Segmentation vs. Paging - Prof. Dorian and more Study notes Operating Systems in PDF only on Docsity!

THE UNIVERSITY

of

NEW MEXICO

Main Memory

Prof. Dorian Arnold

CS 481: Operating System Principles

Spring ‘

2

Background

•^

CPU can only access registers or main memory^ – All instructions or data must be in register or main memory

-^

Instruction execution cycle^ – CPU fetch instruction from main-memory (via PC register)– CPU decodes instruction– CPU executes instruction

-^

load data from memory to register

-^

store data from register to memory

-^

modify register value (e.g. increment)

-^

…

•^

Register access fast (typically one cycle)

-^

Memory access slow (many cycles, CPU stalls)^ – Cache: fast memory between CPU and main memory

3

Memory Management Considerations

• Performance

  • memory devices have different latencies

• Protection

  • OS must be protected– Processes must be protected from each other

• Correctness

  • Processes must have consistent view of data

4

Address Space Protection

• A process has own, disjoint address space• Hardware validates

every

memory access

  • Base register: smallest legal physical address– Limit register: size of process’ address space Should user have write-access to base and limit registers?

5

Process Address Space

temporary (per function call) datadynamically allocated memorystatic globals (initialized, unitialized)^ program code

6

Why Dynamic Memory

-^

Don’t know needs at compile time^ – If using static allocation, must be pessimistic

(overestimate before underestimate)

-^

Recursive procedures^ – How many times will procedures be called?

-^

Processes have 2 types of dynamic allocations^ – Stack– Heap

7

Process Stack

• Stack frame

implicitly allocated per

function call

• Used for procedure’s local data

  • Arguments– Local variables– Return address– …

8

Process Stack (cont’d)

main()

int a =

foo(

a );

} foo(int b)

int c =

foo(

c = 1 b = 2 c = 1 b = 0 a = 0 return: foo+…

st^1 main’s stack foo’s stack nd 2 foo’s stack

return: main+…

13

Allocation Policies

• Best fit

  • search all entries for closest match

• First fit

  • search free list for first match

• Next fit

  • search free list for first match– start search from “last” searched position

• Worst fit

  • find largest available chunk

14

Freeing Memory

•^

Explicit de-allocation (

free()



Recycling storage that is still in use

-^

e.g. two pointers to same chunk, free 1

st, use 2

nd



Memory leak

-^

lose pointer to non-freed chunk

•^

Implicit de-allocation^ 

Needs language/system support

  • Reference counting -^

free when last reference goes away

  • Garbage collection -^

maintain all objects and pointers to objects

-^

recycle all objects not pointed to by any pointers 

Expensive!

15

Multiprogramming Goals

-^

Sharing^ –

Multiple processes simultaneously in main memory– Cooperating processes can share (portions of) address space

-^

Transparency^ –

Processes should be unaware of memory sharing– Works regardless of number or location of processes

-^

Protection^ –

OS should be protected from processes– Processes should be protected from each other

-^

Efficiency^ –

High CPU/memory utilization

16

Memory Management Overview • Program relocation

  • Programs must be in memory to be executed

• Segmentation

  • Partition address space into distinct areas

• Paging

  • Fine grain address space partitioning (pages

comprise segments)

17

Relocation

• Cannot predict where process will be

loaded into main memory

• Adjust program to run in different memory

areas

• Static vs. dynamic relocation

18

Memory Allocation: Contiguous

P

1 P 0 Virtual Address Space

Physical Memory

19

Naming: The Relocation Problem^ •

Source code uses symbolic address naming^ – E.g. int * a; char * foo;

-^

Compiler binds symbolic addresses torelocatable addresses:^ – E.g. “14 bytes from start of this module”

-^

Loader binds relocatable addresses to absoluteaddresses^ – E.g. 0x8888^ When/how do we bind to absolute addresses?

20

Static Relocation

•^

Compiler binds to absolute address^ – Must know program memory location at compile time

-^

Loader binds to absolute address^ – Compiler generates re-locatable code– Requires reloading if program’s main memory location to change 

No hardware/OS support needed 

Program location cannot change^ •

Swap in must restore original location



Requires contiguous memory allocation 

Must over-allocate stack/heap 

Fragmentation (how?)

25

Dynamic Relocation Implementation

Context switching: 1.

Change to privileged mode

Save limit register of old process

Load limit and set relocation registers of new process

Change back to user mode

Run new process

26

Base/Limit Relocation

Protects address spaces

Supports dynamic relocation

Simple and inexpensive^ – Few registers, fast logic (add and compare)

Requires contiguous memory allocation^ • Over-allocation

fragmentation

No partial sharing: cannot share proper subsetof address space

27

Segmentation

• Divide address space into independent

logical areas^ – Map to user’s view of memory areas

  • E.g. code, stack, heap, libraries, functions, …
    • Placed separately in physical memory– Can grow and shrink– Have distinct protection bits

28

Memory Allocation: Segmentation

P

, S 0

0

P

, S 0

1

P

, S 0

2

P

, S 1

0

P

, S 1

1

P^1

, S

2

P

, S 0

0

P

, S 0

1

P

, S 0

2

P

, S 1

0

P

, S 1

1

P

, S 1

2

Virtual Address Space

Physical Memory

P

1 P 0

29

User’s View of a Program

30

Logical View of Segmentation

1 3

(^24)

1 4 2 3

user space

physical memory space

31

Segmentation Architecture

R^11111

W^01101

0x10000x05700x01200x0800 0x

Limit

X 0 1 0 1 0

Seg.

Base0x40000x10000x20000x0000 0x

Offset

Segment

Virtual Address:

  • High-order bits of address

segment #

  • Look up segment base in segment table
    • Low-order bits of address

offset

  • Compare offset to segment limit

Segment Table:

32

Segmentation

Sparse allocation of address space

Differentiate protection on per segment basis

Independent segment swapping/relocation

Segment requires contiguous memoryallocation^ • Large segments?

37

Paging Example

38

Paging and Fragmentation

• No external fragmentation

  • Any free frame can be allocated to a process

• Possible internal fragmentation

  • Entire frame may not be used

39

Free Frames

40

Frame/Page sizes

•^

Range from 512B to 8KB^ – Typically 4KB or 8KB

-^

Large page sizes?^ – Greater internal fragmentation

-^

Small page sizes?^ – Greater page table overhead

-^

~4 bytes per page table entry

•^

E.g. How much memory for page table to manage 64 GBof physical memory divided into 512 B frames:^ 64 GB = 2

36

bytes (physical memory)

512 B = 2

8 bytes (page size)

(^362) / 2

8 = 2

28 pages

(^282) entries x 4 bytes per entry =

2 30 bytes or 1 GB

for page table

41

Page Table Implementations

• Paging requires two lookups per access

  • 1

st

to page table to resolve requested address

  • 2

nd

to access requested address

• Desire page table reference to be fast!

  • Small page tables can use fast registers– In general, page tables too large for registers

42

Paging and Memory Access Overhead

-^

Generally, page table kept in memory^ – Page-table base register

(PTBR) points to location of

page table

  • Not all processes need entire page table
    • Page-table limit register

(PTLR) indicates size of page table

-^

Accessing main memory is slow^ – 2 accesses/reference doubles effective memory latency– Solution:

translation look-aside buffer

  • high-speed, associative cache

43

Translation Look-aside Buffer (TLB)^ •

Fast, associative hardware cache mapping key-value pairs^ – Typically 64 – 1,024 entries

-^

Used to store page #, frame # mappings

-^

TLB hit: referenced page # found^ – Mapped frame # returned immediately

-^

TLB miss: referenced page # not found^ – Look up page # in page table in physical memory– Store page #, frame # mapping in TLB

-^

TLB entry replacement strategies?

44

Paging Hardware with TLB

49

2-level Paging

50

2-level Paging Address Translation

51

Hashed Page Tables

-^

Common in address spaces > 32 bits

-^

Virtual page # hashed into page table^ – Page table contains a chain of elements

hashing to the same location

-^

Virtual page numbers are compared in thischain searching for a match^ – If a match is found, the corresponding physical

frame is extracted

52

Hashed Page Table

53

Inverted Page Table

-^

One entry for each real page of memory

-^

Inverted page table entry

-^

Decreases memory needed to store eachpage table, but increases time needed tosearch the table when a page referenceoccurs

-^

Use hash table to limit the search to one —or at most a few — page-table entries

54

Inverted Page Table

-^

Regular (per process) page table:^ – 1 entry per page (whether valid or not)

-^

Inverted (global) Table:^ – 1 entry per physical page– entry:

<process-id,

page-number>

  • On reference, search table for pid, page # match



saves memory space 

lookups are expensive

55

Inverted Page Table Architecture

56

Segmentation and Paging