Docsity

Memory Management - Lecture Slides | EECS 678, Study notes of Operating Systems

Toggle sidebar
bg1
1


■Background
■Swapping
■Contiguous Memory Allocation
■Paging
■Segmentation
■Segmented Paging
2


■Memory is a large array of bytes
●memory and registers are only storage CPU can access directly
●both instructions and data are fetched from memory
●memory unit just sees a stream on memory addresses
■Accessing memory is slow (why?)
●register access in one CPU clock (or less)
●cache sits between main memory and CPU registers
■Protection of memory required to ensure correct
operation
●isolate processes from one another and from the OS
3


■Protect OS from user processes
and processes from each other
●give each process separate space
●determine legal range of addresses
for each process
■A pair of base and limit
registers define the process
address space.
■CPU compares each address
generated by the process with
the base and limit registers.
4


■Address binding of instructions and data to memory
addresses can happen at three different stages
●compile time: If memory location known a priori, absolute code
can be generated
must recompile code if starting location changes
●load time: Must generate relocatable code if memory location is
not known at compile time
code can be placed anywhere in memory
●execution time: Binding delayed until run time if the process can
be moved during its execution from one memory segment to
another
need hardware support for address maps (e.g., base and limit
registers)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Memory Management - Lecture Slides | EECS 678 and more Study notes Operating Systems in PDF only on Docsity!

1



 

^       

n^

Background

n^

Swapping

n^

Contiguous Memory Allocation

n^

Paging

n^

Segmentation

n^

Segmented Paging



 

^       

n^

Memory is a large array of bytes^ l^

memory and registers are only storage CPU can access directly l both instructions and data are fetched from memory l memory unit just sees a stream on memory addresses

n^

Accessing memory is slow (why?)^ l^

register access in one CPU clock (or less) l cache

sits between main memory and CPU registers

n^

Protection of memory required to ensure correctoperation^ l^

isolate processes from one another and from the OS

3



 

^       

n^

Protect OS from user processesand processes from each other^ l^

give each process separate space l determine legal range of addressesfor each process

n^

A pair of

base

and

limit

registers define the processaddress space.

n^

CPU compares each addressgenerated by the process withthe base and limit registers.



 

^       

n^

Address binding of instructions and data to memoryaddresses can happen at three different stages^ l^

compile time

: If memory location known a priori,

absolute code

can be generated^ ^

must recompile code if starting location changes l^ load time

: Must generate

relocatable code

if memory location is

not known at compile time^ ^

code can be placed anywhere in memory l^ execution time

: Binding delayed until run time if the process can

be moved during its execution from one memory segment toanother^ ^

need hardware support for address maps (e.g., base and limitregisters)

5



 

^       



 

^       

n^

The concept of a logical address space that is bound to aseparate

physical address space

is central to proper

memory management^ l^

logical address

  • generated by the CPU; also referred to as

virtual address l physical address

  • address seen by the hardware memory unit

n^

Logical and physical addresses are the same in compile-time and load-time address-binding schemes.

n^

Logical and physical addresses differ in execution-timeaddress-binding scheme.

7



 

^       



n^

Hardware device that mapsvirtual to physical address.

n^

Simple MMU scheme^ l^

start each process's logicaladdress from 0 l base memory address for eachprocess stored in

relocation

register l max. process address stored in limit register l relocation register added toevery logical address l logical: 0 –

max

correspond to

physical: R+0 – R+limit



 

^       

n^

Static

loading l^ entire program in memory before program starts l^ program size limited by size of memory

n^

Dynamic loading^ l^

routine is not loaded until it is called l better memory-space utilization; unused routine is never loaded l useful when large amounts of code are needed to handleinfrequently occurring cases l no special support from the operating system is requiredimplemented through program design

13



 

^       

n^

Relocation and limit registers used to protection^ l^

of user processes from each other, and l operating system from the user processes

n^

Hardware checks^ l^

base register contains value of smallest physical address l each logical address must be less than the limit register l MMU maps logical address

dynamically



 

^       

^

n^

Multiple-partition method^ l^

hole

  • block of available memory ^ holes of various size are scattered throughout memory l^ when a process arrives, it is allocated memory from a hole largeenough to accommodate it l^ operating system maintains information about:a) allocated partitions

b) free partitions (hole)

OS process 5process 8process 2

OS process 5 process 2

OS process 5 process 2

OS process 5process 9 process 2

process 9

process 10

15



 

^       

n^

Given

a

list

of

variable-sized

holes,

how

to

allocate

memory for a new arriving process of some size,

n

n^

Memory allocation schemes^ l^

First-fit

: Allocate the

first

hole that is big enough

l^ Best-fit

: Allocate the

smallest

hole that is big enough; must

search entire list, unless ordered by size^ ^

produces the smallest leftover hole l^ Worst-fit

: Allocate the

largest

hole; must also search entire list

^ produces the largest leftover hole

n^

Performance^ l^

first-fit and best-fit better than worst-fit in terms of performanceand storage requirement l first-fit generally the fastest



 

^       

n^

External Fragmentation

  • total memory space exists to

satisfy a request, but it is not contiguous

n^

50-percent rule^ l^

given N allocated blocks, 0.5N blocks will be lost to fragmentation

n^

Solutions to external fragmentation^ l^

compaction^ ^

move allocated blocks to place all

holes

together

^ compaction is possible

only

if relocation is dynamic

l^ permit non-contiguous allocation of logical address space

n^

Internal Fragmentation^ l^

occurs with fixed sized blocks used to reduce cost of managingextremely small holes l size of last block may be slightly larger than requested memory l what is the greatest memory loss due to internal fragmentation?

17



 

^       

n^

Solution to external fragmentation and compaction^ l^

logical address space of a process can be noncontiguous l commonly used in most OS l frequently depend on hardware support

n^

Scheme^ l^

divide physical memory into fixed-sized blocks called

frames

^ size is generally power of 2, between 512 bytes and 8,192 bytes l^ divide logical memory into blocks of same size called

pages

l^ OS keeps track of all free frames l^ a program of size

n^ pages, needs

n^ free frames before loading

l^ needs a

page table

to translate logical to physical addresses

n^

Problem of internal fragmentation still exists



 

^       

n^

Hardware defines the page size (and frame size).

n^

Page size,

d , determines the number of bits required to

specify the page offset,

n.

l^

How? l^ d = 2

n

n^

The remaining bits in the logical address specify thepage number,

p.

n^

For logical address space, 2

m, and page size 2

n,

page number

page offset

p^

d

m - n

n

19



 

^       



 

^       

^

25



 

^       



 

^       

n^

Memory protection implemented by associating protectionbit with each frame

n^

Valid-invalid

bit attached to each entry in the page table:

l^ “valid” indicates that the associated page is in the process’ logicaladdress space, and is thus a legal page l^ “invalid” indicates that the page is not in the process’ logicaladdress space

27



 

^       

n^

Scheme^ l^

14 bit address space l Program usesaddresses 0 – 10468 l Page size of 2kb l Accessing addresseson pages 6 and 7 isinvalid^ ^

segmentation fault

n^

Problems^ l^

addresses 10469 –12287 are still valid l page table stillcontains all rows l use page table lengthregister



 

^       

!^

n^

Shared code^ l^

Paging allows the possibility ofeasily sharing code l one copy of read-only (reentrant)code shared among processes(i.e., text editors, compilers,window systems) l each process has private data l significant reduction in memoryusage possible

n^

Example^ l^

3 code pages shared l each process has private data

29



 

^       

n^

Hierarchical Paging

n^

Hashed Page Tables

n^

Inverted Page Tables



 

^       

n^

Sometimes necessary to divide page table into pieces^ l^

to support a large logical address space (

32 to 2

l^ example: for page size of 4kb, page table has 2

20 entries

l^ contiguous allocation of big page tables not feasible

n^

Two-level page table (paged page-table)^ l^

further divide the page number field l p1 –

index into outer page table l^ p2 –

displacement within outer page table

page number

page offset

p i^

p^2

d

^

^



31



 

^       



 

^       

37



 

^       

.^



 

^       

n^

Memory-management scheme that supports user view ofmemory^ l^

a program is a collection of segments

(^13)

(^24)

1 4 2 3

user space

physical memory space

39



 

^       

n^

Logical address consists of a two tuple:

<segment-number, offset>

n^

Segment table

  • maps two-dimensional physical

addresses; each table entry has^ l^

base

  • contains the starting physical address where the segments reside in memory l limit - specifies the length of the segment

n^

Segment-table base register (STBR)

points to the

segment table’s location in memory

n^

Segment-table length register (STLR)

indicates number

of segments used by a program;

segment number

s^

is legal if

s^ <^ STLR



 

^       

41



 

^       



 

^       

n^

Supports both segmentation and segmentation withpaging

n^

CPU generates logical address^ l^

given to segmentation unit^ ^

which produces linear addresses l^ linear address given to paging unit

^ which generates physical address in main memory ^ paging units form equivalent of MMU

43



 

^       

.^

n^

32-bit architecture

n^

Max. number of segments – 16K^ l^

8k segments in local descriptor table (LDT) l 8k segments in global descriptor table (GDT)

n^

Max. size of segment – 4GB

n^

Each segment descriptor is 8 bytes^ l^

contains segment base and limit

n^

Each logical address is 48 bits^ l^

16 bit segment selector, 32 bit offset

n^

16 bit segment selector contains^ l^

13 bit segment number, 1 bit table identifier, 2 bits of protection

n^

Maps 48 bit logical address to 32 bit linear address



 

^       

.^