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
Code
Compiler Object
Module
Linker Executable
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
System
Common
procedures
Overlay driver
Common
data
Overlay 1
Operating
System
Common
procedures
Overlay driver
Common
data
Overlay 2
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
Jump
to 497
+
25000
497
logical address
25497
physical
address
What Is Swapping?
P2
P1
Backing Store
Operating
System
User Space
Occupied by
Process P1
Swapping Out A Process
P2
P1
Backing Store
Operating
System
Process P1
Is Written on
Backing Store
Swapping In A Process
P2
P1
Backing Store
Operating
System
Process P2
Is Read From
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
Operating System
User Process
FFFFFF
000000
Single-Partition Allocation With Transient
Operating-System Code
Operating System
User Process
FFFFFF
000000
Rarely-used
device driver
Hardware Support For Limit and Relocation
Registers
CPU <? +
Memory
Limit
register
Relocation
register
logical
address
yes
no
physical
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
FFFFFF
Operating
system
019000
Available
memory
allocate P1
000000
FFFFFF
Operating
system
019000
270000
P1
allocate P2
000000
FFFFFF
Operating
system
019000
270000
P1
670000
P2
MVT and Terminating Processes
allocate P3
000000
FFFFFF
Operating
system
119000
270000
P1
terminate P2
000000
FFFFFF
Operating
system
119000
270000
P1
670000
000000
FFFFFF
Operating
system
119000
270000
P1
670000
P2 P2
P3
P3
670000
E00000 E00000
P4
MVT and Fragmentation
000000
FFFFFF
Operating
system
119000
270000
P1
allocate P4
000000
FFFFFF
Operating
system
119000
270000
P1
670000
P2
P3
P3
670000
E00000 E00000
Process P4
doesn’t fit
P4
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
P2
P3
Internal
fragmentation
External
fragmentation
Compaction
Operating
System
P1
P2
P3
Operating
System
P1
P2
Before
compaction
P3
After
compaction
Other Compaction Strategies
Operating
System
P1
P2
P3
Operating
System
P1
P2
P3
Operating
System
P1
P2
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
Address
Physical
Address
Displacement
Page Number
Frame Number Displacement
Hardware Support For Paging
p d
CPU
logical address
f
Page
Table
p
physical
address
f d
Physical Memory
Pages and Frames
Page 0
Page 2
Page 1
Page 4
Page 3
Page 4
Page 6
6
4
0
2
1
4
3
1
2
0
Page 1
Page 2
Page 0
Logical memory Physical memory
Page Table
Loading a New Process
0
1
2
3
New process
1
2
3
4
5
6
7
8
9
10
11
12
0
0
2
3
1
Free-frame list
7
6
2
10
9
8
11
12
5
1
2
3
4
5
6
7
8
9
10
11
12
0
0
2
3
1
1
2
3
0
Using Registers For the Page Table
displacement
page #
f
p
Page Table
Associative Registers and Paging
CPU p d
f
p
p f
f d
Main
Memory
TLB Hit
TLB Miss
Paging And Memory Protection
f v/i f r/w f r/w/x
PTLR
(a) (b) (c)
2-Level 32-bit Paging
p1 p2 d
logical address
10 bits 10 bits 12 bits
p2
d
Outer-page
table
Page of
Page Table
Paging on the VAX
s p d
logical address
2 bits 21 bits 9 bits
p
d
Outer-page
table
Page of
Page Table
Inverted Page Tables
p d
logical address
pid
i
pid p
Page Table
d
physical address
i
search
Shared Pages
pgm 1
pgm 2
pgm 3
pgm 4
data 1
data 2
2
1
6
4
0
3
Process 1
Process 1
Page Table
pgm 1
pgm 2
pgm 3
pgm 4
data 1
data 2
2
1
6
4
8
9 Process 2
Process 2
Page Table
1
0
2
3
4
5
6
7
8
9
10
11
12
pgm 1
pgm 2
pgm 3
pgm 4
data 1
data 2
data 1
data 2
Physical memory
Segmentation
main
program
Sub 1
Sub 2
Library
routines
stack
global
data
A user’s program can be thought of as a collection
of separate segments
Segmentation Hardware
STBR
s d
limit base
s
< + physical
address
logical
address
yes
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.

muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg

  • 1.
    CSC 453 OperatingSystems 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).
  • 2.
    The Same ProgramMay Be Loaded Into Different Memory Locations 00000 00000 00000 Preparing A Source Program For Execution Source Code Compiler Object Module Linker Executable Program Other Object Module(s) System Library
  • 3.
    Address Binding Address bindingcan 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.
  • 4.
    Dynamic Linking • Mostprograms 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 System Common procedures Overlay driver Common data Overlay 1 Operating System Common procedures Overlay driver Common data Overlay 2
  • 5.
    Logical Vs. PhysicalAddress • 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 Jump to 497 + 25000 497 logical address 25497 physical address
  • 6.
    What Is Swapping? P2 P1 BackingStore Operating System User Space Occupied by Process P1 Swapping Out A Process P2 P1 Backing Store Operating System Process P1 Is Written on Backing Store
  • 7.
    Swapping In AProcess P2 P1 Backing Store Operating System Process P2 Is Read From Backing Store Bare Machine No Restrictions On Memory Use - No Operating System Services
  • 8.
    Organizing Memory • Mainmemory 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 Operating System User Process FFFFFF 000000
  • 9.
    Single-Partition Allocation WithTransient Operating-System Code Operating System User Process FFFFFF 000000 Rarely-used device driver Hardware Support For Limit and Relocation Registers CPU <? + Memory Limit register Relocation register logical address yes no physical address
  • 10.
    Multiple-Partition Allocation • Morethan 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.
  • 11.
    MVT • MVT (Multiprogrammingwith 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 FFFFFF Operating system 019000 Available memory allocate P1 000000 FFFFFF Operating system 019000 270000 P1 allocate P2 000000 FFFFFF Operating system 019000 270000 P1 670000 P2
  • 12.
    MVT and TerminatingProcesses allocate P3 000000 FFFFFF Operating system 119000 270000 P1 terminate P2 000000 FFFFFF Operating system 119000 270000 P1 670000 000000 FFFFFF Operating system 119000 270000 P1 670000 P2 P2 P3 P3 670000 E00000 E00000 P4 MVT and Fragmentation 000000 FFFFFF Operating system 119000 270000 P1 allocate P4 000000 FFFFFF Operating system 119000 270000 P1 670000 P2 P3 P3 670000 E00000 E00000 Process P4 doesn’t fit P4
  • 13.
    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 P2 P3 Internal fragmentation External fragmentation
  • 14.
  • 15.
    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 Address Physical Address Displacement Page Number Frame Number Displacement
  • 16.
    Hardware Support ForPaging p d CPU logical address f Page Table p physical address f d Physical Memory Pages and Frames Page 0 Page 2 Page 1 Page 4 Page 3 Page 4 Page 6 6 4 0 2 1 4 3 1 2 0 Page 1 Page 2 Page 0 Logical memory Physical memory Page Table
  • 17.
    Loading a NewProcess 0 1 2 3 New process 1 2 3 4 5 6 7 8 9 10 11 12 0 0 2 3 1 Free-frame list 7 6 2 10 9 8 11 12 5 1 2 3 4 5 6 7 8 9 10 11 12 0 0 2 3 1 1 2 3 0 Using Registers For the Page Table displacement page # f p Page Table
  • 18.
    Associative Registers andPaging CPU p d f p p f f d Main Memory TLB Hit TLB Miss Paging And Memory Protection f v/i f r/w f r/w/x PTLR (a) (b) (c)
  • 19.
    2-Level 32-bit Paging p1p2 d logical address 10 bits 10 bits 12 bits p2 d Outer-page table Page of Page Table Paging on the VAX s p d logical address 2 bits 21 bits 9 bits p d Outer-page table Page of Page Table
  • 20.
    Inverted Page Tables pd logical address pid i pid p Page Table d physical address i search Shared Pages pgm 1 pgm 2 pgm 3 pgm 4 data 1 data 2 2 1 6 4 0 3 Process 1 Process 1 Page Table pgm 1 pgm 2 pgm 3 pgm 4 data 1 data 2 2 1 6 4 8 9 Process 2 Process 2 Page Table 1 0 2 3 4 5 6 7 8 9 10 11 12 pgm 1 pgm 2 pgm 3 pgm 4 data 1 data 2 data 1 data 2 Physical memory
  • 21.
    Segmentation main program Sub 1 Sub 2 Library routines stack global data Auser’s program can be thought of as a collection of separate segments Segmentation Hardware STBR s d limit base s < + physical address logical address yes no addressing error
  • 22.
    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.
  • 23.
    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.