0% found this document useful (0 votes)
7 views23 pages

CSC 453 Operating Systems: Memory Is A Resource

Uploaded by

aditya080160
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

CSC 453 Operating Systems: Memory Is A Resource

Uploaded by

aditya080160
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSC 453 Operating Systems

Lecture 8 : Memory Management

Memory Is A Resource
• Main memory is an array of words or bytes that
can be accessed directly by their own unique
addresses.
• Executing an instruction will involve accessing
memory several times to :
– fetch the instruction
– fetch operand(s)
– store the result
• Memory allocated to one process cannot be used
by another process (unless the process allows it).
The Same Program May Be Loaded Into
Different Memory Locations
00000 00000 00000

Preparing A Source Program For


Execution

Source Object Executable


Compiler Linker
Code Module Program

Other
Object
Module(s)

System
Library
Address Binding
Address binding can occur at:
• Compile time
– when the program is translated, absolute addresses are
generated.
– This assumes that the compiler knows where in
memory the program will be loaded.
• Load time
– The compiler generates relocatable addresses.
– The binding occurs when the program is loaded
• Execution time
– If the process can moved during execution, address
binding is delayed until run time.

Dynamic Loading

• The main program is loaded into memory and it is


executed. All other routines are stored on disk
with relocatable addresses.
• When a routine is called, it is read into memory
and the program’s address table is updated to
show the change.
• This allows large programs to run using only a
fraction of the memory that they would need if the
entire program is stored in memory.
• Users design their programs to use this approach,
so no special operating system support is needed.
Dynamic Linking
• Most programs are statically linked, i.e. the libraries are
combined with other modules to form the executable
module.
• Some systems support dynamic linking where a routine
called a stub is linked for every library routine instead of
the actual code.
• When the procedure is called, the stub is executed.
– If the procedure is not in memory, it loads it into
memory.
– The procedure replaces itself with the routine ’s address.
• Dynamic linking requires operating system support
because the procedures may be in an area of memory
belonging to another process.

Overlays
Operating Operating
System System
Common Common
procedures procedures

Overlay driver Overlay driver

Overlay 1 Overlay 2

Common Common
data data
Logical Vs. Physical Address
• Logical Address – The address used by the
instructions to load and store data, jump to
other instructions, etc.
• Physical Address – Used by the hardware to
load and store data, fetch instructions, etc.

Relocatable Addressing

25000

Jump +
to 497 497 25497

physical
logical address address
What Is Swapping?

Operating
System

P1

User Space
Occupied by P2
Process P1

Backing Store

Swapping Out A Process

Operating
System

P1

Process P1
Is Written on P2
Backing Store

Backing Store
Swapping In A Process

Operating
System

P1

Process P2
Is Read From P2
Backing Store

Backing Store

Bare Machine

No Restrictions On
Memory Use
-
No Operating System
Services
Organizing Memory
• Main memory is comprised of two
partitions:
– Operating system
– User processes
• The operating system can be at the high end
or low end of memory – the location of the
interrupt vector is largely responsible for
where the operating system will reside.

Single-Partition Allocation

000000
Operating System

User Process

FFFFFF
Single-Partition Allocation With Transient
Operating-System Code

000000

Operating System Rarely-used


device driver

User Process

FFFFFF

Hardware Support For Limit and Relocation


Registers

Limit Relocation
register register

yes Memory
CPU <? +

logical physical
no
address address
Multiple-Partition Allocation
• More than one user process can reside in
memory at a time in contiguous memory
allocations. There are two possible
approaches:
– There are a fixed maximum number of
processes than can be resident at a time.
– The number of process that can reside in
memory can vary.

MFT
• MFT (Multiprogramming with Fixed
number of Tasks) assumes that there will be
up to N processes resident in memory.
• Memory is divided into N partitions of fixed
sizes (that are not necessarily equal).
• MFT was used in IBM’s OS/360 and is no
longer used.
MVT
• MVT (Multiprogramming with Variable
number of Tasks) assumes that there is no
fixed maximum number of resident
processes.
• The operating system keeps track of what
parts of memory are in use and when a
process needs a memory allocation, it looks
for a partition in memory in which it will fit.

MVT and Allocating Memory


000000 Operating 000000 Operating 000000 Operating
system system system
019000 019000 019000
P1 P1

270000 270000
P2
Available allocate P 1 allocate P 2
memory
670000

FFFFFF FFFFFF FFFFFF


MVT and Terminating Processes
000000 Operating 000000 Operating 000000 Operating
system system system
119000 119000 119000
P1 P1 P1

270000 270000 270000


P2 P2
allocate P 3 terminate P 2

670000 670000 670000


P3
P3

E00000 E00000
FFFFFF FFFFFF FFFFFF P4

MVT and Fragmentation


000000 Operating 000000 Operating
system system
119000 119000
P1 P1

270000 270000
P2 Process P 4doesn’t fit
allocate P 4

670000 670000
P4
P3
P3

E00000 E00000
FFFFFF FFFFFF
The Dynamic Storage -Allocation
Problem
• There are three different algorithms that we
can use to select an available block of
memory into which the operating system
will load the program:
• First fit
– Best fit
– Worst fit

External & Internal Fragmentation


Operating
System

P1
Internal
fragmentation

External
P2
fragmentation

P3
Compaction
Operating Operating
System System

Before
P1 compaction P1

P2
After
P2
compaction
P3

P3

Other Compaction Strategies


Operating Operating Operating
System System System

P1 P1
P1

P3 P2

P2

P2

P3
P3
What Is Paging?
• In a paged memory management system, physical
memory is divided into fixed-sized block called
frames into which are loaded blocks of logical
memory.
• All the addresses appear to be contiguous but they
are not necessarily stored in contiguous areas of
memory.
• This eliminates the problem of external
fragmentation.

Logical and Physical Addressing in


Paged Memory System
Logical Page Number Displacement
Address

Physical Frame Number Displacement


Address
Hardware Support For Paging
CPU Physical Memory

physical
address
f d
Page
Table
logical address
p d p

Pages and Frames Page 1

Page 2
Page Table

Page 0 0 1 Page 0

Page 1
1 2

Page 2 Page 4
2 0
Page 3

3 4
Page 4
Page 6
4 6
Logical memory Physical memory
Loading a New Process
0 1 0 1
0 Free-frame list 0
1 1
7
2 2 2
6
0
3 2 2 3 2
1 10
4 3 4 3
2 9
5 3 8 5
6 11 6 1
12
7 New process 7 0
5
8 8

9 9

10 10 3

11 11

12 12

Using Registers For the Page Table

displacement
page #

Page Table

f
Associative Registers and Paging
CPU p d
p f TLB Hit

f d
Main
Memory
p

f
TLB Miss

Paging And Memory Protection


PTLR

f v/i f r/w f r/w/x

(a) (b) (c)


2-Level 32-bit Paging

logical address
10 bits 10 bits 12 bits

p1 p2 d

p2

Outer-page
Page of
table
Page Table

Paging on the VAX

logical address
2 bits 21 bits 9 bits

s p d

Outer-page
table
d

Page of
Page Table
Inverted Page Tables
logical address physical address

pid p d
i d
i
search

pid p

Page Table

Shared Pages
0 data 1
pgm 1 2 1 pgm 2 2 pgm 1
pgm 2 2 pgm 1 pgm 2
1 1
3 data 2
pgm 3 pgm 3
6 4 pgm 4 6
pgm 4 pgm 4
5
data 1 4 6 pgm 3 4 data 1

data 2 7 data 2
0 8
8 data 1
Process 1 Process 2
3 9 data 2 9
10
Process 1 11 Process 2
Page Table Page Table
12
Physical memory
Segmentation

main Library
program routines

Sub 1 stack

global
Sub 2 data

A user’s program can be thought of as a collection


of separate segments

Segmentation Hardware
STBR

s
s d
logical limit base
address

yes
< + physical
address

no

addressing error
Segmentation and Memory
Protection
• Segments represent different components of
a program, such as code and data.
– Code segments can be marked as read-only or
execute-only.
– Segments containing arrays can check array
indices automatically.
– Code and data segments can be shared by
different processes.

Segments and Fragmentation


• Memory allocation works in a manner
similar to paging, except that segments are
of variable size.
– There will external fragmentation, which can be
alleviated by compaction.
– The problem can be minimized by working
with smaller segments.
Segmentation With Paging
• The two approaches could be used together
to improve memory management
– MULTICS paged the memory segments (which
could be as large as 64K) to minimize external
fragmentation.
– The 32-bit version of OS/2 took a similar
approach.

You might also like