Chapter3 Memory
Chapter3 Memory
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 1
Email: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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 20
Email: tien.phamvan1@hust.edu.vn 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 21
Email: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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 23
Email: tien.phamvan1@hust.edu.vn 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 24
Email: tien.phamvan1@hust.edu.vn 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 25
Email: tien.phamvan1@hust.edu.vn 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 26
Email: tien.phamvan1@hust.edu.vn 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 27
Email: tien.phamvan1@hust.edu.vn 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 28
Email: tien.phamvan1@hust.edu.vn 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 29
Email: tien.phamvan1@hust.edu.vn 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 30
Email: tien.phamvan1@hust.edu.vn 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 31
Email: tien.phamvan1@hust.edu.vn 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 32
Email: tien.phamvan1@hust.edu.vn 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 33
Email: tien.phamvan1@hust.edu.vn 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 35
Email: tien.phamvan1@hust.edu.vn 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 36
Email: tien.phamvan1@hust.edu.vn 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 37
Email: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn 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 39
Email: tien.phamvan1@hust.edu.vn 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: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596