SlideShare a Scribd company logo
OPERATING SYSTEMS
(CSC 2205)
Instructor: Mr. Khwaja Bilal Hassan
BS CS UET Peshawar
M.Phil. CS Quaid-e-Azam University Islamabad
Lecture#12
Memory Management
1
Agenda for Today
 What is memory management
 Source code to execution
 Address binding
 Logical and physical address spaces
 Dynamic loading, dynamic linking,
and overlays
Memory Hierarchy
 Very small, extremely fast, extremely
expensive, and volatile CPU registers
 Small, very fast, expensive, and volatile
cache
 Hundreds of megabytes of medium-
speed, medium-price, volatile main
memory
 Hundreds of gigabytes of slow, cheap,
and non-volatile secondary storage
Purpose of Memory
Management
To ensure fair, secure,
orderly, and efficient use
of memory
Memory Management
Keeping track of used and free
memory space
When, where, and how much
memory to allocate and
deallocate
Swapping processes in and out
of main memory
Source to Execution
Compile/Assemble
↓
Link
↓
Load
↓
Execute
© Copyright Virtual University of Pakistan May 4, 2021
 Binding instructions and data
to memory addresses
 Compile time
 Load time
 Execution time
Address Binding
 Compile time: If you know at
compile time where the
process will reside in memory,
the absolute code can be
generated. Process must
reside in the same memory
region for it to execute
correctly.
Address Binding
 Load time: If the location of a
process in memory is not known
at compile time, then the
compiler must generate re-
locatable code. In this case the
final binding is delayed until load
time. Process can be loaded in
different memory regions.
Address Binding
Address Binding
 Execution time: If the process
can be moved during its
execution from one memory
region to another, then binding
must be delayed until run time.
Special hardware must be
available for this to work.
Logical and Physical
Addresses
 Logical address: An address
generated by the process/CPU;
refers to an instruction or data in
the process
 Physical address: An address for
a main memory location where
instruction or data resides
Logical and Physical
Address Spaces
 The set of all logical addresses
generated by a process comprises
its logical address space.
 The set of physical addresses
corresponding to these logical
addresses comprises the physical
address space for the process.
Logical and Physical
Address Spaces
 The run-time mapping from
logical to physical addresses is
done by a piece of the CPU
hardware, called the memory
management unit (MMU).
Example
 The base register is called the
relocation register.
 The value in the relocation
register is added to every
address generated by a user
process at the time it is sent to
memory.
Example
14000
Process
Example
 In i8086, the logical address of
the next instruction is specified by
the value of instruction pointer
(IP). The physical address for the
instruction is computed by shifting
the code segment register (CS)
left by four bits and adding IP to it.
Example
CPU
CS * 24
+
MMU
Logical
address
Physical
address
Example
 Logical address (16-bit)
IP = 0B10h
CS = D000h
 Physical address (20-bit)
CS * 24
+ IP = D0B10h
 Sizes of logical and physical
address spaces?
Dynamic Loading
With dynamic loading, a routine
is not loaded into the main
memory until it is called.
All routines are kept on the disk
in a re-locatable format.
The main program is loaded
into memory and is executed
Dynamic Loading
Advantages
Potentially less time needed to
load a program
Potentially less memory space
needed
Disadvantage
Run-time activity
Dynamic Linking
In static linking, system
language libraries are linked
at compile time and, like any
other object module, are
combined by the loader into
the binary image
Dynamic Linking
 In dynamic linking, linking is
postponed until run-time.
 A library call is replaced by a
piece of code, called stub,
which is used to locate
memory-resident library routine
Dynamic Linking
During execution of a process,
stub is replaced by the address
of the relevant library code and
the code is executed
If library code is not in memory,
it is loaded at this time
Dynamic Linking
Advantages
Potentially less time needed to
load a program
Potentially less memory space
needed
Less disk space needed to
store binaries
Dynamic Linking
Disadvantages
Time-consuming run-time activity,
resulting in slower program
execution
gcc compiler
Dynamic linking by default
-static option allows static
linking
Overlays
 Allow a process to be larger
than the amount of memory
allocated to it
 Keep in memory only those
instructions and data that are
needed at any given time
Overlays
 When other instructions are
needed, they are loaded into the
space occupied previously by
instructions that are no longer
needed
 Implemented by user
 Programming design of overlay
structure is complex and not
possible in all cases
Overlays Example
 2-Pass assembler/compiler
 Available main memory: 150k
 Code size: 200k
Pass 1 ……………….. 70k
Pass 2 ……………….. 80k
Common routines …... 30k
Symbol table ………… 20k
Overlays Example
SWAPPING
Swap out and swap in (or roll out
and roll in)
Major part of swap time is transfer
time; the total transfer time is
directly proportional to the amount
of memory swapped
Large context switch time
SWAPPING
Cost of Swapping
 Process size = 1 MB
 Transfer rate = 5 MB/sec
 Swap out time = 1/5 sec
= 200 ms
 Average latency = 8 ms
 Net swap out time = 208 ms
 Swap out + swap in = 416 ms
Issues with Swapping
 Quantum for RR scheduler
 Pending I/O for swapped out
process
 User space used for I/O
 Solutions
Don’t swap out processes with
pending I/O
Do I/O using kernel space
Contiguous Allocation
 Kernel space, user space
 A process is placed in a single
contiguous area in memory
 Base (re-location) and limit
registers are used to point to the
smallest memory address of a
process and its size, respectively.
Contiguous Allocation
Main
Memory
Process
MFT
 Multiprogramming with fixed
tasks (MFT)
 Memory is divided into several
fixed-size partitions.
 Each partition may contain
exactly one process/task.
MFT
 Boundaries for partitions are set
at boot time and are not
movable.
 An input queue per partition
 The degree of multiprogramming
is bound by the number of
partitions.
Partition 4
Partition 3
Partition 2
Partition 1
OS
MFT
100 K
300 K
200 K
150 K
Input
Queues
 Potential for wasted memory
space—an empty partition but
no process in the associated
queue
 Load-time address binding
MFT With Multiple Input Queues
 Single queue for all partitions
Search the queue for a
process when a partition
becomes empty
First-fit, best-fit, worst-fit
space allocation algorithms
MFT With Single Input Queue
Partition 4
Partition 3
Partition 2
Partition 1
OS
100 K
300 K
200 K
150 K
Input Queue
MFT With Single Input Queue
43
Process ID
Memory
Size
1 200
2 150
3 300
Starting
Address
Size
0 500
500 300
800 200
1000 100
1100 400
Processes
Example: First fit
Partitions
Process
ID
Memory
Size
Allocated
Partition
1 200 0
2 150 500
3 300 1100
Allocation
44
Process
ID
Memory
Size
Allocated
Partition
1 200 800
2 150 500
3 300 1100
Process ID
Memory
Size
1 200
2 150
3 300
Starting
Address
Size
0 500
500 300
800 200
1000 100
1100 400
Processes
Example: Best fit
Partitions
Allocation
45
Process
ID
Memory
Size
Allocated
Partition
1 200 800
2 150 0
3 300 500
Process ID
Memory
Size
1 200
2 150
3 300
Starting
Address
Size
0 500
500 300
800 200
1000 100
1100 400
Processes
Example: Worst Fit
Partitions
Allocation
 Internal fragmentation—
wasted space inside a fixed-
size memory region
 No sharing between
processes.
 Load-time address binding
with multiple input queues
MFT Issues

More Related Content

PPT
PPTX
HW29kkkkkkkkkkkkkkkkkkkmmmmkkmmkkk454.pptx
PPT
Memory management principles in operating systems
PPT
Chapter 8 - Main Memory
PDF
Operating Systems Part III-Memory Management
PPTX
Memory Managment(OS).pptx
PPT
Main memory os - prashant odhavani- 160920107003
PDF
CH08.pdf
HW29kkkkkkkkkkkkkkkkkkkmmmmkkmmkkk454.pptx
Memory management principles in operating systems
Chapter 8 - Main Memory
Operating Systems Part III-Memory Management
Memory Managment(OS).pptx
Main memory os - prashant odhavani- 160920107003
CH08.pdf

Similar to Week 12 Operating System Lectures lec 2.pptx (20)

PPTX
Memory Management
PPT
Bab 4
 
PPT
Operating System
PDF
CS6401 OPERATING SYSTEMS Unit 3
PPT
Chapter 9 OS
 
PPT
PPT
Ch9 OS
 
PDF
Cs8493 unit 3
PDF
Cs8493 unit 3
DOCX
Opetating System Memory management
PPTX
Unit 5Memory management.pptx
PPT
Memory+management
PPTX
Main Memory Management in Operating System
PDF
Unit iiios Storage Management
PPTX
Operating system memory management
PPT
Memory comp
PDF
Operating Systems - memory management
DOCX
Operating system
Memory Management
Bab 4
 
Operating System
CS6401 OPERATING SYSTEMS Unit 3
Chapter 9 OS
 
Ch9 OS
 
Cs8493 unit 3
Cs8493 unit 3
Opetating System Memory management
Unit 5Memory management.pptx
Memory+management
Main Memory Management in Operating System
Unit iiios Storage Management
Operating system memory management
Memory comp
Operating Systems - memory management
Operating system
Ad

More from MujtabaVlogs (19)

PDF
Lecture 2 The Environment and Corporate Culture.pdf
PDF
Lecture 3 Managing Ethics and Social Responsibility.pdf
PDF
Lecture 1 Introduction to Mangement and Management thinking.pdf
PDF
L06-07 Software Design Requirements Basics.pdf
PDF
L08-09-10 Use cases - Use case Diagram- Expanded Use Cases.pdf
PDF
L13 L14 Software System Sequence Diagram.pdf
PPTX
Chapter 6 - Operating System Deadlock.pptx
PPTX
Chapter 5 - Operating Synchronization.pptx
PPTX
Chapter 4 - Operating Systems Threads.pptx
PPTX
Chapter 3 - InterProcess Communication.pptx
PPTX
Chapter 3 - Operating System Scheduling.pptx
PPTX
Chapter 1 - Operating Systems Introduction.pptx
PPTX
Chapter 0 - Operating System Preliminaries.pptx
PPTX
Chapter 2 - Operating System Processes(1).pptx
PPT
Lec-4-2-Recurrence Relation-Iteration Method.ppt
PPTX
Network layer. IP Addressing Part 1.pptx
PDF
CS3001-Computer-Networks-Ch3-Chapter-3.pdf
PPTX
CS3001_Computer_Networks_Chapter_2_v8.1.pptx
PPTX
CS3001_Computer_Networks_Chapter_1_v8.1.pptx
Lecture 2 The Environment and Corporate Culture.pdf
Lecture 3 Managing Ethics and Social Responsibility.pdf
Lecture 1 Introduction to Mangement and Management thinking.pdf
L06-07 Software Design Requirements Basics.pdf
L08-09-10 Use cases - Use case Diagram- Expanded Use Cases.pdf
L13 L14 Software System Sequence Diagram.pdf
Chapter 6 - Operating System Deadlock.pptx
Chapter 5 - Operating Synchronization.pptx
Chapter 4 - Operating Systems Threads.pptx
Chapter 3 - InterProcess Communication.pptx
Chapter 3 - Operating System Scheduling.pptx
Chapter 1 - Operating Systems Introduction.pptx
Chapter 0 - Operating System Preliminaries.pptx
Chapter 2 - Operating System Processes(1).pptx
Lec-4-2-Recurrence Relation-Iteration Method.ppt
Network layer. IP Addressing Part 1.pptx
CS3001-Computer-Networks-Ch3-Chapter-3.pdf
CS3001_Computer_Networks_Chapter_2_v8.1.pptx
CS3001_Computer_Networks_Chapter_1_v8.1.pptx
Ad

Recently uploaded (20)

PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
Soil Improvement Techniques Note - Rabbi
PDF
737-MAX_SRG.pdf student reference guides
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Artificial Intelligence
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPT
Total quality management ppt for engineering students
Automation-in-Manufacturing-Chapter-Introduction.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Visual Aids for Exploratory Data Analysis.pdf
Nature of X-rays, X- Ray Equipment, Fluoroscopy
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Fundamentals of Mechanical Engineering.pptx
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Soil Improvement Techniques Note - Rabbi
737-MAX_SRG.pdf student reference guides
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Artificial Intelligence
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Exploratory_Data_Analysis_Fundamentals.pdf
Total quality management ppt for engineering students

Week 12 Operating System Lectures lec 2.pptx

  • 1. OPERATING SYSTEMS (CSC 2205) Instructor: Mr. Khwaja Bilal Hassan BS CS UET Peshawar M.Phil. CS Quaid-e-Azam University Islamabad Lecture#12 Memory Management 1
  • 2. Agenda for Today  What is memory management  Source code to execution  Address binding  Logical and physical address spaces  Dynamic loading, dynamic linking, and overlays
  • 3. Memory Hierarchy  Very small, extremely fast, extremely expensive, and volatile CPU registers  Small, very fast, expensive, and volatile cache  Hundreds of megabytes of medium- speed, medium-price, volatile main memory  Hundreds of gigabytes of slow, cheap, and non-volatile secondary storage
  • 4. Purpose of Memory Management To ensure fair, secure, orderly, and efficient use of memory
  • 5. Memory Management Keeping track of used and free memory space When, where, and how much memory to allocate and deallocate Swapping processes in and out of main memory
  • 7. © Copyright Virtual University of Pakistan May 4, 2021
  • 8.  Binding instructions and data to memory addresses  Compile time  Load time  Execution time Address Binding
  • 9.  Compile time: If you know at compile time where the process will reside in memory, the absolute code can be generated. Process must reside in the same memory region for it to execute correctly. Address Binding
  • 10.  Load time: If the location of a process in memory is not known at compile time, then the compiler must generate re- locatable code. In this case the final binding is delayed until load time. Process can be loaded in different memory regions. Address Binding
  • 11. Address Binding  Execution time: If the process can be moved during its execution from one memory region to another, then binding must be delayed until run time. Special hardware must be available for this to work.
  • 12. Logical and Physical Addresses  Logical address: An address generated by the process/CPU; refers to an instruction or data in the process  Physical address: An address for a main memory location where instruction or data resides
  • 13. Logical and Physical Address Spaces  The set of all logical addresses generated by a process comprises its logical address space.  The set of physical addresses corresponding to these logical addresses comprises the physical address space for the process.
  • 14. Logical and Physical Address Spaces  The run-time mapping from logical to physical addresses is done by a piece of the CPU hardware, called the memory management unit (MMU).
  • 15. Example  The base register is called the relocation register.  The value in the relocation register is added to every address generated by a user process at the time it is sent to memory.
  • 17. Example  In i8086, the logical address of the next instruction is specified by the value of instruction pointer (IP). The physical address for the instruction is computed by shifting the code segment register (CS) left by four bits and adding IP to it.
  • 19. Example  Logical address (16-bit) IP = 0B10h CS = D000h  Physical address (20-bit) CS * 24 + IP = D0B10h  Sizes of logical and physical address spaces?
  • 20. Dynamic Loading With dynamic loading, a routine is not loaded into the main memory until it is called. All routines are kept on the disk in a re-locatable format. The main program is loaded into memory and is executed
  • 21. Dynamic Loading Advantages Potentially less time needed to load a program Potentially less memory space needed Disadvantage Run-time activity
  • 22. Dynamic Linking In static linking, system language libraries are linked at compile time and, like any other object module, are combined by the loader into the binary image
  • 23. Dynamic Linking  In dynamic linking, linking is postponed until run-time.  A library call is replaced by a piece of code, called stub, which is used to locate memory-resident library routine
  • 24. Dynamic Linking During execution of a process, stub is replaced by the address of the relevant library code and the code is executed If library code is not in memory, it is loaded at this time
  • 25. Dynamic Linking Advantages Potentially less time needed to load a program Potentially less memory space needed Less disk space needed to store binaries
  • 26. Dynamic Linking Disadvantages Time-consuming run-time activity, resulting in slower program execution gcc compiler Dynamic linking by default -static option allows static linking
  • 27. Overlays  Allow a process to be larger than the amount of memory allocated to it  Keep in memory only those instructions and data that are needed at any given time
  • 28. Overlays  When other instructions are needed, they are loaded into the space occupied previously by instructions that are no longer needed  Implemented by user  Programming design of overlay structure is complex and not possible in all cases
  • 29. Overlays Example  2-Pass assembler/compiler  Available main memory: 150k  Code size: 200k Pass 1 ……………….. 70k Pass 2 ……………….. 80k Common routines …... 30k Symbol table ………… 20k
  • 31. SWAPPING Swap out and swap in (or roll out and roll in) Major part of swap time is transfer time; the total transfer time is directly proportional to the amount of memory swapped Large context switch time
  • 33. Cost of Swapping  Process size = 1 MB  Transfer rate = 5 MB/sec  Swap out time = 1/5 sec = 200 ms  Average latency = 8 ms  Net swap out time = 208 ms  Swap out + swap in = 416 ms
  • 34. Issues with Swapping  Quantum for RR scheduler  Pending I/O for swapped out process  User space used for I/O  Solutions Don’t swap out processes with pending I/O Do I/O using kernel space
  • 35. Contiguous Allocation  Kernel space, user space  A process is placed in a single contiguous area in memory  Base (re-location) and limit registers are used to point to the smallest memory address of a process and its size, respectively.
  • 37. MFT  Multiprogramming with fixed tasks (MFT)  Memory is divided into several fixed-size partitions.  Each partition may contain exactly one process/task.
  • 38. MFT  Boundaries for partitions are set at boot time and are not movable.  An input queue per partition  The degree of multiprogramming is bound by the number of partitions.
  • 39. Partition 4 Partition 3 Partition 2 Partition 1 OS MFT 100 K 300 K 200 K 150 K Input Queues
  • 40.  Potential for wasted memory space—an empty partition but no process in the associated queue  Load-time address binding MFT With Multiple Input Queues
  • 41.  Single queue for all partitions Search the queue for a process when a partition becomes empty First-fit, best-fit, worst-fit space allocation algorithms MFT With Single Input Queue
  • 42. Partition 4 Partition 3 Partition 2 Partition 1 OS 100 K 300 K 200 K 150 K Input Queue MFT With Single Input Queue
  • 43. 43 Process ID Memory Size 1 200 2 150 3 300 Starting Address Size 0 500 500 300 800 200 1000 100 1100 400 Processes Example: First fit Partitions Process ID Memory Size Allocated Partition 1 200 0 2 150 500 3 300 1100 Allocation
  • 44. 44 Process ID Memory Size Allocated Partition 1 200 800 2 150 500 3 300 1100 Process ID Memory Size 1 200 2 150 3 300 Starting Address Size 0 500 500 300 800 200 1000 100 1100 400 Processes Example: Best fit Partitions Allocation
  • 45. 45 Process ID Memory Size Allocated Partition 1 200 800 2 150 0 3 300 500 Process ID Memory Size 1 200 2 150 3 300 Starting Address Size 0 500 500 300 800 200 1000 100 1100 400 Processes Example: Worst Fit Partitions Allocation
  • 46.  Internal fragmentation— wasted space inside a fixed- size memory region  No sharing between processes.  Load-time address binding with multiple input queues MFT Issues