Memory Management

squabbletownmushySoftware and s/w Development

Dec 14, 2013 (3 years and 8 months ago)

82 views





MEMORY
MANAGEMENT








Presented By
:
-







Lect. Puneet Gupta







G.P.C.G. Patiala

Operating System


A

operating

system

is

a

set

of

master

programs

which

is

desired

to

control

entire

operations

of

computer
.

Operating

System

is

a

general

purpose

software

which

is

involved

in

each

and

every

activity

of

the

computer
.

The

various

functions

of

operating

system

are


Interface

between

user

and

hardware


Peripheral

Management


Memory

Management


Process

Management


Memory


Memory refers to the physical devices used to
store the data temporary or permanent. There
are two types of memories that are

1
.

Primary

Memory
:
-

These

are

internal

memory

of

computer
.

They

are

very

fast
.

The

two

types

of

primary

memory

are

RAM

and

ROM
.

2
.

Secondary

Memory
:
-

These

are

physical

devices

to

store

program

and

data
.

They

are

slow

to

access

with

higher

memory

capacity
.

The

various

types

of

secondary

memories

are

tape,

magnetic

disks

and

optical

discs
.


Principals of Memory Management


The

various

aim

and

principals

of

memory

management

are


To

provide

the

memory

space

to

enable

many

processes

to

be

executed

at

same

time
.


To

protect

the

processes

from

each

other
.


To

provide

satisfactory

level

as

performance

to

users
.



To

make

addressing

of

memory

space

transparent

for

an

programmer
.

Address Binding


Each

program

resides

on

a

disk

but

it

must

be

brought

to

memory

for

execution
.

The

process

has

to

be

moved

between

disk

and

memory
.

Thus

different

memory

address

are

used

at

different

location
.

The

process

of

changing

the

addresses

during

storage

is

called

binding
.

The

various

types

of

binding

are

1.
Compile

Time

Binding

2.
Load

Time

Binding

3.
Execution

Time

Binding

Physical and Logical Address


1
.

Physical

Address
:
-

The

actual

addresses

that

refer

to

actual

physical

location

of

the

main

memory

are

called

physical

addresses
.

2
.

Logical

Address
:
-

The

addresses

generated

by

CPU

are

called

Logical

addresses
.


The

logical

and

physical

addresses

are

same

at

compile

time

and

load

time

binding
.

But

it

differs

at

Execution

time

binding
.

The

mapping

of

logical

address

to

physical

addresses

is

done

by

hardware

device

called

Memory

Management

Unit
.


Relocation


The

logical

addresses

are

converted

into

physical

addresses

by

the

help

of

relocation

register

present

in

Memory

Management

Unit

or

by

relocating

linkers

and

loader
.

The

ability

to

load

and

execute

a

given

program

to

arbitrary

place

in

memory

is

called

relocation
.

The

user

program

supplies

logical

address

must

be

mapped

to

physical

addresses

before

they

are

used
.

The

types

of

Relocation

are

1.
Static

Relocation


2.
Dynamic

Relocation

Relocation



The

types

of

Relocation

are

1.
Static

Relocation

:
-

The

Static

Relocation

is

performed

before

or

during

the

loading

of

program

into

memory
.

This

is

done

by

relocating

linkers

and

relocating

loaders
.


2.
Dynamic

Relocation

:
-

The

Dynamic

relocation

is

performed

during

the

execution

of

the

program
.

This

is

done

by

the

help

of

the

hardware

support

called

Memory

management

Unit

(MMU)
.

Relocation










Logical Address



Physical


Address





346




14346











MMU

CPU

Relocation
Register

Memory

+

14000

Partitioning


The

Operating

System

manages

the

main

memory

by

dividing

the

memory

into

regions

of

fixed

or

variable

size

is

called

Partitioning
.

It

manages

the

main

memory

efficiently
.

The

two

types

of

partitions

are

1.
Static

Partition
:
-

The

whole

memory

is

divided

into

fixed

sized

frame

is

called

static

partition
.

2.
Dynamic

Partition
:
-

The

memory

is

divided

into

variable

sized

frame

according

to

page

size

is

called

dynamic

partition
.

Partitioning


Example








0




0





10




10




15




15







20











30




33












38






40




40











47




50




50

Static

Partition

Dynamic

Partition


P1

P2

P3

P3

P4

process

space

P1

10

P2

5

P3

18

P4

7

P1

P2

P3


P4

Fragmentation


Fragmentation

is

a

process

in

which

storage

space

is

used

inefficiently,

reducing

capacity

and

often

performance
.

Fragmentation

leads

to

storage

space

being

wasted,

and

the

term

also

refers

to

the

wasted

space

itself
.

The

two

types

of

fragmentation

are

1
.

External

fragmentation


2
.

Internal

fragmentation


This

problem

is

overcome

by

using

Disk

defragmentation
.


Contiguous Memory Allocation


Each

logical

object

is

placed

in

physical

memory

at

consecutive

addresses

is

called

contiguous

memory

allocation
.

Generally

memory

is

partitioned

and

any

process

requesting

memory

is

allocated

a

partition
.

On

termination,

process

release

memory

partition

to

a

free

pool

of

memory
.

There

are

number

of

scheme

which

can

be

used

for

a

system

but

we

can

rate

efficiency

of

a

particular

memory

management

scheme

like

wasted

memory,

time

complexity,

memory

access

overheads
.


Static Partitioned Memory Allocation


The

memory

partition

are

created

sometime

before

the

execution

of

user

programs

these

partitions

remain

fixed
.

This

approach

is

known

as

Static

Partitioned

Memory

Allocation
.

The

number

and

size

of

partitions

is

fixed

during

system

generation

process

by

considering


1
.

Capacity

of

memory,


2
.

Degree

of

multiprogramming,


3
.

Size

of

program
.


Dynamic Partitioned Memory Allocation


The

memory

partition

are

created

dynamically

in

response

to

process

requests
.

This

approach

is

known

as

Dynamic

Partitioned

Memory

Allocation
.

The

partition

creation

process

goes

on

till

the

whole

memory

has

been

utilized
.

Whenever

a

process

requests

for

memory

partition,

memory

management

of

operating

system

creates

a

suitable

partition

to

allocate

to

the

process
.

It

selects

the

greater

or

equal

size

to

process,

allocate

the

memory

and

keeps

the

record

in

PDT
.

Algorithms


1.
First

Fit
:
-

OS

searches

the

first

partition

large

enough

to

store

process
.

2.
Next

Fit
:
-

The

memory

manager

keep

a

pointer

on

the

free

list

to

store

process
.

3.
Best

Fit
:
-

Memory

manager

searches

free

list

from

beginning

to

end

and

selects

the

smallest

possible

portion

to

hold

process
.


4.
Worst

Fit
:
-

The

memory

manager

scans

the

free

list

and

allocate

the

largest

memory

block

to

create

partition

to

store

process
.

Swapping


The

process

of

a

suspended

or

preempted

process

in

main

memory

with

a

new

process

is

called

swapping
.

When

all

memory

partitions

have

been

allocated

and

there

is

need

to

allocate

a

partition

to

a

process,

a

suspended

or

preempted

processes

is

taken

out

from

the

memory

in

order

to

bring

new

process
.

Swapping

is

used

to

implement

the

multi

programming

in

the

system

which

have

less

hardware

support

for

memory

management
.

Swapping

is

done

by

module

called

Swapper

Swapping








Swap out



Swap in


Operating
System






User Space

Process
P1

Process
P2

Compaction


The

process

of

relocating

all

partitions

into

one

end

of

memory

and

to

combine

holes

into

large

free

area

is

called

compaction
.

When

the

memory

becomes

fragmented,

solution

is

to

relocate

all

partition

into

one

end

using

compaction
.

All

the

effected

processes

are

suspended

and

they

are

copied

from

one

to

another

area

of

memory
.

It

can

be

performed

whenever

possible

or

only

when

needed
.

System

compacts

memory

when

large

free

area

is

return

or

there

is

failure

to

allocate

memory
.



Paging


Paging

is

a

memory

management

scheme

that

permits

the

physical

address

space

of

a

process

to

be

non

contiguous
.

In

paging

physical

memory

is

divided

into

fixed

sized

partitions

called

frames
.

Virtual

address

space

is

also

divided

into

blocks

of

same

size

called

pages
.

A

page

map

table

is

used

for

address

translation
.

The

virtual

address

of

a

process

is

divided

into

two

parts

that

are

1.
Page number (p)

2.
Page offset (d)


Paging



Segmentation


Segmentation

is

memory

management

scheme

in

which

the

address

space

of

single

process

is

divided

into

blocks

that

may

be

placed

into

non

contiguous

area

of

memory
.

Due

to

external

fragmentation

the

waste

memory

increases

which

should

be

reduced

in

system

by

using

the

segmentation

to

utilize

the

memory

in

proper

manner
.

Segment

descriptor

table

is

use

to

record

the

details

of

different

segment

belonging

to

different

process

area

in

memory
.

It

is

flexible

and

easy

to

use
.


Segmentation


The

translation

from

virtual

to

physical

address

in

main

memory

is

done

as

follows
.

1.
The

segment

number

of

virtual

address

is

used

to

access

the

segment

descriptor

table
.

2.
It

is

checked

that

whether

the

offset

of

segment

is

within

bound

of

segment
.

3.
The

base

address

of

segment

is

obtained

from

SDT
.

4.

The

offset

of

the

virtual

address

is

added

to

physical

base

address

to

get

actual

physical

address
.


Segmentation



Virtual Memory


The

secondary

memory

that

is

used

to

keep

the

portions

of

address

spaces

of

currently

executing

process

is

called

virtual

memory
.

The

portions

are

shifted

to

and

fro

between

main

memory

and

virtual

memory
.

Virtual

memory

is

a

technique

that

allows

the

execution

of

processes

that

may

not

be

completely

in

memory
.

By

this

method

CPU

can

execute

the

program

as

large

as

physical

memory
.

Virtual Memory


The

various

issue

that

are

specific

to

virtual

memory

concept

are

1.
Allocation

Policy
:
-

decides

how

much

real

memory

to

be

allocated

to

each

process
.

2.
Fetch

Policy

:
-

decides

when

and

which

items

are

to

be

brought

to

main

memory
.

3.
Replacement

Policy
:
-

decides

which

item

should

replaced

with

new

memory
.


4.
Placement

policy
:
-
decides

about

placement

of

an

incoming

item

in

physical

memory
.

Demand Paging


A

demand

paging

system

is

similar

to

a

paging

system

with

swapping
.

Processes

reside

on

secondary

memory
.

When

we

want

to

execute

a

process,

we

swap

it

into

memory
.

A

swapper

swaps

the

page

into

memory

only

when

it

needed
.

It

avoids

reading

into

memory

pages

that

will

not

be

used

anyway

thus

decreasing

the

swap

time

and

amount

of

physical

memory

needed
.

The

hardware

support

for

demand

paging

is

page

table

and

secondary

memory

which

is

same

as

in

paging

and

swapping
.


Page Replacement


To

reduce

the

page

faults

occurrence,

we

used

the

technique

of

page

replacement
.

In

this

if

no

frame

is

free,

the

OS

finds

the

one

that

is

not

in

used

and

make

it

free
.

Then

this

freed

frame

is

used

to

hold

the

page

which

is

faulted
.

The

routine

for

page

replacement

are

1.
Find

the

location

of

desired

page

on

disk
.

2.
Find

a

free

frame


a
.

if

there

is

a

free

frame,

use

it
.


b
.

if

there

is

no

free

frame,

use

a

page

Page Replacement



replacement

algorithm

to

select

the

victim

frame

and

write

victim

page

to

disk

change

the

page

and

frame

table

accordingly
.

3
.

Read

the

page

to

free

frame,

change

the

page

and

frame

tables
.

4

.

Restart

the

user

process
.

The

algorithm

used

for

page

replacement

are



a
.

FIFO

Page

Replacement,



b
.

LRU

page

Replacement,


c
.

Optimal

Page

Replacement

First In First Out


FIFO

replacement

algorithm

is

the

simplest

replacement

algorithm
.

In

this

page

which

is

the

oldest

one

is

replaced

with

the

new

page
.

It

use

the

page

queue

in

which

the

page

at

the

head

is

replaced

with

the

page

inserted

at

the

tail
.

It

is

implement

by

using

the

FIFO

queue
.

There

is

Belady

anomaly

problem

with

FIFO

replacement

algorithm
.

The

number

of

page

faults

are

increases

with

increase

in

number

of

frames

in

main

memory
.


First In First Out


Example
:
-

Consider

the

main

memory

consists

of

3

frames

for

FIFO

Page

Replacement

algorithm

and

input

pattern

of

memory

pages
.



7

0

1

2

0

3

0

4


2


3

0



7

7

0

7

0

1

2

0

1

2

3

1

2

3

0

4

3

0

4

2

0

2

0

1

4

2

3

0

2

3

Optimal Page Replacement


The

Optimal

Page

Replacement

algorithm

replace

the

page

that

will

not

be

used

for

longest

period

of

time
.

The

page

which

might

not

be

used

for

longer

time

is

replaced

with

the

new

page
.

This

replacement

algorithm

has

lowest

page

fault

rate
.

This

algorithm

is

difficult

to

implement

as

it

requires

future

knowledge

of

the

reference

string
.

It

overcome

the

belady’s

anomaly
.

Optimal Page Replacement


Example
:
-

Consider

the

main

memory

consists

of

3

frames

for

Optimal

Page

Replacement

algorithm

and

input

pattern

of

memory

pages
.



7

0

1

2

0

3

0

4


2


3

0


7

7

0

7

0

1

2

0

1

2

0

3

2

0

3

2

4

3

2

4

3

2

0

1

2

4

3

2

0

3

Least Recently Used


The

least

recently

used

algorithm

will

replace

the

page

that

has

not

been

used

for

longest

period

of

time
.

LRU

replacement
.

LRU

chooses

that

page

which

has

not

been

used

for

the

longest

period

of

time
.

This

algorithm

is

looking

backward

in

time,

rather

than

forward

that

is

used

in

Optimal

page

replacement
.

The

two

implementation

for

LRU

are

Counters

and

Stack
.


Least Recently Used


Example
:
-

Consider

the

main

memory

consists

of

3

frames

for

LRU

Page

R
eplacement

algorithm

and

input

pattern

of

memory

pages



7

0

1

2

0

3

0

4


2


3

0




7

7

0

7

0

1

2

0

1

2

0

3

2

0

3

4

0

3

4

0

2

2

0

1

4

3

2

0

3

2

Summary


Memory

refers

to

the

physical

devices

used

to

store

the

data

temporary

or

permanent
.


The

process

of

changing

the

addresses

during

storage

is

called

binding
.



The

binding

is

of

three

types

are

Compile

Time,

Load

Time,

Execution

Time

Binding
.


The

actual

addresses

that

refer

to

actual

physical

location

of

main

memory

are

called

physical

addresses
.


The

addresses

generated

by

CPU

are

called

Logical

addresses
.




Summary


The

OS

manages

the

main

memory

by

dividing

the

memory

into

regions

of

fixed

or

variable

size

is

called

Partitioning
.


The

secondary

memory

that

is

used

to

keep

the

portions

of

address

spaces

of

currently

executing

process

is

called

virtual

memory
.



A

demand

paging

system

is

similar

to

a

paging

system

with

swapping


The

algorithm

used

for

page

replacement

are

FIFO

Page

Replacement,

LRU

page

Replacement,

Optimal

Page

Replacement
.





Thanks