SlideShare a Scribd company logo
1 of 58
Operating Systems: CSE 3204
Chapter Seven
Main Memory
(Materials partly taken from Operating System Concepts by Silberschatz, Galvin and Gagne, 2005 – 7th Edition, chapter 1-2)
ASTU
Department of CSE
January 4, 2023 1
Operating Systems
Lecture 7: Memory Management: Main Memroy
Chapter 7: Memory Management
• Background
• Swapping
• Contiguous Memory Allocation
• Segmentation
• Paging
• Structure of the Page Table
Background
• At the time of execution, a program must be brought (from disk) into the main memory
(RAM) and placed within a process for it to be run
• Main memory and registers are only storage devices that CPU can access directly, all other
storage devices like hard disk and flash memory involves more delay in access as compared
to RAM and registers
• Memory unit only sees a stream of addresses + read requests, or address + data and write
requests issues by CPU
• Register access is generally faster than RAM, which in turns faster than secondary storage
devices, also there is considerable differences in the sizes of these storage devices (read
memory hierarchy)
• Main memory can take many cycles as compared to registers, causing a stall
• Cache is a faster than RAM and located between main memory and CPU registers
Protection of Address space
• Each process in the memory must be protected against
unauthorized access of its code, data, stack and other
segments by other processes
• For protection, A pair of base and limit registers is
maintained in order to define the logical address space
of a process in main memory
• CPU must check every memory access generated in
user mode to be sure it is between base and limit for
that user/process
• A hardware component is used for checking every
address generated by a user
Hardware Address Protection
Address Binding
• Generally, programs on the disk reside in the form of an executable file. And they must be brought into main
memory in order to be scheduled for execution, without support, programs must be loaded into the starting
address of RAM i.e. 0000
• But multiprogramming require ability to load the programs on the different addresses at the load time because
there may be multiple programs in the system.
• The nature of addresses changes with the lifetime of a program as follows:
• Source code addresses are usually symbolic
• Compiled code addresses bind to relocatable addresses
• i.e. “14 bytes from beginning of this module”
• Linker or loader addresses will bind relocatable addresses to absolute addresses
• i.e. 74014
Operating system implements various mechanisms for address binding at different levels, each binding maps
one address space to another
Binding of Instructions and Data to Memory
• Address binding of instructions and data to memory addresses can happen at
three different stages
• Compile time: If memory location known a priori, absolute code can be
generated; must recompile code if starting location changes
• Load time: Must generate relocatable code if memory location is not known
at compile time
• Execution time: Binding delayed until run time if the process can be moved
during its execution from one memory segment to another
• Need hardware support for address maps (e.g., base and limit registers)
Logical vs. Physical Address Space
• The concept of a logical address space that is bound to a separate physical address
space is central to proper memory management
• Logical address – generated by the CPU; also referred to as virtual address
• Physical address – address seen by the memory unit
• Logical and physical addresses are the same in compile-time and load-time address-
binding schemes; logical (virtual) and physical addresses differ in execution-time
address-binding scheme
• Logical address space is the set of all logical addresses generated by a program
• Physical address space is the set of all physical addresses generated by a program
Memory-Management Unit (MMU)
The run-time mapping from virtual to physical addresses is done by a hardware
device called the memory-management unit (MMU).
value in the relocation register is added to every address generated by a user
process at the time it is sent to memory
• Base register now called relocation register
• MS-DOS on Intel 80x86 used 4 relocation registers
• The user program deals with logical addresses; it never sees the real physical
addresses
• Execution-time binding occurs when reference is made to location in
memory
• Logical address bound to physical addresses
Dynamic relocation using a relocation register
 In case of dynamic loading, a routine is not loaded
until it is called
 Better memory-space utilization; unused routine is
never loaded
 All routines kept on disk in relocatable load format
 This method is useful when large amounts of code
are needed to handle infrequently occurring cases
 No special support from the operating system is
required
 Implemented through program design
 OS can help by providing libraries to
implement dynamic loading
Dynamic Linking
Static linking – System libraries and program code combined by the loader into the binary program image
• Programs becomes too large
Dynamic Linking: Dynamically linked libraries are system libraries that are linked to user programs when
the programs are run
Characteristics of dynamically linked code:
• linking postponed until execution time
• Small piece of code, stub, used to locate the appropriate memory-resident library routine
• Stub replaces itself with the address of the routine, and executes the routine
• Operating system checks if routine is in processes’ memory address
• If not in address space, add to address space
• Dynamic linking is particularly useful for libraries
• System also known as shared libraries
• Consider applicability to patching system libraries
• Versioning may be needed
Swapping
• Swapping is a technique to make it possible for the total physical address space of all processes to
exceed the real physical memory of the system, thus increasing the degree of multiprogramming in
a system.
A process can be swapped temporarily out of memory to a backing store, and then brought back
into memory for continued execution
• Backing store – it is a fast disk, large enough to accommodate copies of all memory images for all
users; it must provide direct access to these memory images
• Roll out, roll in – A swapping variant used for priority-based scheduling algorithms; lower-priority
process is swapped out so higher-priority process can be loaded and executed
• Major part of swap time is transfer time; total transfer time is directly proportional to the amount of
memory swapped
• System maintains a ready queue of ready-to-run processes which have memory images on disk.
• If memory is full, Dispatcher swaps out a process currently in the memory and swaps in the desired
process whose turn of execution comes next
Schematic View of Swapping
Context Switch Time including Swapping
• If next processes to be put on CPU is not in memory, need to swap out a process
and swap in target process
• If swapping is to be done for large process, context switch time can become very
high
• Let, 100MB process swapping to hard disk with transfer rate of 50MB/sec
• It takes swap out time of 2000 ms
• Plus swap in time of same sized process
• Total context switch swapping component time of 4000ms (4 seconds), which is
very high as compared to in memory context switch time
• Swapping time can be reduced, if we reduce the size of memory to be swapped –
i.e. by knowing how much memory really being used
Contiguous Allocation
• Main memory must support both OS and user processes
• Limited resource, must allocate efficiently
• Contiguous allocation is one early method
• Main memory is divided into two partitions:
• Resident operating system, usually held in low memory with interrupt vector
• User processes then held in high memory
• In contiguous memory allocation, each process is contained in a single section of
memory that is contiguous to the section containing the next process
Contiguous Allocation (Cont.)
• In contiguous memory allocation, relocation registers are used to protect user
processes from each other, and from changing operating-system code and data
• Base register contains value of smallest physical address
• Limit register contains range of logical addresses – each logical address must
be less than the limit register
• MMU maps logical address dynamically
• OS designers can then allow actions such as kernel code being transient and
kernel changing size
Hardware Support for Relocation and Limit Registers
Memory Allocation: Multiple-partition allocation
• Fixed size-partitioning: One of the simplest methods for
allocating memory is to divide memory into several fixed-sized
partitions. Each partition may contain exactly one process.
Degree of multiprogramming limited by number of partitions
• Variable size-partitioning in this scheme, sizes of partitions
may vary as per the need of process, it increases the efficiency
• Hole – A block of available memory in variable size
partitioning; holes of various size are scattered throughout
memory
• When a process arrives, it is allocated memory from a hole large
enough to accommodate it
• Process exiting frees its partition, adjacent free partitions
combined
• Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Dynamic Storage-Allocation Problem
• First-fit: Allocate the first hole that is big enough
• Best-fit: Allocate the smallest hole that is big enough; must search entire
list, unless ordered by size
• Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search entire list
• Produces the largest leftover hole
In a variable length, dynamic memory allocation scheme with variable size partitions , an
important question arises as the system runs various processes. i.e. How to satisfy a request
of size n from a list of free holes? Following algorithms are used for solving this problem:
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
Fragmentation
As processes are loaded and removed from memory, the free memory space is broken into little
pieces. External fragmentation exists when there is enough total memory space to satisfy a
request but the available spaces are not contiguous: storage is fragmented into a large number of
small holes.
External Fragmentation – Unused memory across the partitions. Total memory space exists to
satisfy a request, but it is not contiguous.
Internal Fragmentation – It is the unused memory internal to a partition. Allocated memory
may be slightly larger than requested memory; this size difference is memory internal to a
partition, but not being used
• First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to fragmentation
• 1/3 may be unusable -> 50-percent rule
Fragmentation solution (Cont.)
• To reduce external fragmentation OS use a technique called compaction
• In compaction OS Shuffle the memory contents to place all the free memory
together in one large block.
• Compaction is possible only if relocation is dynamic, and is done at execution
time.
• Another possible solution to the external-fragmentation problem is to permit
the logical address space of the processes to be noncontiguous, thus allowing a
process to be allocated physical memory wherever such memory is available.
Segmentation
• Memory-management scheme that supports user view of memory, allows non contiguous
allocations to a process in the form of segments.
• A program is a collection of segments. The programmer therefore specifies
each address by two quantities: a segment name and an offset.
• A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
User’s View of a Program
Logical View of Segmentation
1
3
2
4
1
4
2
3
user space physical memory space
Segmentation Architecture
• Logical address consists of a two tuple:
<segment-number, offset>,
• Segment table – maps two-dimensional physical addresses; each table entry has:
• base – contains the starting physical address where the segments reside in memory
• limit – specifies the length of the segment
• Segment-table base register (STBR) points to the segment table’s location in memory
• Segment-table length register (STLR) indicates number of segments used by a program;
segment number s is legal if s < STLR
Segmentation Architecture (Cont.)
• Protection
• With each entry in segment table associate:
• validation bit = 0  illegal segment
• read/write/execute privileges
• Protection bits associated with segments; code sharing occurs at segment level
• Since segments vary in length, memory allocation is a dynamic storage-allocation
problem
• A segmentation example is shown in the following diagram
Segmentation Hardware
Paging
• Paging is another technique which allows non contiguous allocation of memory to a process, it maps
logical addresses of pages into physical address of page frames. Physical address space of a process
can be noncontiguous. process is allocated physical memory whenever the latter is available. It avoids
the need of external fragmentation.
• Avoids problem of varying sized memory chunks, divide physical address space into fixed size
page frames
• Size is power of 2, between 512 bytes and 16 Mbytes
• It divide logical memory into blocks of same size called pages
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames and load program
• Set up a page table to translate logical to physical addresses
• Backing store likewise split into pages
• Still have Internal fragmentation
Address Translation Scheme
• Address generated by CPU is divided into:
• Page number (p) – used as an index into a page table which contains base
address of each page in physical memory
• Page offset (d) – combined with base address to define the physical
memory address that is sent to the memory unit
• For given logical address space 2m and page size 2n
page number page offset
p d
m -n n
Paging Hardware
Paging Model of Logical and Physical Memory
Paging Example
n=2 and m=4 32-byte memory and 4-byte pages
Paging (Cont.)
• Calculating internal fragmentation
• Page size = 2,048 bytes
• Process size = 72,766 bytes
• 35 pages + 1,086 bytes
• Internal fragmentation of 2,048 - 1,086 = 962 bytes
• Worst case fragmentation = 1 frame – 1 byte
• On average fragmentation = 1 / 2 frame size
• So small frame sizes desirable?
• But each page table entry takes memory to track
• Page sizes growing over time
• Solaris supports two page sizes – 8 KB and 4 MB
• Process view and physical memory now very different
• By implementation process can only access its own memory
Free Frames
Before allocation After allocation
Implementation of Page Table
• Page table is kept in main memory
• Page-table base register (PTBR) points to the page table
• Page-table length register (PTLR) indicates size of the page table
• In this scheme every data/instruction access requires two memory accesses
• One for the page table and one for the data / instruction
• The two memory access problem can be solved by the use of a special fast-
lookup hardware cache called associative memory or translation look-aside
buffers (TLBs)
Implementation of Page Table (Cont.)
• Some TLBs store address-space identifiers (ASIDs) in each TLB entry
– uniquely identifies each process to provide address-space protection for
that process
• Otherwise need to flush at every context switch
• TLBs typically small (64 to 1,024 entries)
• On a TLB miss, value is loaded into the TLB for faster access next time
• Replacement policies must be considered
• Some entries can be wired down for permanent fast access
Associative Memory
• Associative memory – parallel search
• Address translation (p, d)
• If p is in associative register, get frame # out
• Otherwise get frame # from page table in memory
Page # Frame #
Paging Hardware With TLB
Effective Access Time
• Associative Lookup =  time unit
• Can be < 10% of memory access time
• Hit ratio = 
• Hit ratio – percentage of times that a page number is found in the associative registers;
ratio related to number of associative registers
• Consider  = 80%,  = 20ns for TLB search, 100ns for memory access
• Effective Access Time (EAT)
EAT = (1 + )  + (2 + )(1 – )
= 2 +  – 
• Consider  = 80%,  = 20ns for TLB search, 100ns for memory access
• EAT = 0.80 x 100 + 0.20 x 200 = 120ns
• Consider more realistic hit ratio ->  = 99%,  = 20ns for TLB search, 100ns for memory
access
• EAT = 0.99 x 100 + 0.01 x 200 = 101ns
Memory Protection
• Memory protection is implemented by associating protection bit with each frame to
indicate if read-only or read-write access is allowed
• Can also add more bits to indicate page execute-only, and so on
• Valid-invalid bit attached to each entry in the page table:
• “valid” indicates that the associated page is in the process’ logical address space,
and is thus a legal page
• “invalid” indicates that the page is not in the process’ logical address space
• Or use page-table length register (PTLR)
• Any violations result in a trap to the kernel
Valid (v) or Invalid (i) Bit In A Page Table
Shared Pages
• Shared code
• One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems)
• Similar to multiple threads sharing the same process space
• Also useful for interprocess communication if sharing of read-write pages is
allowed
• Private code and data
• Each process keeps a separate copy of the code and data
• The pages for the private code and data can appear anywhere in the logical
address space
Shared Pages Example
Structure of the Page Table
• Memory structures for paging can get huge using straight-forward methods
• Consider a 32-bit logical address space as on modern computers
• Page size of 4 KB (212)
• Page table would have 1 million entries (232 / 212)
• If each entry is 4 bytes -> 4 MB of physical address space / memory for page table
alone
• That amount of memory used to cost a lot
• Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
• Hashed Page Tables
• Inverted Page Tables
Hierarchical Page Tables
• Break up the logical address space into multiple page tables
• A simple technique is a two-level page table
• We then page the page table
Two-Level Page-Table Scheme
Two-Level Paging Example
• A logical address (on 32-bit machine with 1K page size) is divided into:
• a page number consisting of 22 bits
• a page offset consisting of 10 bits
• Since the page table is paged, the page number is further divided into:
• a 12-bit page number
• a 10-bit page offset
• Thus, a logical address is as follows:
• where p1 is an index into the outer page table, and p2 is the displacement within the page
of the inner page table
• Known as forward-mapped page table
Address-Translation Scheme
64-bit Logical Address Space
• Even two-level paging scheme not sufficient
• If page size is 4 KB (212)
• Then page table has 252 entries
• If two level scheme, inner page tables could be 210 4-byte entries
• Address would look like
• Outer page table has 242 entries or 244 bytes
• One solution is to add a 2nd outer page table
• But in the following example the 2nd outer page table is still 234 bytes in size
• And possibly 4 memory access to get to one physical memory location
Three-level Paging Scheme
Hashed Page Tables
• Common in address spaces > 32 bits
• The virtual page number is hashed into a page table
• This page table contains a chain of elements hashing to the same location
• Each element contains (1) the virtual page number (2) the value of the mapped page
frame (3) a pointer to the next element
• Virtual page numbers are compared in this chain searching for a match
• If a match is found, the corresponding physical frame is extracted
• Variation for 64-bit addresses is clustered page tables
• Similar to hashed but each entry refers to several pages (such as 16) rather than 1
• Especially useful for sparse address spaces (where memory references are non-
contiguous and scattered)
Hashed Page Table
Inverted Page Table
• Rather than each process having a page table and keeping track of all possible logical pages,
track all physical pages
• One entry for each real page of memory
• Entry consists of the virtual address of the page stored in that real memory location, with
information about the process that owns that page
• Decreases memory needed to store each page table, but increases time needed to search the
table when a page reference occurs
• Use hash table to limit the search to one — or at most a few — page-table entries
• TLB can accelerate access
• But how to implement shared memory?
• One mapping of a virtual address to the shared physical address
Inverted Page Table Architecture
Questions
• Consider a logical address space of 64 pages of 1,024 words each, mapped onto a physical memory of 32 frames.
a. How many bits are there in the logical address?
b. How many bits are there in the physical address?
• Why are page sizes always powers of 2?
• Describe a mechanism by which one segment could belong to the address
space of two different processes.
• Sharing segments among processes without requiring that they have the
same segment number is possible in a dynamically linked segmentation
system.
a. Define a system that allows static linking and sharing of segments
without requiring that the segment numbers be the same.
b. Describe a paging scheme that allows pages to be shared without
requiring that the page numbers be the same.
January 4, 2023 55
Operating Systems
• Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB,
and 125 KB (in order), how would the first-fit, best-fit, and worst-fit
algorithms place processes of size 115 KB, 500 KB, 358 KB, 200 KB, and
375 KB (in order)? Rank the algorithms in terms of how efficiently they
use memory.
• Most systems allow a program to allocate more memory to its address
space during execution. Allocation of data in the heap segments of
programs is an example of such allocated memory. What is required
to support dynamic memory allocation in the following schemes?
a. Contiguous memory allocation
b. Pure segmentation
c. Pure paging
January 4, 2023 56
Operating Systems
• Compare the memory organization schemes of contiguous memory
allocation, pure segmentation, and pure paging with respect to the
following issues:
a. External fragmentation
b. Internal fragmentation
c. Ability to share code across processes
• Program binaries in many systems are typically structured as follows.
Code is stored starting with a small, fixed virtual address, such as 0. The
code segment is followed by the data segment that is used for storing
the program variables. When the program starts executing, the stack is
allocated at the other end of the virtual address space and is allowed
to grow toward lower virtual addresses. What is the significance of this
structure for the following schemes?
a. Contiguous memory allocation
b. Pure segmentation
c. Pure paging
January 4, 2023 57
Operating Systems
• Consider a logical address space of 256 pages with a 4-KB page size,
mapped onto a physical memory of 64 frames.
a. How many bits are required in the logical address?
b. How many bits are required in the physical address?
January 4, 2023 58
Operating Systems

More Related Content

Similar to Lecture-7 Main Memroy.pptx

Memory Management Strategies - II.pdf
Memory Management Strategies - II.pdfMemory Management Strategies - II.pdf
Memory Management Strategies - II.pdfHarika Pudugosula
 
CSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptxCSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptxMadhuraK13
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategiesDr. Loganathan R
 
MK Sistem Operasi.pdf
MK Sistem Operasi.pdfMK Sistem Operasi.pdf
MK Sistem Operasi.pdfwisard1
 
Chapter 9 OS
Chapter 9 OSChapter 9 OS
Chapter 9 OSC.U
 
Ios103 ios102 iv-operating-system-memory-management_wk4
Ios103 ios102 iv-operating-system-memory-management_wk4Ios103 ios102 iv-operating-system-memory-management_wk4
Ios103 ios102 iv-operating-system-memory-management_wk4Anwal Mirza
 
08 operating system support
08 operating system support08 operating system support
08 operating system supportdilip kumar
 
08 operating system support
08 operating system support08 operating system support
08 operating system supportBitta_man
 
ch8 Memory Management OS.pptx
ch8 Memory Management OS.pptxch8 Memory Management OS.pptx
ch8 Memory Management OS.pptxIndhu Periys
 
Computer Architecture & Organization.ppt
Computer Architecture & Organization.pptComputer Architecture & Organization.ppt
Computer Architecture & Organization.pptFarhanaMariyam1
 
08 operating system support
08 operating system support08 operating system support
08 operating system supportAnwal Mirza
 
07-MemoryManagement.ppt
07-MemoryManagement.ppt07-MemoryManagement.ppt
07-MemoryManagement.ppthello509579
 
Memory management
Memory managementMemory management
Memory managementPATELARCH
 
Introduction to memory management
Introduction to memory managementIntroduction to memory management
Introduction to memory managementSweety Singhal
 

Similar to Lecture-7 Main Memroy.pptx (20)

Memory Management Strategies - II.pdf
Memory Management Strategies - II.pdfMemory Management Strategies - II.pdf
Memory Management Strategies - II.pdf
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
CSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptxCSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptx
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
MK Sistem Operasi.pdf
MK Sistem Operasi.pdfMK Sistem Operasi.pdf
MK Sistem Operasi.pdf
 
Chapter 9 OS
Chapter 9 OSChapter 9 OS
Chapter 9 OS
 
Ios103 ios102 iv-operating-system-memory-management_wk4
Ios103 ios102 iv-operating-system-memory-management_wk4Ios103 ios102 iv-operating-system-memory-management_wk4
Ios103 ios102 iv-operating-system-memory-management_wk4
 
CH08.pdf
CH08.pdfCH08.pdf
CH08.pdf
 
CS6401 OPERATING SYSTEMS Unit 3
CS6401 OPERATING SYSTEMS Unit 3CS6401 OPERATING SYSTEMS Unit 3
CS6401 OPERATING SYSTEMS Unit 3
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
ch8 Memory Management OS.pptx
ch8 Memory Management OS.pptxch8 Memory Management OS.pptx
ch8 Memory Management OS.pptx
 
Computer Architecture & Organization.ppt
Computer Architecture & Organization.pptComputer Architecture & Organization.ppt
Computer Architecture & Organization.ppt
 
ch1.ppt
ch1.pptch1.ppt
ch1.ppt
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
07-MemoryManagement.ppt
07-MemoryManagement.ppt07-MemoryManagement.ppt
07-MemoryManagement.ppt
 
Memory management
Memory managementMemory management
Memory management
 
Chapter07_ds.ppt
Chapter07_ds.pptChapter07_ds.ppt
Chapter07_ds.ppt
 
Introduction to memory management
Introduction to memory managementIntroduction to memory management
Introduction to memory management
 

More from Amanuelmergia

Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxLecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxAmanuelmergia
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxOperating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxAmanuelmergia
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxOperating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxAmanuelmergia
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
 
OS course Outilne 2021.doc
OS course Outilne 2021.docOS course Outilne 2021.doc
OS course Outilne 2021.docAmanuelmergia
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdfAmanuelmergia
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdfLecture 2- Processes.pdf
Lecture 2- Processes.pdfAmanuelmergia
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfLecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfAmanuelmergia
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxLecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxAmanuelmergia
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxLecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxAmanuelmergia
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxAmanuelmergia
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxLecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxAmanuelmergia
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxAmanuelmergia
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxAmanuelmergia
 

More from Amanuelmergia (14)

Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxLecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptx
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxOperating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptx
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxOperating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptx
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
OS course Outilne 2021.doc
OS course Outilne 2021.docOS course Outilne 2021.doc
OS course Outilne 2021.doc
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdf
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdfLecture 2- Processes.pdf
Lecture 2- Processes.pdf
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfLecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdf
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxLecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptx
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxLecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptx
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxLecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptx
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 

Recently uploaded

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Lecture-7 Main Memroy.pptx

  • 1. Operating Systems: CSE 3204 Chapter Seven Main Memory (Materials partly taken from Operating System Concepts by Silberschatz, Galvin and Gagne, 2005 – 7th Edition, chapter 1-2) ASTU Department of CSE January 4, 2023 1 Operating Systems Lecture 7: Memory Management: Main Memroy
  • 2. Chapter 7: Memory Management • Background • Swapping • Contiguous Memory Allocation • Segmentation • Paging • Structure of the Page Table
  • 3. Background • At the time of execution, a program must be brought (from disk) into the main memory (RAM) and placed within a process for it to be run • Main memory and registers are only storage devices that CPU can access directly, all other storage devices like hard disk and flash memory involves more delay in access as compared to RAM and registers • Memory unit only sees a stream of addresses + read requests, or address + data and write requests issues by CPU • Register access is generally faster than RAM, which in turns faster than secondary storage devices, also there is considerable differences in the sizes of these storage devices (read memory hierarchy) • Main memory can take many cycles as compared to registers, causing a stall • Cache is a faster than RAM and located between main memory and CPU registers
  • 4. Protection of Address space • Each process in the memory must be protected against unauthorized access of its code, data, stack and other segments by other processes • For protection, A pair of base and limit registers is maintained in order to define the logical address space of a process in main memory • CPU must check every memory access generated in user mode to be sure it is between base and limit for that user/process • A hardware component is used for checking every address generated by a user
  • 6. Address Binding • Generally, programs on the disk reside in the form of an executable file. And they must be brought into main memory in order to be scheduled for execution, without support, programs must be loaded into the starting address of RAM i.e. 0000 • But multiprogramming require ability to load the programs on the different addresses at the load time because there may be multiple programs in the system. • The nature of addresses changes with the lifetime of a program as follows: • Source code addresses are usually symbolic • Compiled code addresses bind to relocatable addresses • i.e. “14 bytes from beginning of this module” • Linker or loader addresses will bind relocatable addresses to absolute addresses • i.e. 74014 Operating system implements various mechanisms for address binding at different levels, each binding maps one address space to another
  • 7. Binding of Instructions and Data to Memory • Address binding of instructions and data to memory addresses can happen at three different stages • Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes • Load time: Must generate relocatable code if memory location is not known at compile time • Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another • Need hardware support for address maps (e.g., base and limit registers)
  • 8. Logical vs. Physical Address Space • The concept of a logical address space that is bound to a separate physical address space is central to proper memory management • Logical address – generated by the CPU; also referred to as virtual address • Physical address – address seen by the memory unit • Logical and physical addresses are the same in compile-time and load-time address- binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme • Logical address space is the set of all logical addresses generated by a program • Physical address space is the set of all physical addresses generated by a program
  • 9. Memory-Management Unit (MMU) The run-time mapping from virtual to physical addresses is done by a hardware device called the memory-management unit (MMU). value in the relocation register is added to every address generated by a user process at the time it is sent to memory • Base register now called relocation register • MS-DOS on Intel 80x86 used 4 relocation registers • The user program deals with logical addresses; it never sees the real physical addresses • Execution-time binding occurs when reference is made to location in memory • Logical address bound to physical addresses
  • 10. Dynamic relocation using a relocation register  In case of dynamic loading, a routine is not loaded until it is called  Better memory-space utilization; unused routine is never loaded  All routines kept on disk in relocatable load format  This method is useful when large amounts of code are needed to handle infrequently occurring cases  No special support from the operating system is required  Implemented through program design  OS can help by providing libraries to implement dynamic loading
  • 11. Dynamic Linking Static linking – System libraries and program code combined by the loader into the binary program image • Programs becomes too large Dynamic Linking: Dynamically linked libraries are system libraries that are linked to user programs when the programs are run Characteristics of dynamically linked code: • linking postponed until execution time • Small piece of code, stub, used to locate the appropriate memory-resident library routine • Stub replaces itself with the address of the routine, and executes the routine • Operating system checks if routine is in processes’ memory address • If not in address space, add to address space • Dynamic linking is particularly useful for libraries • System also known as shared libraries • Consider applicability to patching system libraries • Versioning may be needed
  • 12. Swapping • Swapping is a technique to make it possible for the total physical address space of all processes to exceed the real physical memory of the system, thus increasing the degree of multiprogramming in a system. A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution • Backing store – it is a fast disk, large enough to accommodate copies of all memory images for all users; it must provide direct access to these memory images • Roll out, roll in – A swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed • Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped • System maintains a ready queue of ready-to-run processes which have memory images on disk. • If memory is full, Dispatcher swaps out a process currently in the memory and swaps in the desired process whose turn of execution comes next
  • 13. Schematic View of Swapping
  • 14. Context Switch Time including Swapping • If next processes to be put on CPU is not in memory, need to swap out a process and swap in target process • If swapping is to be done for large process, context switch time can become very high • Let, 100MB process swapping to hard disk with transfer rate of 50MB/sec • It takes swap out time of 2000 ms • Plus swap in time of same sized process • Total context switch swapping component time of 4000ms (4 seconds), which is very high as compared to in memory context switch time • Swapping time can be reduced, if we reduce the size of memory to be swapped – i.e. by knowing how much memory really being used
  • 15. Contiguous Allocation • Main memory must support both OS and user processes • Limited resource, must allocate efficiently • Contiguous allocation is one early method • Main memory is divided into two partitions: • Resident operating system, usually held in low memory with interrupt vector • User processes then held in high memory • In contiguous memory allocation, each process is contained in a single section of memory that is contiguous to the section containing the next process
  • 16. Contiguous Allocation (Cont.) • In contiguous memory allocation, relocation registers are used to protect user processes from each other, and from changing operating-system code and data • Base register contains value of smallest physical address • Limit register contains range of logical addresses – each logical address must be less than the limit register • MMU maps logical address dynamically • OS designers can then allow actions such as kernel code being transient and kernel changing size
  • 17. Hardware Support for Relocation and Limit Registers
  • 18. Memory Allocation: Multiple-partition allocation • Fixed size-partitioning: One of the simplest methods for allocating memory is to divide memory into several fixed-sized partitions. Each partition may contain exactly one process. Degree of multiprogramming limited by number of partitions • Variable size-partitioning in this scheme, sizes of partitions may vary as per the need of process, it increases the efficiency • Hole – A block of available memory in variable size partitioning; holes of various size are scattered throughout memory • When a process arrives, it is allocated memory from a hole large enough to accommodate it • Process exiting frees its partition, adjacent free partitions combined • Operating system maintains information about: a) allocated partitions b) free partitions (hole)
  • 19. Dynamic Storage-Allocation Problem • First-fit: Allocate the first hole that is big enough • Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size • Produces the smallest leftover hole • Worst-fit: Allocate the largest hole; must also search entire list • Produces the largest leftover hole In a variable length, dynamic memory allocation scheme with variable size partitions , an important question arises as the system runs various processes. i.e. How to satisfy a request of size n from a list of free holes? Following algorithms are used for solving this problem: First-fit and best-fit better than worst-fit in terms of speed and storage utilization
  • 20. Fragmentation As processes are loaded and removed from memory, the free memory space is broken into little pieces. External fragmentation exists when there is enough total memory space to satisfy a request but the available spaces are not contiguous: storage is fragmented into a large number of small holes. External Fragmentation – Unused memory across the partitions. Total memory space exists to satisfy a request, but it is not contiguous. Internal Fragmentation – It is the unused memory internal to a partition. Allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used • First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to fragmentation • 1/3 may be unusable -> 50-percent rule
  • 21. Fragmentation solution (Cont.) • To reduce external fragmentation OS use a technique called compaction • In compaction OS Shuffle the memory contents to place all the free memory together in one large block. • Compaction is possible only if relocation is dynamic, and is done at execution time. • Another possible solution to the external-fragmentation problem is to permit the logical address space of the processes to be noncontiguous, thus allowing a process to be allocated physical memory wherever such memory is available.
  • 22. Segmentation • Memory-management scheme that supports user view of memory, allows non contiguous allocations to a process in the form of segments. • A program is a collection of segments. The programmer therefore specifies each address by two quantities: a segment name and an offset. • A segment is a logical unit such as: main program procedure function method object local variables, global variables common block stack symbol table arrays
  • 23. User’s View of a Program
  • 24. Logical View of Segmentation 1 3 2 4 1 4 2 3 user space physical memory space
  • 25. Segmentation Architecture • Logical address consists of a two tuple: <segment-number, offset>, • Segment table – maps two-dimensional physical addresses; each table entry has: • base – contains the starting physical address where the segments reside in memory • limit – specifies the length of the segment • Segment-table base register (STBR) points to the segment table’s location in memory • Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR
  • 26. Segmentation Architecture (Cont.) • Protection • With each entry in segment table associate: • validation bit = 0  illegal segment • read/write/execute privileges • Protection bits associated with segments; code sharing occurs at segment level • Since segments vary in length, memory allocation is a dynamic storage-allocation problem • A segmentation example is shown in the following diagram
  • 28. Paging • Paging is another technique which allows non contiguous allocation of memory to a process, it maps logical addresses of pages into physical address of page frames. Physical address space of a process can be noncontiguous. process is allocated physical memory whenever the latter is available. It avoids the need of external fragmentation. • Avoids problem of varying sized memory chunks, divide physical address space into fixed size page frames • Size is power of 2, between 512 bytes and 16 Mbytes • It divide logical memory into blocks of same size called pages • Keep track of all free frames • To run a program of size N pages, need to find N free frames and load program • Set up a page table to translate logical to physical addresses • Backing store likewise split into pages • Still have Internal fragmentation
  • 29. Address Translation Scheme • Address generated by CPU is divided into: • Page number (p) – used as an index into a page table which contains base address of each page in physical memory • Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit • For given logical address space 2m and page size 2n page number page offset p d m -n n
  • 31. Paging Model of Logical and Physical Memory
  • 32. Paging Example n=2 and m=4 32-byte memory and 4-byte pages
  • 33. Paging (Cont.) • Calculating internal fragmentation • Page size = 2,048 bytes • Process size = 72,766 bytes • 35 pages + 1,086 bytes • Internal fragmentation of 2,048 - 1,086 = 962 bytes • Worst case fragmentation = 1 frame – 1 byte • On average fragmentation = 1 / 2 frame size • So small frame sizes desirable? • But each page table entry takes memory to track • Page sizes growing over time • Solaris supports two page sizes – 8 KB and 4 MB • Process view and physical memory now very different • By implementation process can only access its own memory
  • 34. Free Frames Before allocation After allocation
  • 35. Implementation of Page Table • Page table is kept in main memory • Page-table base register (PTBR) points to the page table • Page-table length register (PTLR) indicates size of the page table • In this scheme every data/instruction access requires two memory accesses • One for the page table and one for the data / instruction • The two memory access problem can be solved by the use of a special fast- lookup hardware cache called associative memory or translation look-aside buffers (TLBs)
  • 36. Implementation of Page Table (Cont.) • Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely identifies each process to provide address-space protection for that process • Otherwise need to flush at every context switch • TLBs typically small (64 to 1,024 entries) • On a TLB miss, value is loaded into the TLB for faster access next time • Replacement policies must be considered • Some entries can be wired down for permanent fast access
  • 37. Associative Memory • Associative memory – parallel search • Address translation (p, d) • If p is in associative register, get frame # out • Otherwise get frame # from page table in memory Page # Frame #
  • 39. Effective Access Time • Associative Lookup =  time unit • Can be < 10% of memory access time • Hit ratio =  • Hit ratio – percentage of times that a page number is found in the associative registers; ratio related to number of associative registers • Consider  = 80%,  = 20ns for TLB search, 100ns for memory access • Effective Access Time (EAT) EAT = (1 + )  + (2 + )(1 – ) = 2 +  –  • Consider  = 80%,  = 20ns for TLB search, 100ns for memory access • EAT = 0.80 x 100 + 0.20 x 200 = 120ns • Consider more realistic hit ratio ->  = 99%,  = 20ns for TLB search, 100ns for memory access • EAT = 0.99 x 100 + 0.01 x 200 = 101ns
  • 40. Memory Protection • Memory protection is implemented by associating protection bit with each frame to indicate if read-only or read-write access is allowed • Can also add more bits to indicate page execute-only, and so on • Valid-invalid bit attached to each entry in the page table: • “valid” indicates that the associated page is in the process’ logical address space, and is thus a legal page • “invalid” indicates that the page is not in the process’ logical address space • Or use page-table length register (PTLR) • Any violations result in a trap to the kernel
  • 41. Valid (v) or Invalid (i) Bit In A Page Table
  • 42. Shared Pages • Shared code • One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems) • Similar to multiple threads sharing the same process space • Also useful for interprocess communication if sharing of read-write pages is allowed • Private code and data • Each process keeps a separate copy of the code and data • The pages for the private code and data can appear anywhere in the logical address space
  • 44. Structure of the Page Table • Memory structures for paging can get huge using straight-forward methods • Consider a 32-bit logical address space as on modern computers • Page size of 4 KB (212) • Page table would have 1 million entries (232 / 212) • If each entry is 4 bytes -> 4 MB of physical address space / memory for page table alone • That amount of memory used to cost a lot • Don’t want to allocate that contiguously in main memory • Hierarchical Paging • Hashed Page Tables • Inverted Page Tables
  • 45. Hierarchical Page Tables • Break up the logical address space into multiple page tables • A simple technique is a two-level page table • We then page the page table
  • 47. Two-Level Paging Example • A logical address (on 32-bit machine with 1K page size) is divided into: • a page number consisting of 22 bits • a page offset consisting of 10 bits • Since the page table is paged, the page number is further divided into: • a 12-bit page number • a 10-bit page offset • Thus, a logical address is as follows: • where p1 is an index into the outer page table, and p2 is the displacement within the page of the inner page table • Known as forward-mapped page table
  • 49. 64-bit Logical Address Space • Even two-level paging scheme not sufficient • If page size is 4 KB (212) • Then page table has 252 entries • If two level scheme, inner page tables could be 210 4-byte entries • Address would look like • Outer page table has 242 entries or 244 bytes • One solution is to add a 2nd outer page table • But in the following example the 2nd outer page table is still 234 bytes in size • And possibly 4 memory access to get to one physical memory location
  • 51. Hashed Page Tables • Common in address spaces > 32 bits • The virtual page number is hashed into a page table • This page table contains a chain of elements hashing to the same location • Each element contains (1) the virtual page number (2) the value of the mapped page frame (3) a pointer to the next element • Virtual page numbers are compared in this chain searching for a match • If a match is found, the corresponding physical frame is extracted • Variation for 64-bit addresses is clustered page tables • Similar to hashed but each entry refers to several pages (such as 16) rather than 1 • Especially useful for sparse address spaces (where memory references are non- contiguous and scattered)
  • 53. Inverted Page Table • Rather than each process having a page table and keeping track of all possible logical pages, track all physical pages • One entry for each real page of memory • Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page • Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs • Use hash table to limit the search to one — or at most a few — page-table entries • TLB can accelerate access • But how to implement shared memory? • One mapping of a virtual address to the shared physical address
  • 54. Inverted Page Table Architecture
  • 55. Questions • Consider a logical address space of 64 pages of 1,024 words each, mapped onto a physical memory of 32 frames. a. How many bits are there in the logical address? b. How many bits are there in the physical address? • Why are page sizes always powers of 2? • Describe a mechanism by which one segment could belong to the address space of two different processes. • Sharing segments among processes without requiring that they have the same segment number is possible in a dynamically linked segmentation system. a. Define a system that allows static linking and sharing of segments without requiring that the segment numbers be the same. b. Describe a paging scheme that allows pages to be shared without requiring that the page numbers be the same. January 4, 2023 55 Operating Systems
  • 56. • Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and 125 KB (in order), how would the first-fit, best-fit, and worst-fit algorithms place processes of size 115 KB, 500 KB, 358 KB, 200 KB, and 375 KB (in order)? Rank the algorithms in terms of how efficiently they use memory. • Most systems allow a program to allocate more memory to its address space during execution. Allocation of data in the heap segments of programs is an example of such allocated memory. What is required to support dynamic memory allocation in the following schemes? a. Contiguous memory allocation b. Pure segmentation c. Pure paging January 4, 2023 56 Operating Systems
  • 57. • Compare the memory organization schemes of contiguous memory allocation, pure segmentation, and pure paging with respect to the following issues: a. External fragmentation b. Internal fragmentation c. Ability to share code across processes • Program binaries in many systems are typically structured as follows. Code is stored starting with a small, fixed virtual address, such as 0. The code segment is followed by the data segment that is used for storing the program variables. When the program starts executing, the stack is allocated at the other end of the virtual address space and is allowed to grow toward lower virtual addresses. What is the significance of this structure for the following schemes? a. Contiguous memory allocation b. Pure segmentation c. Pure paging January 4, 2023 57 Operating Systems
  • 58. • Consider a logical address space of 256 pages with a 4-KB page size, mapped onto a physical memory of 64 frames. a. How many bits are required in the logical address? b. How many bits are required in the physical address? January 4, 2023 58 Operating Systems