CNG334: Introduction to Operating Systems
Assitant Prof Dr. Mariem Hmila
Email: mariem@[Link]
Middle East Technical University
North Cyprus Campus
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 1 / 40
LECTURE 7: Memory Management: Main Memory
1 An overview
2 Basic Concepts
3 Contiguous Memory Allocation
4 Non-Contiguous Memory Allocation
5 Conclusion
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 2 / 40
Memory Management
An overview
CPU can access directly: ONLY main memory and the built in register
Registers are accessed within one tick of the CPU clock → very fast memories
Main memory access can take many CPU clock ticks → causing a stall
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 3 / 40
Memory Management
An Overview
Remember memory is already partitioned:
Part for the user → divided into multiple parts for various user process.
Part for the OS
Multiprogramming → Memory Management
This includes:
Main memory → real memory
Virtual Memory
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 4 / 40
Memory Management
Basic Concepts
Memory allocation is done:
Static Allocation → before process execution
Dynamic Allocation → deferred until process execution (for
multiprogramming)
Logical and physical addresses:
Process allocation is not known at the compiler time load time
The processor at compile time generates logical addresses
At the time of execution, logical addresses → physical addresses
Mapping logical to physical addresses →Special hardware
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 5 / 40
Memory Management
Basic Concepts: Swapping 1/3
Swapping: A process can be swapped temporarily out of memory then brought
back.
Questions:
Why Swapping?
Where will this process reside?
Which process will be taken out?
Where in the memory will the process be brought back?
Figure 1: Swapping
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 6 / 40
Memory Management
Basic Concepts: Swapping 2/3
Which process will be taken out?
In round robin process-scheduling: if the process execute for the given
time slot yet has not finished.
In priority based scheduling: lower priority processes could be swapped
out
Blocked processes that are waiting for an I/O
Where in the memory will the process be brought back?
Swap in the process at same location.
Swap in anywhere in the memory where there is a space → relocation
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 7 / 40
Memory Management
Basic Concepts: Swapping 3/3
Are there any disadvantages for swapping?
Example:
A process of size 200MB needs to be swapped into the hard disk → no
space in the memory
A process of size 250MB is lying idle in memory → could be swapped out
Question: How much swap time is required to swap-in and swap-out the
process if:
average latency time of hard disk = 10ms
Transfer rate of hard disk = 60MB/s
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 8 / 40
Memory Management
Basic Concepts: Relocation 1/3
Static allocation is difficult,Why?
Relocation provides new location and translates all references into physical
address
Relocation is done according to current location in memory, How?
Base address and hardware support are both needed
Figure 2: Relocation with base and limit register
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 9 / 40
Memory Management
Basic Concepts: Relocation 2/3
Figure 3: Memory mapping using base and limit registers
Example:
A process is to be swapped-in at the location 20100 in memory. If logical
address generated by the process are 200, 345, 440, and 550.
Question: what are the corresponding physical logical addresses into
physical addresses?
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 10 / 40
Memory Management
Basic Concepts: Relocation 3/3
Figure 4: Memory Management Unit.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 11 / 40
Memory Management
Basic Concepts: Protecting and sharing
From Protection point of view:
Base and limit registers → lower-bound and upper-bound register
Example:
a process has relocatable code of size 900K. The relocation register is
loaded with 40020K and the limit register contains the address 41000K. If
the processor generates a logical address 990, where will it be located in
physical memory?
From sharing: sharing is useful but still need to be with protection
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 12 / 40
Memory Management
Contiguous Memory Allocation: fixed partitioning 1/4
Fixed partition: A method of memory partition at the time of system generation.
Example: How long term scheduler work?
Figure 5: fixed equal sized partitioning based contiguous memory allocation.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 13 / 40
Memory Management
Contiguous Memory Allocation: fixed partitioning 2/4
Example: How long term scheduler work (conti...)?
Apply a memory allocation technique.
Enter partition ID in PCB, them move the process to ready queue
Memory manager marks the stats of the partition as "allocated"
Figure 6: Partition description table
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 14 / 40
Memory Management
Contiguous Memory Allocation: fixed partitioning 3/4
Example:
Three processes P1, P2, P3
The sizes of these processes are
21900, 21950, 21990 bytes
respectively to be allocated
Question: if equal-sized
partition of 22000 bytes are
allocated will there be any
fragmentation?
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 15 / 40
Memory Management
Contiguous Memory Allocation: fixed partitioning 4/4
Fixed partitioning can be with:
unequal sized partitions can be
used.
Equal sized partitions as shown
earlier.
Question: What are the
disadvantages of fixed partitioning?
External Fragmentation:
enough total memory space
available but spaces are not
contiguous.
Internal Fragmentation: big
blocks assigned to processes,
more than their needs. Figure 7: Fixed unequal-sized
partitioning-based contiguous memory
allocation
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 16 / 40
Memory Management
Contiguous Memory Allocation: Dynamic/variable partitioning 1/4
Dynamic Allocation: the number and the size of partitions are not fixed at the
time of system generation.
Example:
Consider three processes P1, P2, P3
Initially: 120M is available in the memory
P1 consumes 30M, P2 and P3 are allocated 40M and 48 of memory
Explanation in Figures next slides.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 17 / 40
Memory Management
Contiguous Memory Allocation: Dynamic/variable partitioning 2/4
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 18 / 40
Memory Management
Contiguous Memory Allocation: Dynamic/variable partitioning 3/4
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 19 / 40
Memory Management
Contiguous Memory Allocation: Dynamic/variable partitioning 4/4
External Fragmentation occurs in
figure 7 (a)
No process can be allocated in 6M+
2M+2M (wast of memory) = 10M
Compaction: merge free memories
spaces together so 10M can be
obtained contiguously
Problem: compaction requires
relocation that is time consuming
Figure 8: Compaction
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 20 / 40
Memory Management
Contiguous Memory Allocation: Memory Allocation techniques
Known as: Memory Allocation techniques or partition selection algorithms.
They decide which hole must be allocated to the process → dynamic partition or
unequal sized partition:
First-fit allocation
Best-fit allocation
Worst-fit allocation
Example:
Consider the memory allocation scenario as shown bellow. Allocate
memory for additional request of 4K and 10K, respectively using all the
previous strategies and compare between the resulted allocation in term of
internal fragmentation.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 21 / 40
Memory Management
Non-Contiguous Memory Allocation
A memory-management scheme where physical address space of a process to
can be noncontiguous.
Question 1: How does it work?
Breaking physical memory into fixed-sized blocks → frames
Breaking logical memory into blocks of the same size → pages
When a process is to be executed, its pages are loaded into any available
memory frames from their source.
Question 2: Advantages of paging?
Avoid the problem of external fragmentation and its solution compaction
Improve secondary storage while swapping where compact is impossible
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 22 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging Example 1/3
Assume that there are 10 free frames in the memory.
There are four processes: P1,P2, P3 and P4
The processes consist of (in order) 3, 4, 2 and 5 pages
Let’s see the illustration in figures
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 23 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging Example 2/3
Figure 9: Paging concept example 1/2
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 24 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging Example 3/3
Figure 10: Paging concept example 1/2
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 25 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging 1/3
Question: How to decide pages and
frames allocations?
Answer:
Page table is used to store the
base addresses of each page in
the process.
It indicated the frame location
of a page in the memory.
Figure 11: Paging model of logical and
physical memory.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 26 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging 2/3
An address generated by the CPU is
divided into two parts:
A page number (p) → index
into page table.
A page offset (d) →
displacement within the page.
Remember:
Page table is used to store the
base addresses of each page in
the process.
It indicated the frame location
of a page in the memory.
Figure 12: Paging example for a 32-byte
memory with 4-byte pages.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 27 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging 3/3
An address generated by the CPU is
divided into two parts:
A page number (p) → index
into page table.
A page offset (d) →
displacement within the page.
The base address (f), correspond the
page number, is retrieved.
Figure 13: Address translation in paging
f is added to the offset d to get the
corresponding physical address.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 28 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging and Hardware Support
Methods for storing page tables differ among operating systems.
Question: What are the options?
Implemented as a set of dedicated registers
Kept in main memory, and a page-table base register (PTBR) points to the
page table.
Use a hardware cache, called a translation look-aside buffer (TLB)
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 29 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging and Hardware Support TLB
The TLB is associative, high-speed memory.
Each entry in it consists of: a key (or tag) and a value.
Figure 14: Paging hardware with TLB.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 30 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging and shared pages 1/2
Shared pages help with common code sharing that is useful in time-sharing
environment.
Example: consider a system that supports 40 users, each executes a text editor.
if the code is not shared then:
assuming a text editor consists of 150 KB of code and 50 KB of data space
→ (150 * 40) + (50* 40) = 6000 + 2000 = 8000 KB to support those users.
Idea: a pure or reentrant code can be shared among different processes, while
each has its own copy of register data and storage data.
Share code lead to: 150 + (50*40) = 150 + 200 = 2150 KB to support 40
users.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 31 / 40
Memory Management
Non-Contiguous Memory Allocation: Paging and shared pages 2/2
Figure 15: Sharing of code in a paging environment.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 32 / 40
Memory Management
Non-Contiguous Memory Allocation: Page Table and Hierarchical Paging 1/2
Modern computer support a large logical address space → page table might be
large
Idea: multilevel Paging, means two level paging algorithm where page table
itself is also paged.
Remember:
Logical address: Page number , page offset
In multipaging: Page number 1 ( index of outer page table), Page number 2
(displacement within the page of the outer page table), page offset
This is known as forward-mapped page table.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 33 / 40
Memory Management
Non-Contiguous Memory Allocation: Page Table and Hierarchical Paging 2/2
Figure 16: A two-level page-table scheme.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 34 / 40
Memory Management
Non-Contiguous Memory Allocation: Segmentation 1/2
Paging → separation of the user’s view of memory from physical memory
Segmentation → supports the user’s view.
Figure 17: User’s view of a program.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 35 / 40
Memory Management
Non-Contiguous Memory Allocation: Segmentation 2/2
A logical address space is a collection of segments each has name and a length
The address specifies by the user involves segment name and the offset within
the segment
For simplicity: segments are numbered, consists of a two tuple:
(segment number, offset) → logical address
A C compiler might create separate segments for the following:
The code
Global variables
The heap, from which memory is allocated
The stacks used by each thread
The standard C library
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 36 / 40
Memory Management
Non-Contiguous Memory Allocation: Segmentation Table
physical memory is one-dimensional sequence of bytes
Segment table maps user’s two dimensional reference into one
Each entry in the table has segment base and segment limit:
Segment base: the starting physical address where the segment resides in
the memory
Segment Limit: the length of the segment.
Example:
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 37 / 40
Memory Management
Non-Contiguous Memory Allocation: Segmentation Table Example
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 38 / 40
Memory Management
Non-Contiguous Memory Allocation: Segmentation Hardware Support
Remember:
(segment number (s), offset (d)) → logical address
Offset d must be between 0 and the segment limit (length of segment).
legal offset d + base = physical address
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 39 / 40
Conclusion
Memory-management algorithms include contiguous allocation, paging, and
segmentation comparable via:
Hardware support: A simple base register or a base–limit register pair is
sufficient, whereas paging and segmentation need mapping tables to define
the address map.
Swapping: can be added to any algorithm, which allows more processes to
be run than can be fit into memory at one time.
Relocation: One solution to the external-fragmentation problem is
compaction.
Sharing help increasing the multiprogramming level by running many
processes with a limited amount of memory.
Asst Prof [Link] Hmila (METU NCC) CNG334: Introduction to Operating Systems 40 / 40