0% found this document useful (0 votes)
25 views65 pages

(OS'25) Lecture 2

Uploaded by

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

(OS'25) Lecture 2

Uploaded by

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

Operating Systems:

Internals and Design Principles, 6/E


William Stallings

Chapter 7
Memory Management

Dave Bremer
Otago Polytechnic, N.Z.
©2009, Prentice Hall
*Lecture slides are modified by Dr. Ahmed Salah*
GENERAL NOTE
Hidden Slides are Extra Knowledge
Not Included in Exams
Roadmap
KNOW HOW the Operating System do its Main Jobs?

Solve competition on…

Shared
Memory CPU Resourc
e

Physical Virtual Single Critical


Multi CPU Deadlocks
Mem Mem CPU Sections
(CH.10) (CH.6)
(CH.7) (CH.8) (CH.9) (CH.5)
Instruction Cycle with Interrupts
OS Objectives:
1. User/Comp. Interface
2. Resource manager
3. Evolution…
7-State Model
2) Memory Management
• OS has 5 principal responsibilities:
1. Process isolation
2. Automatic allocation and management
3. Support of modular programming
4. Protection and access control
5. Long-term storage (by file system module)
2) Memory Management
• ALL achieved by Virtual Memory concept
– allows programs to address memory from a logical point of view
– Without regard to the limits of RAM Virtual Mem
Process X
Process X Process Y
SHARING
2) Memory Management
Address Translation
CPU

MMU
Agenda
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic PART I
– Buddy System
– Paging
– Segmentation PART II
• Questions
Learning Outcomes

• Discuss the principal requirements for memory management.


• Understand the reason for memory partitioning and explain the
various techniques that are used.
• Understand the concept of paging.
• Assess the relative advantages of paging and segmentation.
References
• Chapter 7
– Section 7.1 (Basic Requirements)
– Section 7.2 (Fixed, Dynamic)
– Section 7.3 (Paging)

• Berkeley CS162
– L13: Address Translation (Paging & Segmentation)
Memory Management

Memory needs to be allocated to ensure a reasonable


supply of ready processes to consume available
processor time
Memory Management Requirements
Virtual Mem
Process X
1. Relocation
2. Protection Mod#5

Mod#4
3. Sharing Any Loc.

4. Logical organisation Mod#3

5. Physical organisation
Mod#2 RAM

Yellow prog.
Can’t access Mod#1
Red one
Requirements: Relocation
• The programmer does not know where the program will
be placed in memory when it is executed,
– It may be swapped to disk and return to main memory at a
different location (relocated)
• Memory references must be translated to the actual
physical memory address
Requirements: Protection
• Processes should not be able to reference memory
locations in another process without permission
• Impossible to check absolute addresses at compile time
• Must be checked at run time Virtual Mem
Process X

Yellow prog.
Can’t access
Red one
Requirements: Sharing
• Allow several processes to access the same portion of
memory
• Better to allow each process access to the same
copy of the program rather than have their own
separate copy
Process X Process Y
SHARING
Requirements: Logical Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled Virtual Mem
Process X
independently
Mod#5
• Different degrees of protection given to
Mod#4 Shared
modules (read-only, execute-only)
• Share modules among processes Mod#3

• Segmentation helps here


Mod#2

Mod#1
Requirements: Physical Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its data
may be insufficient
– Overlaying allows various modules to be assigned
the same region of memory but is time consuming
to program
• Programmer does not know how much space
will be available
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation
• Questions
Partitioning
• An early method of managing memory
– Not used much now
• Considered as basic for virtual memory
Types of Partitioning
1. Fixed Partitioning
2. Dynamic Partitioning
3. Buddy System CH.7
4. Simple Paging
5. Simple Segmentation
6. Virtual Memory Paging
CH.8
7. Virtual Memory Segmentation
Fixed Partitioning
• Equal-size partitions
– Any process whose size is less than or
equal to the partition size can be loaded
into an available partition
• OS can swap a process out of a partition
– If none are in a ready or running state
Example

Request 4
2
7 MB: Process A
B
C A

Internal Fragmentations
WHAT are the problems? B
(Not used – Can’t be Used)
C
Process > Partition!
Request 12 MB: Process D 7 MB
12 MB
5 MB
Can’t be loaded!!
Programmer should split it
into 2 parts (Overlaying)
ONLY one part loaded at a time
Fixed Partitioning
• Advantages:
1. Simple to implement
2. Little OS overheads
• Disadvantages:
1. A program may not fit in a partition.
• Solution: overlays
2. Main memory use is inefficient.
• Internal fragmentation: Any program, no matter how small, occupies an
entire partition.
Fixed Partitioning
• Disadvantages (cont.)
3. The number of active processes is fixed (limited by number of
partitions)
4. A large number of very small process will not use the space
efficiently
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation
• Questions
Dynamic Partitioning

• Partitions are of variable length and number


• Process is allocated exactly as much memory as required
Example
OS (8M)
• PROBLEM:
External Fragmentation
P2
P1
(14M)
(20M)
Memory external to all processes is
Empty (6M) fragmented
Empty
P4(8M)
P2 (Not Used – Can be Used)
(56M)
(14M)
Empty (6M)
• SOLUTION:
P3 Compaction
(18M)
– OS moves processes so that they are
Empty (16M)
contiguous
Empty (4M)
– Time consuming and wastes CPU time
Refer to Figure 7.4
Dynamic Partitioning
• Advantages:
– No internal fragmentation
– Efficient use of RAM
– No limits on number of active processes
• Disadvantages:
– External fragmentation
– Inefficient use of CPU due to compaction needs
Placement Example
First
Best Fit

Best Fit

Request 16 MB Next Fit


Dynamic Partitioning
Placement Algorithms:
• OS must decide which free block to allocate to a process
• Best-fit algorithm
– Chooses the block that is closest in size to the request
– Worst performer overall
– Since smallest block is found for process, the smallest amount of
fragmentation is left
– Memory compaction must be done more often
Dynamic Partitioning
Placement Algorithms:
• First-fit algorithm
– Scans memory form the beginning and chooses the first available
block that is large enough
– Fastest
– May have many process loaded in the front end of memory that
must be searched over when trying to find a free block
Dynamic Partitioning
Placement Algorithms:
• Next-fit
– Scans memory from the location of the last placement
– More often allocate a block of memory at the end of memory where
the largest block is found
– The largest block of memory is broken up into smaller blocks
– Compaction is required to obtain a large block at the end of
memory
Placement Comparison
Best-Fit First-Fit Next-Fit
Speed Slowest Fastest Fast
Fragments Large number of Fragment the blocks Separated overall mem
scattered small near the beginning Usually appear at the end of
fragments of the memory memory space. The largest
block of free memory, which
usually appears at the end of
the memory space, is quickly
broken up into small fragments.
Memory must be done more may be required less may be required more
compaction frequently than frequently than frequently than first-fit.
needs with other alg’s. other algorithms.
Example (Midterm 2019)
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic Address Translation
– Buddy System
– Paging
– Segmentation
• Questions
Address Translation
• When program loaded into memory the actual (absolute)
memory locations are determined

• A process may occupy different partitions which means


different absolute memory locations during execution
– Swapping
– Compaction
Address Translation
Fixed Partition: What’s the required info to be stored in table?
Virtual/Logical RAM
N-1
Process A
N-2

.
.
Process B
.

2
Process C
1
.
. 0
.
Address Translation
Dynamic Partition: What’s the required info to be stored in table?
Virtual/Logical RAM

Process A

Process B

Process C

.
.
.
Address Translation
Address from process

Start Address

OS Role?
Limit

MMU Role?
Address Translation (H/W)
1. absolute address = relative + base
2. If absolute address < bounds
– Access physical memory
3. Else
– Generate interrupt to OS
Address Translation (OS)
• Base register
– Starting address for the process
• Bounds register
– Ending location of the process
• These values are set by OS when the process is loaded
or when the process is swapped in
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation Address Translation
• Questions
Paging
Paging: What’s the required info to be stored in table?
Logical of Process A RAM

Page#M-1 N-1

. N-2
.
. A
.
Process
.
Page#1 .

Page#0 2

PAGES 1

FRAMES
Paging
• Extension of fixed partitioning

• Partition memory into small equal fixed-size chunks and


divide each process into the same size chunks

• The chunks of a process are called pages


• The chunks of memory are called frames
Paging
• OS maintains:
1. page table for each process
• Contains the frame location for each page in process

2. Free Frame List


• A list of currently free frames in RAM
4
5
Example 6

A.0
A.1
A.2
A.3
D.0
B.0
D.1
B.1
D.2
B.2
C.0
C.1
C.2
C.3
D.3
D.4
Paging
• Advantages:
– No external fragmentation
– Reduce internal fragmentation
• Disadvantages
– Side-effect on the modular programming
Address Translation (Ex)
Logical of Process A Logical Address RAM
P# (3 bits) Offset (11 bits)
Page#7 2 KB 15
Process A: Page Table
. 2 KB 14
. Page# Frame #
. 0 14 .
.
1 2
Page#1 .
.
Page#0 .
. 2 KB 2

PAGES 7 0
2 KB 1
Logical Address Physical Address
2 KB 0
0 KB 28 KB
MemoryPAGE#
address
= consist
2 KB 4 KB
1. page number FRAMES
3 KB OFFSET
2. offset =
within the page 5 KB
15 KB ?? KB
Address Translation
Address Translation (Ex.)
Given:
• Page Size = 1 KB
• Logical Address = 1502 (0000,0101,1101,1110)2
• Page table of Process A:
Page # 0 1 2
Frame # 5 6 17

Required
• Calculate corresponding physical address?
Address Translation (Ex.)

Check Slide Notes


For Detailed
Explanation
Address Translation (Ex.)
Check Slide Notes
For Detailed
Explanation
Sum-Up Comparison
Fixed Dynamic Buddy Paging Segment.
Program As a whole & As a whole & As a whole &
Loading contiguous contiguous contiguous
Yes (Base & Yes (Base & Yes (Base &
Relocation
Limit Reg’s) Limit Reg’s) Limit Reg’s)
Protection
Yes Yes Yes
(overall)
Protection
No No No
(modules)
Sharing No No No
Logical Org. Yes Yes Yes
Physical Org Yes Yes Yes
(-) Internal
Internal External
Fragment. (minimized)
(-) Overlay/
Yes/No No/Yes No/No
Compaction
(-) Fixed #
Yes No No
Process’s
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation Address Translation
• Questions
Program
Sum-Up
Fixed Dynamic
Comparison
Buddy Paging
As a whole & As a whole & As a whole & As a whole & NOT
Segment.
As a whole &
Loading contiguous contiguous contiguous contiguous NOT contiguous
Yes (Base & Yes (Base & Yes (Base &
Relocation Yes (Table) Yes (Table)
Limit Reg’s) Limit Reg’s) Limit Reg’s)
Protection
Yes Yes Yes Yes Yes
(overall)
Protection Yes (need cooper.
No No No Yes
(modules) bet. OS & Prog)
Yes (need cooper.
Sharing No No No Yes
bet. OS & Prog)
Logical Org. Yes Yes Yes Yes Yes
Physical Org Yes Yes Yes Yes Yes
(-) Internal Internal (very
Internal External External
Fragment. (minimized) small)
(-) Overlay/
Yes/No No/Yes No/No No/No No/Yes
Compaction
(-) Fixed #
Yes No No No No
Process’s
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation
• Questions

You might also like