Chapter3_Memory
Chapter3_Memory
Chapter3_Memory
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 1
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Concept
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 2
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management Requirements
• Relocation
– Programmer does not know where the
program will be placed in memory
when it is executed
– While the program is executing, it may
be swapped to disk and returned to
main memory at a different location
(relocated)
– Memory references must be translated
in the code to actual physical memory
address
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 3
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 4
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management 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
– Memory protection requirement must be satisfied by
the processor (hardware) rather than the operating
system (software)
• Operating system cannot anticipate all of the memory
references a program will make
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 5
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management 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
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 6
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management Requirements
• Logical Organization
– Programs are written in modules
– Modules can be written and compiled independently
– Different degrees of protection given to modules (read-
only, execute-only)
– Share modules among processes
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 7
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
• Operating Systems 2 - Memory Manager -
YouTube
https://www.youtube.com/watch?v=qdkxXygc3rE&t=
195s
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 8
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management Requirements
• Physical Organization
– Memory available for a program plus its data may
be insufficient
• Overlaying allows various modules to be assigned the
same region of memory
– Programmer does not know how much space will
be available
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 9
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Overview clip
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 10
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
MMU
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 11
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Fixed Partitioning
• Equal-size partitions
– Because all partitions are of equal size, it does not matter
which partition is used
– Main memory use is inefficient. Any program, no matter
how small, occupies an entire partition. This is called
internal fragmentation.
• Unequal-size partitions
– Can assign each process to the smallest partition within
which it will fit
– Queue for each partition
– Processes are assigned in such a way as to minimize
wasted memory within a partition
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 12
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 13
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Dynamic Partitioning
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 14
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 15
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Dynamic Partitioning Placement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 16
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Dynamic Partitioning Placement Algorithm
• 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
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 17
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Dynamic Partitioning Placement Algorithm
• 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
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 18
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 19
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
• https://www.youtube.com/watch?v=ue
V1gcYJSZg
20
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Buddy System
• Entire space available is treated as a single block of
2U
• If a request of size s such that 2U-1 < s <= 2U, entire
block is allocated
– Otherwise block is split into two equal buddies
– Process continues until smallest block greater than or
equal to s is generated
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 21
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 22
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 23
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Addresses
• Logical
– Reference to a memory location independent of the
current assignment of data to memory
– Translation must be made to the physical address
• Relative
– Address expressed as a location relative to some known
point
• Physical
– The absolute address or actual location in main memory
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 24
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 25
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Paging
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 26
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page Size (1)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 27
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page Size (2)
• Overhead due to page table and internal
fragmentation
se p
overhead = + internal
p 2 fragmentation
• Where
– s = average process size in bytes
Optimized when
– p = page size in bytes
p = 2se
– e = page entry
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 28
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page size (3)
Smallest
Architecture page Larger page sizes
size
2 MiB, 1 GiB (only when the CPU
x86-64[18] 4 KiB
has PDPE1GB flag)
Power ISA[21] 4 KiB 64 KiB, 16 MiB, 16 GiB
SPARC v8 with SPARC
4 KiB 256 KiB, 16 MiB
Reference MMU[22]
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 29
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page size (4)
#include <stdio.h>
#include <unistd.h> /* sysconf(3) */
int main(void)
{
printf("The page size for this system is %ld bytes.\n",
sysconf(_SC_PAGESIZE)); /* _SC_PAGE_SIZE is OK too. */
return 0;
}
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 30
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 31
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Virtual Memory
• https://www.youtube.com/watch?v=AKGtJAi
4wGo&ab_channel=Xoviabcs
https://www.youtube.com/watch?v=AKGtJAi4wGo
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 32
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 33
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Virtual Memory Paging
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 34
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Paging and translation
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 35
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Internal Operation of MMU with 16 4 KB Pages*
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 37
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 38
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 39
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 40
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Hardware Support
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 41
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 42
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Solutions to Large Page Table Problems
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 43
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Solutions to Large Page Table Problems (cont.)
Multilevel page tables avoid keeping one huge page table in memory
all the time: this works because most processes use only a few of its
pages frequently and the rest, seldom if at all. Scheme: the page
table itself is paged.
Example: Using 32 bit addressing:
The top-level table contains 1,024 pages (indices). The entry at each
index contains the page frame number of a 2nd-level page table. This
index (or page number) is found in the 10 highest (leftmost) bits in the
virtual address generated by the CPU.
The next 10 bits in the address hold the index into the 2nd-level page
table. This location holds the page frame number of the page itself.
The lowest 12 bits of the address is the offset, as usual.
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 44
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Two-level Page Tables
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 48
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management with Bit Maps
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 49
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory Management with Linked Lists
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 50
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page Replacement Algorithms
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 51
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Page Replacement Algorithms
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 52
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Optimal Page Replacement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 53
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Not Recently Used Page Replacement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 54
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
FIFO Page Replacement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 55
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Second Chance Page Replacement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 56
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
The Clock Page Replacement Algorithm
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 57
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Least Recently Used (LRU)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 58
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
LRU example
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 59
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Least-Frequently-Used (LFU) Page Replacement
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 60
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
The Working Set Page Replacement Algorithm(1)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 61
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Algorithm review
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 62
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
The Working Set Page Replacement Algorithm(2)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 65
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Review of Page Replacement Algorithms
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 66
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
References
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 67
Email: [email protected] C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596