csnb334 advanced operating systems 5. memory management
TRANSCRIPT
![Page 1: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/1.jpg)
CSNB334 Advanced Operating Systems5. Memory Management
![Page 2: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/2.jpg)
Review of basic concepts
What are the requirements of memory management? Relocation
For managing the available memory in a multiprogramming environment.
Protection Must be satisfied by the hardware (processor) rather than the
OS. Permissibility of a memory reference by an instruction can only be
checked at the time of execution of the instruction. Sharing Physical Organization
Moving information between the main memory and secondary memory.
![Page 3: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/3.jpg)
Relocation
Phenomenon by which a process may occupy different partitions during the course of its life.
Three types of addresses Logical Address
Reference to a memory location independent of the current assignment of the process to memory.
Relative Address Type of logical address – address is expressed as a location
relative to some known point. Physical Address/ Absolute Addres
Actual location in main memory.
![Page 4: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/4.jpg)
Loading
Absolute Loading Decision of where to load a module in the memory is made at
compile time. Thus, a given module is always loaded into a specific location in main
memory. Relocatable Loading.
Decision is made at load time. Thus a module can be loaded anywhere in the main memory. But, is swapped back to the same memory.
Dynamic Run-Time loading Decision is made at run-time.
Therefore, we can swap a process image into different locations at different times.
Done by special processor hardware rather than software. Base Register, Bounds Register, Adder, Comparator.
![Page 5: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/5.jpg)
Memory Management Techniques Fixed partitioning
Main memory is divided into a number of static partitions at system generation time. Equal-size partitions Unequal-size partitions
Pros Simple to implement; little OS overhead
Cons Inefficient : internal fragmentation Maximum number of active processes – fixed.
Placement Algorithm One process queue per partition. Single queue.
![Page 6: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/6.jpg)
![Page 7: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/7.jpg)
Fixed partitioning Placement Algorithm
![Page 8: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/8.jpg)
Memory Management Techniques Dynamic partitioning
Partitions are created dynamically Each process is loaded into a partition of exactly the same size as
that process. Pros
No internal fragmentation. Cons
External fragmentation. Counteract : by compaction.
But an overhead for the processor. Placement Algorithm : (because compaction is time consuming)
Best-fit First-fit. Next-fit.
![Page 9: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/9.jpg)
Dynamic partitioning
![Page 10: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/10.jpg)
Dynamic partitioning
![Page 11: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/11.jpg)
Memory Management Techniques Paging
Main memory is divided into a number of equal-sized, relatively small frames.
Each process is divided into a number of equal-sized pages – same length as a frame.
A process is loaded by loading all of its pages into available frames. Not necessarily be contiguous.
Possible thru the use of a page table for each process. Logical address (page number, offset) --- Physical Address (frame
number, offset). Pros
No external fragmentation Cons
A small amount of internal fragmentation.
![Page 12: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/12.jpg)
Address Translation in a Paging System
Program Paging Main Memory
Logical address
Register
Page Table
PageFrame
Offset
P#
Frame #
Page Table Ptr
Page # Offset Frame # Offset
+
![Page 13: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/13.jpg)
Memory Management Techniques Segmentation
Each process is divided into number of segments. Need not be of same size.
A process is loaded by loading all of its segments into dynamic partitions. Need not be contiguous
Use segment table. Difference with dynamic partitioning
A process may occupy more than one partition. Partitions need not be contiguous.
Pros No internal fragmentation.
Cons External fragmentation : though less severe than dynamic partitioning
because of the small size of the segments.
![Page 14: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/14.jpg)
Hardware support
Base + d
Program Segmentation Main Memory
Virtual Address
Register
Segment Table
Seg
men
t
d
S#
Length Base
Seg Table Ptr
Seg # Offset = d
Segment Table
+
+
![Page 15: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/15.jpg)
Memory Management Techniques Virtual memory
Similar to paging/segmentation except that it is not necessary to load all of the segments/pages of a process into main memory. Nonresident pages/segments that are needed are brought in later
automatically. May require writing a page/segment out to disk if the memory is full.
Pros: Large virtual address space.
More processes may be maintained in main memory. A process may be larger than all of main memory.
Cons Overhead of complex memory management.
Thrashing : The system spends most of its time swapping pieces rather than executing instructions.
![Page 16: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/16.jpg)
Translation Lookaside Buffer
Every virtual memory reference causes two physical memory access: To fetch the appropriate page table entry. To fetch the desired data.
Thus, the memory access time is doubled. To overcome this: Use a special high-speed cache for page table
entries – Translation Lookaside Buffer Similar to a memory cache. Contains those page table entries – most recently used.
![Page 17: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/17.jpg)
Use of a Translation Lookaside Buffer
![Page 18: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/18.jpg)
Virtual Memory
Virtual memory can be based on Paging only
Virtual Address : Page Number + Offset Page table entry : P(bit)+M(bit)+ Frame Number
Segmentation Virtual Address : Segment Number + Offset Segment table entry : P(bit)+M(bit)+ Length + Segment Base
Or, a combination of the two. Virtual Address : Segment Number + PageNumber + Offset Segment table entry : Length + Segment Base Page table entry : P(bit)+M(bit)+ Frame Number
![Page 19: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/19.jpg)
Memory Management in Linux Linux uses demand paged virtual memory for memory
management design. It's a dynamic memory allocation technique that consists of
deferring page frame allocation until the last possible moment, for example, when a process attemps to access a page that is not present in RAM.
Basic unit of memory allcation – page. Page size : 212 (4096 bytes or 4KB).
Allocation of blocks in physical memory is as page frames Protection mechanism is page by page Sharing is also based on pages Swapping controls automatic movement through the memory
hierarchy.
![Page 20: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/20.jpg)
Getting the Page size
The standard POSIX method #include <unistd.h> long sysconf (int name); long page_size = sysconf(_SC_PAGESIZE);
Linux also provides int getpagesize (void); Returns the page size in bytes.
PAGE_SIZE macro int page_size = PAGE_SIZE Retrieves the page size at compile time.
![Page 21: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/21.jpg)
Abstract view of memory management
![Page 22: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/22.jpg)
Managing the Virtual Address Space in Linux Each process : its own virtual address space.
In i386 arch, the virtual address is 32-bits wide. Therefore, the total virtual memory that a virtual address can
reference = 232 = 4GB. Page Size = 212. Therefore, number of pages that a virtual
address can reference = 220. Assuming that each PTE is 4 bytes, how many pages are
needed to store the page table?
![Page 23: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/23.jpg)
Two Level Page Table The amount of memory devoted to page tables alone is
quite high. Therefore, page tables are stored in virtual memory
rather than main memory. This is achieved thru the use of a two-level hierarchical page table.
Root Page Table (4KB)
User Page Table (4MB)
User Address Space (4 GB)
![Page 24: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/24.jpg)
Two-Level Page-Table Scheme
![Page 25: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/25.jpg)
Hierarchical paging
Address translation scheme:
![Page 26: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/26.jpg)
Virtual -> physical address translation……is a three level process in Linux
![Page 27: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/27.jpg)
Virtual address
4 parts: Page directory offset j.pgd Page middle director offset j.pmd Page table offset j.pte Offset within page j.offset
The physical address i for a virtual address j is : i = PTE(PMD(PGD(j.pgd)+j.pmd)+j.pte)+j.offset.
![Page 28: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/28.jpg)
•The x86(32-bit addressing) only supports a two level conversion of the address.•This is dictated by the hardware’s MMU…
•This is accomplished by reducing each page middle directory to only a single entry.
![Page 29: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/29.jpg)
Segmentation in Linux Linux uses the segmentation model in a limited way. Each virtual address space is divided into two segments: User segment (3 Gb) to contain the applications code and
data. Addressable by the user. Unmapped virtual addresses are simply not used.
Kernel segment (1 Gb) permanently mapped and associated with fixed physical memory addresses used by the kernel.
System calls execute in kernel segment(mode).
4 GB
3 GB
2 GB
1 GB
ox00000000
Kernel
Tasks
Kernel Space (Code + Data)
User Space (Code + Data)
![Page 30: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/30.jpg)
Per-Process Virtual Memory Layout Code (also called text)
segment Static Data segments
Initialized global (and C static) variables
Uninitialized global variables (zeroed when initializing the process, also called bss)
Stack segment: function calls, local variables (also called automatic in C)
Heap segment (malloc())
![Page 31: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/31.jpg)
Page Table Flags
Each entry in the theoretical page table contains the following information: Valid flag. This indicates if this page table entry is valid, The physical page frame number that this entry is describing, Access control information. This describes how the page may
be used. Can it be written to? Does it contain executable code?
Flags in the page table entry indicate The legal access modes into the page. The page’s status.
A page’s status can give vital information for how memory management is performed.
![Page 32: CSNB334 Advanced Operating Systems 5. Memory Management](https://reader034.vdocument.in/reader034/viewer/2022051516/56649e605503460f94b5b61e/html5/thumbnails/32.jpg)
Page Table Flags
PAGE_NONE – No physical memory page associated with entry.
PAGE_SHARED – All types of access permitted. PAGE_READONLY – No writing. “Copy-on-Write”
can be used. PAGE_KERNEL – kernel segment only allowed
access. PAGE_KERNEL_RO – kernel read-only access.