SlideShare a Scribd company logo
1
Outlin
e
• Memory management definition
• Binding of Instructions and Data to Memory
• Executable code
• Simple memory management schemes
• Swapping
• Logical vs. Physical Address Space
• Memory protection with dynamic relocation
(MMU)
• Contiguous memory allocation
• Segmentation
• Paging
• Segmentation with paging
• Memory caching
Memory
Management
• The CPU fetches instructions and data of
a program from memory;
• Therefore, both the program and its data must
reside in the main memory (RAM and ROM).
• Modern multiprogramming systems are
capable of storing more than one program,
together with the data they access, in the
main memory.
• A fundamental task of the memory
management component of an operating
system is to ensure safe execution of programs
by providing:
– Sharing of memory
– Memory protection
3
Memory
Management…
Issues in sharing memory
Transparency
• Several processes may co-exist, unaware of each other,
in the main memory and run regardless of the
number and location of processes.
Safety (or protection)
• Processes must not corrupt each other (nor the OS!)
Efficiency
• CPU utilization must be preserved and memory
must be fairly allocated.
Relocation
• Ability of a program to run in different memory
locations.
19
4
Memory Management:
Definitions
• Relocatable - Means that the program image can
reside anywhere in physical memory.
• Binding - Programs need real memory in which to
reside. When is the location of that real memory
determined?
– This is called mapping logical to physical addresses.
– This binding can be done at compile or run time.
• Compiler - If it’s known where the program will
reside, then absolute code is generated. Otherwise
compiler produces relocatable code.
• Load - Binds relocatable to physical address.
• Execution - The code can be moved around
during execution.
5
Creating an executable
code
• Before a program can be executed by the
CPU, it must go through several steps:
– Compiling (translating) - generates re-
locatable object code.
– Linking - combines the object code into a
single self-sufficient executable code.
– Loading - copies the executable code
into memory.
– Execution - dynamic memory
allocation.
Creating an
executable
code
Compiler
Program/source code
Preprocessor
C or C++ program with
macro substitutions
and file inclusions
Assembly code
Assembler
Relocatable object module
Other relocatable
object modules
or library
modules Linker
Executable code
Loader
Absolute machine code
Execution
code
data
Work space
Main
memory 23
7
Functions of a
linker
 Linker collects and puts together all the
required pieces to form the executable code.
 Issues:
• Relocation
Where to put pieces.
• Cross-reference
where to find pieces.
• Re-organization
new memory layout.
8
Functions of a
loader
 A loader places the executable code in main
memory starting at a pre-determined location
(base or start address).
 This can be done in several ways, depending
on hardware architecture:
 Absolute loading : always loads programs
into a designated memory location.
 Relocatable loading : allows loading programs
in different memory locations.
 Dynamic (run - time) loading : loads functions when
first called (if ever).
9
Binding of Instructions and Data to
Memory
• 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, other wise
relocatable
– Load time: Compiler must generate relocatable
code if memory location is not known at compile
time.
– 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
Multistep Processing of a User
Program
10
11
Simple management
schemes
 An important task of a memory
management system is to bring (load)
programs into main memory for execution.
 The following contiguous memory
allocation techniques were commonly
employed by earlier operating systems :
• Direct placement
• Overlays
• Partitioning
12
Direct
placement
 Memory allocation is
trivial.No
special relocation is needed,
 because the user programs
are always loaded (one at a
time) into the same memory
location (absolute loading ).
 The linker produces the
same loading address for
every user program.
 Examples: Early batch
monitors, MS-DOS
OS(drivers, buffers)
unused
User program
Operating system
0
ma
x
use
r
Overlay
s
 A program was
organized (by the
user) into a tree-
like structure of
object modules,
called overlays .
 The root overlay was
always loaded into
the memory,
 whereas the sub
trees were (re-
loaded as needed
by simply overlaying
existing code.)
0.0
1.0 2.0 3.0
1.1 1.2 2.1 2.2 2.3 2.3 2.3
1.1
1.0
0.0
OS
13
2.1
2.0
0.0
OS
Memory
snapshot
Overlay
tree
Partitionin
g
 A simple method to accommodate several
programs in memory at the same time (to support
multiprogramming) is partitioning.
 In this scheme, the memory is divided into a
number of contiguous regions, called partitions.
 Two forms of memory partitioning, depending on
when and how partitions are created (and
modified), are possible:
• Static partitioning
• Dynamic partitioning
 These techniques were used by the IBM
OS/360 operating system:
 MFT (Multiprogramming with Fixed Number of
Tasks) and
 MVT (Multiprogramming with Variable Number of
Tasks.)
14
Partitioning
…
Static partitioning
 Main memory is divided
into fixed number of (fixed
size) partitions during
system generation or
start-up.
 Programs are queued to
run in the smallest
available partition.
 An executable prepared to
run in one partition may
not be able to run in
another, without being re-
linked.
 This technique uses
absolute loading.
Small jobs
Average jobs
Larger jobs
Operatin
g
System
500
k
100
k
10 k
15
Main
memory
16
Partitioning
…
Dynamic
partitioning
• Any number of
programs can be
loaded to memory as
long as there is room
for each.
• When a program is
loaded (relocatable
loading ), it is allocated
memory in exact
amount as it needs.
• Also, the addresses in
the program are fixed
after loaded, so it
cannot move.
 The operating system
keeps track of each
partition (their size and
locations in the
memory.)
A
OS
B
A
OS
B
OS
K
OS
Partition allocation at different
times
Main
memory
Swappin
g
Operatin
g
system
 The basic idea of swapping is to treat main
memory as a ‘‘pre- emptable ’’ resource.
 A high-speed swapping device is used as the
backing storage of the pre-empted processes.
Swap-in
Swap-
out
Swapping
device
Memor
y
35
18
Swapping
…
 A process can be swapped temporarily out of memory
to a backing store, and then brought back into
memory for continued execution.
 Backing store – fast disk large enough to
accommodate copies of all memory images for all
users; must provide direct access to these memory
images.
 Roll out, roll in – swapping variant used for priority-
based scheduling algorithms.
 lower-priority process is swapped out so higher-priority
process can be loaded and executed.
 UNIX, Linux, and Windows.
Schematic View of
Swapping
19
Swapping
…
Processes
on
disk
Processes
in
memory
Proces
s
runnin
g
Swap-
out
 Swapping is a medium term scheduling
discipline
Swap-in dispatch
Suspen
d
SWAPPER
DISPACHER
 Swapping brings flexibility even to systems
with fixed partitions, because:
 “ if needed, the operating system can always make
room for high-priority jobs, no matter what!’ “
20
21
Swapping
…
 Swapping is a medium-term scheduling
method.
 The responsibilities of a swapper include:
• Selection of processes to swap out
 criteria: suspended/blocked state, low priority, time
spent in memory
• Selection of processes to swap in
 criteria: time spent on swapping device, priority
• Allocation and management of swap space
on a swapping device.
 Swap space can be:
 system wide
 dedicated
22
Logical vs. Physical Address
Space
 The concept of a logical address space that is bound to
a separate physical address space is central to proper
memory management.
 Logical address – generated by the CPU; also
referred to as virtual address.
 Physical address – address seen by the memory
unit.
 Logical and physical addresses are the same in
compile- time and load-time address-binding
schemes;
 logical (virtual) and physical addresses differ
in execution-time address-binding scheme.
23
Memory
Protection
 The other fundamental task of a memory
management system is to protect programs
sharing the memory from each other.
 This protection also covers the operating system
itself.
 Memory protection can be provided at either of the
two levels:
 Hardware:
 address translation
 Software:
 language dependent: strong typing
 language independent: software fault isolation
Dynamic
relocation
CP
U
MM
U
Memor
y
Program
(logical
or
virtual)
address 24
Hardwar
e
(physical
or real)
address
 With dynamic relocation, each program-
generated address (logical address) is
translated to hardware address (physical
address) at runtime for every reference, by a
hardware device known as the memory
management unit (MMU).
Address Translation
Two views of
memory
 Dynamic relocation leads to two different
views of main memory, called address
spaces.
0
SC
0
SB
0
SA
Operatin
g
system
PA starts
PA ends
PB starts
PB ends
PC starts
PC ends
Physical view
(physical address
space) 25
Logical
view
(logical address
space)
26
Memory-Management Unit
(MMU)
 Hardware device that maps virtual to
physical address.
 In MMU scheme, the value in the
relocation register is added to every
address generated by a user process at
the time it is sent to memory.
 The user program deals with logical
addresses; it never sees the real
physical addresses.
Dynamic relocation using a relocation/base
register
27
28
Contiguous
Allocation
 Main memory is divided into two partitions:
 Resident operating system, usually held in low
memory with interrupt vector.
 User processes then held in high memory.
 Single-partition allocation
 Relocation-register scheme used to protect
user processes from each other, and from
changing operating-system code and data.
 Relocation register contains value of smallest
physical address;
 Limit register contains range of logical addresses –
each logical address must be less than the limit register.
Hardware Support for Relocation and Limit
Registers for memory protection discussed
before
29
Contiguous Allocation
(Cont.)
 Multiple-partition allocation
 Hole – block of available memory; holes of various
size are scattered throughout memory.
 When a process arrives, it is allocated memory
from a hole large enough to accommodate it.
 Operating system maintains information about:
a) allocated partitions b) free partitions
(hole)
OS
process 5
process 8
process 2
OS
process 5
process 2
OS
process 5
process 2
OS
process 5
process 9
process 2
process 9
process 10
Main
Memory
48
31
Dynamic Storage-Allocation
Problem
 How to satisfy a request of size n from a list
of free holes.
 First-fit: Allocate the first hole that is big
enough.
 Best-fit: Allocate the smallest hole that is big
enough; must search entire list, unless ordered by
size. Produces the smallest leftover hole.
 Worst-fit: Allocate the largest hole; must also
search entire list. Produces the largest leftover
hole.
 First-fit and best-fit better than worst-fit in terms
of speed and storage utilization.
32
Fragmentatio
n
 Fragmentation refers to the unused memory
that the management system cannot allocate.
 Internal fragmentation
o Waste of memory within a partition, caused by the
difference between the size of a partition and the
process loaded.
o Severe in static (fixed) partitioning schemes.
 External fragmentation
 Waste of memory between partitions, caused by
scattered non-contiguous free space.
 Severe in dynamic (variable size) partitioning schemes.
 Compaction is a technique that is used to overcome
external fragmentation.
Gentle reminder: Base and bounds
relocation
 Each program is
loaded into a
contiguous region of
memory.
 This region appears to
be ‘ ‘private’ ’ and the
bounds register limits
the range of the logical
address of each
program.
 Hardware
implementation is
cheap and efficient:
 2 registers plus an
adder and a
comparator.
Logical Address
Registers
bounds
base
>
+
Physical Address
Fault!
51
34
Segmentatio
n
 The most important problem with base-and-
bounds relocation is that there is only one
segment for each process.
 Segmentation is a memory management scheme that
supports user’s view of memory.
 A segment is a region of contiguous memory.
 Segmentation generalizes the base-and-
bounds technique by allowing each process to
be split over several segments.
 A segment table holds the base and bounds of each
segment.
 Although the segments may be scattered in memory,
each segment is mapped to a contiguous region.
 Additional fields (Read/Write and Shared) in the
segment table adds protection and sharing capabilities
to segments.
35
Segmentation
…
 A program is a collection of
segments.
 A segment is a logical unit such as:
main
program,
procedure,
function,
method,
object,
local variables, global
variables, common block,
stack,
symbol table, arrays
36
Segmentation
Architecture
 Logical address consists of a two
tuple:
<segment-number, offset>,
 Segment table – maps two-dimensional
physical addresses; each table entry has:
 base – contains the starting physical address
where the segments reside in memory.
 limit – specifies the length of the segment.
 Segment-table base register (STBR) points to
the segment table’s location in memory.
 Segment-table length register (STLR) indicates
number of segments used by a program;
segment number s is legal if s < STLR.
User’s View of a
Program
37
Logical View of
Segmentation
1
3
2
4
1
4
2
38
3
user
space
physical memory
space
Relocation with
segmentation
Segment # Offset
Seg# Base Bounds R/W S
0 0X1000 0X0120 0 1
1 0X3000 0X0340 0 0
2 0X0000 0X0FFF 1 0
3 0X2000 0X0F00 0 1
4 0X4000 0X0520 1 0
+
Physical Address
Logical Address
>
Fault!
39
40
Segmentation
…
 When a process is created, a pointer to an empty
segment table is inserted into the process control
block.
 Table entries are filled as new segments are allocated for
the process.
 The segments are returned to the free segment pool
when the process terminates.
 Segmentation, as well as the base and bounds
approach, causes external fragmentation and
requires memory compaction.
 An advantage of the approach is that only a segment,
instead of a whole process, may be swapped to make
room for the (new) process.
41
Pagin
g
 Paging is a memory-management scheme that
permits the physical address space of a process to
be noncontigious.
 Divide physical memory into fixed-sized blocks
called frames (size is power of 2, between 512
bytes and 16 mbytes per page).
 Divide logical memory into blocks of same size
called
pages.
 Keep track of all free frames.
 To run a program of size n pages, need to find n
free frames and load program.
 Set up a page table to translate logical to
physical addresses.
 Problem: Internal fragmentation.
Paging
…
 Physical memory is divided into a number of fixed
size blocks, called frames.
 The logical memory is also divided into chunks of
the same size, called pages.
 The size of frame/page is determined by the
hardware and typically is some value between 512
bytes (VAX) and 16 megabytes (MIPS 10000)!
 A page table maps the base address of pages for
each frame in the main memory.
 The major goals of paging are:
 to make memory allocation and swapping easier and
 to reduce fragmentation.
 Paging also allows allocation of non-contiguous memory
(i.e.,
pages need not be
adjacent.)
62
43
Address Translation
Scheme
 Address generated by CPU is divided into:
 Page number (p) – used as an index into a page
table which contains base address of each page
in physical memory.
 Page offset (d) – combined with base address to
define the physical memory address that is
sent to the memory unit.
Address Translation Architecture
(HW) using paging
44
Paging
Example
45
Relocation with
paging
Logical Address
Page # Page
offset
Page Table
Page table entry (PTE)
Base address
1
2
3
4
5
Frame
#
Frame
offset
Physical Address
46
Paging - an
example
Logical
address
space
0
1
2
3
4
5
Page # - 2 Page offset -300
Page Table
8
3
1
6
0
0
1
2
3
4
0
1
2
3
4
5
6
7
Physical
Memory
Frame #-1 Offset-300
Logical
address
Physical
address
68
48
Paging
…
 Paging itself is a form of dynamic relocation.
 Every logical address is bound by the paging HW
to some physical address.
 Using paging is similar to using a table of base
(relocation) registers, one for each frame of
memory.
 When using paging scheme, there is no
external fragmentation.
 Any free frame can be allocated to a process
that needs it.
 However, we may have some internal
fragmentation.
49
Implementation of Page
Table
 Page table is kept in main memory.
 Page-table base register (PTBR) points to the
page table.
 Page-table length register (PRLR) indicates size of
the page table.
 In this scheme every data/instruction access
requires two memory accesses.
 One for the page table and
 one for the data/instruction.
 The two memory access problem can be solved by
the use of a special fast-lookup hardware cache
called associative memory or translation look-aside
buffers (TLBs)
50
Associative
memory
 Problem: Both paging and segmentation
schemes introduce extra memory references
to access translation tables.
 Solution? Translation buffers.
 Based on the notion of locality (at a given time a
Process is only using a few pages or segments) ,
a very fast but small associative (content
addressable) memory is used to store a few of
the translation table entries.
 This memory is known as a translation look-
aside buffer or TLB
51
Hardware support for
paging
 There are different hardware
implementations of page tables to support
paging.
 A set of dedicated registers, holding
base addresses of frames.
 In memory page table with a page table
base register (PTBR).
 Same as above with multi-level page
tables.
Memory
catching
 Similar to storing memory
addresses in TLBs,
 frequently used data in main
memory can also be stored in
fast buffers, called cache
memory, or simply cache.
 memory access occurs as
follows:
2n-1
for each memory reference
if data is not in cache
<miss> if cache is full
remove some data (make
space)
if read access
issue memory
read
place data in cache
return data
else <hit>
if read
access
return data
else
98
Cache
Memory…
 Memory access may
be slow
 Cache is small but
fast memory close
to processor
 Holds copy of part
of memory
 Hits and misses
Processor
Memory
Cache
Fast/expensive technology,
usually on the same chip
103
Slower/cheaper technology,
usually on a different chip

More Related Content

PPTX
Memory Management techniques -ch8_1.pptx
PDF
Cs8493 unit 3
PDF
CS6401 OPERATING SYSTEMS Unit 3
PDF
Memory Management(MM) in operating system
PDF
Cs8493 unit 3
PPTX
ch8 Memory Management OS.pptx
PDF
CH08.pdf
PDF
lecture 8 b main memory
Memory Management techniques -ch8_1.pptx
Cs8493 unit 3
CS6401 OPERATING SYSTEMS Unit 3
Memory Management(MM) in operating system
Cs8493 unit 3
ch8 Memory Management OS.pptx
CH08.pdf
lecture 8 b main memory

Similar to operating system deadlock management with example (20)

PDF
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
PPT
Memory management principles in operating systems
PPTX
CSE2010- Module 4 V1.pptx
PPT
Operating system ch#7
PDF
Memory management- Swapping Contiguous Memory Allocation
PDF
Memory Management slides of operating system
PPTX
M20CA1030_391_2_Part2.pptx
PDF
Memory Management.pdf
PPTX
Operating system memory management
PPT
Operating systems- Main Memory Management
PPTX
Lecture-7 Main Memroy.pptx
PDF
Ch8 main memory
PPTX
HW29kkkkkkkkkkkkkkkkkkkmmmmkkmmkkk454.pptx
PPTX
memorymanagementoperatingaystemsconvepys.pptx
PPTX
Memory Management in System Designing.pptx
PPTX
Memory Management
PPT
operationg systemsdocumentmemorymanagement
PPT
OS-unit-3 part -1mxmxmxmmxmxmmxmxmxmxmxmmxmxmmx.ppt
PPTX
Week 12 Operating System Lectures lec 2.pptx
PPT
Unit 5 Memory management System in OS.ppt
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
Memory management principles in operating systems
CSE2010- Module 4 V1.pptx
Operating system ch#7
Memory management- Swapping Contiguous Memory Allocation
Memory Management slides of operating system
M20CA1030_391_2_Part2.pptx
Memory Management.pdf
Operating system memory management
Operating systems- Main Memory Management
Lecture-7 Main Memroy.pptx
Ch8 main memory
HW29kkkkkkkkkkkkkkkkkkkmmmmkkmmkkk454.pptx
memorymanagementoperatingaystemsconvepys.pptx
Memory Management in System Designing.pptx
Memory Management
operationg systemsdocumentmemorymanagement
OS-unit-3 part -1mxmxmxmmxmxmmxmxmxmxmxmmxmxmmx.ppt
Week 12 Operating System Lectures lec 2.pptx
Unit 5 Memory management System in OS.ppt
Ad

Recently uploaded (20)

PPTX
Presentation on HIE in infants and its manifestations
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
RMMM.pdf make it easy to upload and study
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Classroom Observation Tools for Teachers
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
Presentation on HIE in infants and its manifestations
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
RMMM.pdf make it easy to upload and study
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Classroom Observation Tools for Teachers
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Ad

operating system deadlock management with example

  • 1. 1 Outlin e • Memory management definition • Binding of Instructions and Data to Memory • Executable code • Simple memory management schemes • Swapping • Logical vs. Physical Address Space • Memory protection with dynamic relocation (MMU) • Contiguous memory allocation • Segmentation • Paging • Segmentation with paging • Memory caching
  • 2. Memory Management • The CPU fetches instructions and data of a program from memory; • Therefore, both the program and its data must reside in the main memory (RAM and ROM). • Modern multiprogramming systems are capable of storing more than one program, together with the data they access, in the main memory. • A fundamental task of the memory management component of an operating system is to ensure safe execution of programs by providing: – Sharing of memory – Memory protection 3
  • 3. Memory Management… Issues in sharing memory Transparency • Several processes may co-exist, unaware of each other, in the main memory and run regardless of the number and location of processes. Safety (or protection) • Processes must not corrupt each other (nor the OS!) Efficiency • CPU utilization must be preserved and memory must be fairly allocated. Relocation • Ability of a program to run in different memory locations. 19
  • 4. 4 Memory Management: Definitions • Relocatable - Means that the program image can reside anywhere in physical memory. • Binding - Programs need real memory in which to reside. When is the location of that real memory determined? – This is called mapping logical to physical addresses. – This binding can be done at compile or run time. • Compiler - If it’s known where the program will reside, then absolute code is generated. Otherwise compiler produces relocatable code. • Load - Binds relocatable to physical address. • Execution - The code can be moved around during execution.
  • 5. 5 Creating an executable code • Before a program can be executed by the CPU, it must go through several steps: – Compiling (translating) - generates re- locatable object code. – Linking - combines the object code into a single self-sufficient executable code. – Loading - copies the executable code into memory. – Execution - dynamic memory allocation.
  • 6. Creating an executable code Compiler Program/source code Preprocessor C or C++ program with macro substitutions and file inclusions Assembly code Assembler Relocatable object module Other relocatable object modules or library modules Linker Executable code Loader Absolute machine code Execution code data Work space Main memory 23
  • 7. 7 Functions of a linker  Linker collects and puts together all the required pieces to form the executable code.  Issues: • Relocation Where to put pieces. • Cross-reference where to find pieces. • Re-organization new memory layout.
  • 8. 8 Functions of a loader  A loader places the executable code in main memory starting at a pre-determined location (base or start address).  This can be done in several ways, depending on hardware architecture:  Absolute loading : always loads programs into a designated memory location.  Relocatable loading : allows loading programs in different memory locations.  Dynamic (run - time) loading : loads functions when first called (if ever).
  • 9. 9 Binding of Instructions and Data to Memory • 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, other wise relocatable – Load time: Compiler must generate relocatable code if memory location is not known at compile time. – 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
  • 10. Multistep Processing of a User Program 10
  • 11. 11 Simple management schemes  An important task of a memory management system is to bring (load) programs into main memory for execution.  The following contiguous memory allocation techniques were commonly employed by earlier operating systems : • Direct placement • Overlays • Partitioning
  • 12. 12 Direct placement  Memory allocation is trivial.No special relocation is needed,  because the user programs are always loaded (one at a time) into the same memory location (absolute loading ).  The linker produces the same loading address for every user program.  Examples: Early batch monitors, MS-DOS OS(drivers, buffers) unused User program Operating system 0 ma x use r
  • 13. Overlay s  A program was organized (by the user) into a tree- like structure of object modules, called overlays .  The root overlay was always loaded into the memory,  whereas the sub trees were (re- loaded as needed by simply overlaying existing code.) 0.0 1.0 2.0 3.0 1.1 1.2 2.1 2.2 2.3 2.3 2.3 1.1 1.0 0.0 OS 13 2.1 2.0 0.0 OS Memory snapshot Overlay tree
  • 14. Partitionin g  A simple method to accommodate several programs in memory at the same time (to support multiprogramming) is partitioning.  In this scheme, the memory is divided into a number of contiguous regions, called partitions.  Two forms of memory partitioning, depending on when and how partitions are created (and modified), are possible: • Static partitioning • Dynamic partitioning  These techniques were used by the IBM OS/360 operating system:  MFT (Multiprogramming with Fixed Number of Tasks) and  MVT (Multiprogramming with Variable Number of Tasks.) 14
  • 15. Partitioning … Static partitioning  Main memory is divided into fixed number of (fixed size) partitions during system generation or start-up.  Programs are queued to run in the smallest available partition.  An executable prepared to run in one partition may not be able to run in another, without being re- linked.  This technique uses absolute loading. Small jobs Average jobs Larger jobs Operatin g System 500 k 100 k 10 k 15 Main memory
  • 16. 16 Partitioning … Dynamic partitioning • Any number of programs can be loaded to memory as long as there is room for each. • When a program is loaded (relocatable loading ), it is allocated memory in exact amount as it needs. • Also, the addresses in the program are fixed after loaded, so it cannot move.  The operating system keeps track of each partition (their size and locations in the memory.) A OS B A OS B OS K OS Partition allocation at different times Main memory
  • 17. Swappin g Operatin g system  The basic idea of swapping is to treat main memory as a ‘‘pre- emptable ’’ resource.  A high-speed swapping device is used as the backing storage of the pre-empted processes. Swap-in Swap- out Swapping device Memor y 35
  • 18. 18 Swapping …  A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution.  Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images.  Roll out, roll in – swapping variant used for priority- based scheduling algorithms.  lower-priority process is swapped out so higher-priority process can be loaded and executed.  UNIX, Linux, and Windows.
  • 20. Swapping … Processes on disk Processes in memory Proces s runnin g Swap- out  Swapping is a medium term scheduling discipline Swap-in dispatch Suspen d SWAPPER DISPACHER  Swapping brings flexibility even to systems with fixed partitions, because:  “ if needed, the operating system can always make room for high-priority jobs, no matter what!’ “ 20
  • 21. 21 Swapping …  Swapping is a medium-term scheduling method.  The responsibilities of a swapper include: • Selection of processes to swap out  criteria: suspended/blocked state, low priority, time spent in memory • Selection of processes to swap in  criteria: time spent on swapping device, priority • Allocation and management of swap space on a swapping device.  Swap space can be:  system wide  dedicated
  • 22. 22 Logical vs. Physical Address Space  The concept of a logical address space that is bound to a separate physical address space is central to proper memory management.  Logical address – generated by the CPU; also referred to as virtual address.  Physical address – address seen by the memory unit.  Logical and physical addresses are the same in compile- time and load-time address-binding schemes;  logical (virtual) and physical addresses differ in execution-time address-binding scheme.
  • 23. 23 Memory Protection  The other fundamental task of a memory management system is to protect programs sharing the memory from each other.  This protection also covers the operating system itself.  Memory protection can be provided at either of the two levels:  Hardware:  address translation  Software:  language dependent: strong typing  language independent: software fault isolation
  • 24. Dynamic relocation CP U MM U Memor y Program (logical or virtual) address 24 Hardwar e (physical or real) address  With dynamic relocation, each program- generated address (logical address) is translated to hardware address (physical address) at runtime for every reference, by a hardware device known as the memory management unit (MMU). Address Translation
  • 25. Two views of memory  Dynamic relocation leads to two different views of main memory, called address spaces. 0 SC 0 SB 0 SA Operatin g system PA starts PA ends PB starts PB ends PC starts PC ends Physical view (physical address space) 25 Logical view (logical address space)
  • 26. 26 Memory-Management Unit (MMU)  Hardware device that maps virtual to physical address.  In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory.  The user program deals with logical addresses; it never sees the real physical addresses.
  • 27. Dynamic relocation using a relocation/base register 27
  • 28. 28 Contiguous Allocation  Main memory is divided into two partitions:  Resident operating system, usually held in low memory with interrupt vector.  User processes then held in high memory.  Single-partition allocation  Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data.  Relocation register contains value of smallest physical address;  Limit register contains range of logical addresses – each logical address must be less than the limit register.
  • 29. Hardware Support for Relocation and Limit Registers for memory protection discussed before 29
  • 30. Contiguous Allocation (Cont.)  Multiple-partition allocation  Hole – block of available memory; holes of various size are scattered throughout memory.  When a process arrives, it is allocated memory from a hole large enough to accommodate it.  Operating system maintains information about: a) allocated partitions b) free partitions (hole) OS process 5 process 8 process 2 OS process 5 process 2 OS process 5 process 2 OS process 5 process 9 process 2 process 9 process 10 Main Memory 48
  • 31. 31 Dynamic Storage-Allocation Problem  How to satisfy a request of size n from a list of free holes.  First-fit: Allocate the first hole that is big enough.  Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole.  Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.  First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
  • 32. 32 Fragmentatio n  Fragmentation refers to the unused memory that the management system cannot allocate.  Internal fragmentation o Waste of memory within a partition, caused by the difference between the size of a partition and the process loaded. o Severe in static (fixed) partitioning schemes.  External fragmentation  Waste of memory between partitions, caused by scattered non-contiguous free space.  Severe in dynamic (variable size) partitioning schemes.  Compaction is a technique that is used to overcome external fragmentation.
  • 33. Gentle reminder: Base and bounds relocation  Each program is loaded into a contiguous region of memory.  This region appears to be ‘ ‘private’ ’ and the bounds register limits the range of the logical address of each program.  Hardware implementation is cheap and efficient:  2 registers plus an adder and a comparator. Logical Address Registers bounds base > + Physical Address Fault! 51
  • 34. 34 Segmentatio n  The most important problem with base-and- bounds relocation is that there is only one segment for each process.  Segmentation is a memory management scheme that supports user’s view of memory.  A segment is a region of contiguous memory.  Segmentation generalizes the base-and- bounds technique by allowing each process to be split over several segments.  A segment table holds the base and bounds of each segment.  Although the segments may be scattered in memory, each segment is mapped to a contiguous region.  Additional fields (Read/Write and Shared) in the segment table adds protection and sharing capabilities to segments.
  • 35. 35 Segmentation …  A program is a collection of segments.  A segment is a logical unit such as: main program, procedure, function, method, object, local variables, global variables, common block, stack, symbol table, arrays
  • 36. 36 Segmentation Architecture  Logical address consists of a two tuple: <segment-number, offset>,  Segment table – maps two-dimensional physical addresses; each table entry has:  base – contains the starting physical address where the segments reside in memory.  limit – specifies the length of the segment.  Segment-table base register (STBR) points to the segment table’s location in memory.  Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR.
  • 37. User’s View of a Program 37
  • 39. Relocation with segmentation Segment # Offset Seg# Base Bounds R/W S 0 0X1000 0X0120 0 1 1 0X3000 0X0340 0 0 2 0X0000 0X0FFF 1 0 3 0X2000 0X0F00 0 1 4 0X4000 0X0520 1 0 + Physical Address Logical Address > Fault! 39
  • 40. 40 Segmentation …  When a process is created, a pointer to an empty segment table is inserted into the process control block.  Table entries are filled as new segments are allocated for the process.  The segments are returned to the free segment pool when the process terminates.  Segmentation, as well as the base and bounds approach, causes external fragmentation and requires memory compaction.  An advantage of the approach is that only a segment, instead of a whole process, may be swapped to make room for the (new) process.
  • 41. 41 Pagin g  Paging is a memory-management scheme that permits the physical address space of a process to be noncontigious.  Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 16 mbytes per page).  Divide logical memory into blocks of same size called pages.  Keep track of all free frames.  To run a program of size n pages, need to find n free frames and load program.  Set up a page table to translate logical to physical addresses.  Problem: Internal fragmentation.
  • 42. Paging …  Physical memory is divided into a number of fixed size blocks, called frames.  The logical memory is also divided into chunks of the same size, called pages.  The size of frame/page is determined by the hardware and typically is some value between 512 bytes (VAX) and 16 megabytes (MIPS 10000)!  A page table maps the base address of pages for each frame in the main memory.  The major goals of paging are:  to make memory allocation and swapping easier and  to reduce fragmentation.  Paging also allows allocation of non-contiguous memory (i.e., pages need not be adjacent.) 62
  • 43. 43 Address Translation Scheme  Address generated by CPU is divided into:  Page number (p) – used as an index into a page table which contains base address of each page in physical memory.  Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit.
  • 46. Relocation with paging Logical Address Page # Page offset Page Table Page table entry (PTE) Base address 1 2 3 4 5 Frame # Frame offset Physical Address 46
  • 47. Paging - an example Logical address space 0 1 2 3 4 5 Page # - 2 Page offset -300 Page Table 8 3 1 6 0 0 1 2 3 4 0 1 2 3 4 5 6 7 Physical Memory Frame #-1 Offset-300 Logical address Physical address 68
  • 48. 48 Paging …  Paging itself is a form of dynamic relocation.  Every logical address is bound by the paging HW to some physical address.  Using paging is similar to using a table of base (relocation) registers, one for each frame of memory.  When using paging scheme, there is no external fragmentation.  Any free frame can be allocated to a process that needs it.  However, we may have some internal fragmentation.
  • 49. 49 Implementation of Page Table  Page table is kept in main memory.  Page-table base register (PTBR) points to the page table.  Page-table length register (PRLR) indicates size of the page table.  In this scheme every data/instruction access requires two memory accesses.  One for the page table and  one for the data/instruction.  The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs)
  • 50. 50 Associative memory  Problem: Both paging and segmentation schemes introduce extra memory references to access translation tables.  Solution? Translation buffers.  Based on the notion of locality (at a given time a Process is only using a few pages or segments) , a very fast but small associative (content addressable) memory is used to store a few of the translation table entries.  This memory is known as a translation look- aside buffer or TLB
  • 51. 51 Hardware support for paging  There are different hardware implementations of page tables to support paging.  A set of dedicated registers, holding base addresses of frames.  In memory page table with a page table base register (PTBR).  Same as above with multi-level page tables.
  • 52. Memory catching  Similar to storing memory addresses in TLBs,  frequently used data in main memory can also be stored in fast buffers, called cache memory, or simply cache.  memory access occurs as follows: 2n-1 for each memory reference if data is not in cache <miss> if cache is full remove some data (make space) if read access issue memory read place data in cache return data else <hit> if read access return data else 98
  • 53. Cache Memory…  Memory access may be slow  Cache is small but fast memory close to processor  Holds copy of part of memory  Hits and misses Processor Memory Cache Fast/expensive technology, usually on the same chip 103 Slower/cheaper technology, usually on a different chip