SlideShare a Scribd company logo
1 of 93
OPERATING SYSTEMS
UNIT-IV
Memory Management &
Virtual Memory Management
ADITYA ENGINEERING COLLEGE (A)
By
P.SRI LATHA
Asst.Professor
Dept of Computer Science and Engineering
Aditya Engineering College(A)
Surampalem.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Memory Management
• Background
• Swapping
• Contiguous Memory Allocation
• Segmentation
• Paging
• Structure of the Page Table
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Objectives
• To provide a detailed description of
various ways of organizing memory
hardware.
• To discuss various memory-management
techniques, including paging and
segmentation.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background
• Program must be brought (from disk) into memory
and placed within a process for it to run.
• Main memory and registers are only storage entities
that a CPU can access directly.
• The CPU fetches instructions from main memory
according to the value of the program counter.
• Typical instruction execution cycle – fetch instruction
from memory, decode the instruction, operand fetch,
possible storage of result in memory.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background (Cont.)
• Memory unit only sees a stream of one of the following:
– address + read requests (e.g., load memory location
20010 into register number 8)
– address + data and write requests (e.g., store content of
register 6 into memory location 1090)
• Memory unit does not know how these addresses were
generated.
• Register access can be done in one CPU clock (or less).
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background (Cont.)
• Completing a memory access may take many cycles of the
CPU clock. In such a case the processor needs to stall since it
does not have the data required to complete the instruction it
is executing.
• Cache sits between main memory and CPU registers to deal
with the “stall” issue.
• Protection of memory is required to ensure correct
operation:
– User process cannot access OS memory
– One user process cannot access the memory of another
user process.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Physical Address
• The physical address refers to a location in the
memory. It allows access to data in the main
memory.
• A physical address is not directly accessible to
the user program hence, a logical address
needs to be mapped to it to make the address
accessible. This mapping is done by
the MMU.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Logical Address
• A logical address or virtual address is an
address that is generated by the CPU during
program execution.
• A logical address doesn't exist physically. The
logical address is used as a reference to access
the physical address.
• This logical address combines with the base
address generated by the MMU to form
the physical address.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
• 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.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
mapping between logical and physical addresses
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Memory Management unit(MMU)
• The Memory Management Unit is a combination of 2 registers –
• Base Register (Relocation Register)
• Limit Register.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Hardware Address Protection
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
• The binding is necessary to link the logical memory to the physical
memory. To know where the program is stored is necessary in order to
access it.
The binding may be of three different types.
• Compile Time Binding:Address where the program is stored is known at
compile time.
• Load Time Binding:Address is not known at compile time but known at
loading of program i.e,before running.
• Run Time Binding:Address is known at running of executable program.
Address Binding
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Multi step Processing of a User Program
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Static Linking
• Linker:A linker is a program that takes one or more
object files generated by a compiler and combines them
into a single executable file
• Static linking: In static linking, the linker combines all
necessary program modules into a single executable
program. Some operating systems support only static
linking, in which system language libraries are treated
like any other object module.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Dynamic linking
• In dynamic linking, “Stub” is included for each
appropriate library routine reference.
• A stub is a small piece of code. When the stub
is executed, it checks whether the needed
routine is already in memory or not. If not
available then the program loads the routine
into memory.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Schematic View of Swapping
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Swapping (contd..)
• A process can be swapped temporarily out of memory to
a backing store, and then brought back into memory for
continued execution
• Backing store – fast disk large enough to accommodate
copies of all memory images for all users; must provide
direct access to these memory images
• Roll out, roll in – 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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Contiguous Allocation
• In Contiguous memory allocation which is a
memory management technique, whenever there
is a request by the user process for the memory
then a single section of the contiguous memory
block is given to that process according to its
requirement.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Multiple-partition allocation
– Degree of multiprogramming limited by number of partitions
– Variable-partition sizes for efficiency (sized to a given process’ needs)
– Hole – block of available memory; 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)
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Memory allocation techniques
First fit:-
In the first fit, the first available free hole fulfills the requirement
of the process allocated
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Best fit:-
In the best fit, allocate the smallest hole that is big enough to
process requirements. For this, we search the entire list, unless the
list is ordered by size.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Worst fit:-In the worst fit, allocate the largest available hole to
process. This method produces the largest leftover hole.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Fragmentation
• External Fragmentation – total memory space
exists to satisfy a request, but it is not
contiguous
• Internal Fragmentation – allocated memory
may be slightly larger than requested memory;
this size difference is memory internal to a
partition, but not being used.
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Fragmentation (Cont.)
• Reduce external fragmentation by compaction
– Shuffle memory contents to place all free
memory together in one large block
– Compaction is possible only if relocation is
dynamic, and is done at execution time
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Segmentation
• Memory-management scheme that supports user view of
memory
• A program is a collection of segments
– A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
User’s View of a Program
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Logical View of Segmentation
1
3
2
4
1
4
2
3
user space physical memory space
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Segmentation Hardware
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Paging
• Physical address space of a process can be noncontiguous;
process is allocated physical memory whenever the latter is
available
– Avoids external fragmentation
– Avoids problem of varying sized memory chunks
• Divide physical memory into fixed-sized blocks called frames
– Size is power of 2, between 512 bytes and 16 Mbytes
• 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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Paging Hardware
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Paging Model of Logical and Physical Memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Free Frames
Before allocation After allocation
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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)
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
• TLBs typically small (64 to 1,024 entries)
• On a TLB miss, value is loaded into the TLB for faster
access next time
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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 #
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Paging Hardware With TLB
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Memory Protection
• Memory protection 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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Valid (v) or Invalid (i) Bit In A Page Table
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Shared Pages Example
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Two-Level Page-Table Scheme
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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:
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Address-Translation Scheme
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Three-level Paging Scheme
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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)
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Hashed Page Table
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
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
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Inverted Page Table Architecture
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Virtual Memory
• Background
• Demand Paging
• Page Replacement
• Thrashing
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Objectives
• To describe the benefits of a virtual
memory system
• To explain the concepts of demand
paging, page-replacement algorithms,
and allocation of page frames
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background
• Code needs to be in memory to execute, but entire
program rarely used
– Error code, unusual routines, large data structures
• Entire program code not needed at same time
• Consider ability to execute partially-loaded
program
– Program no longer constrained by limits of physical
memory
– Each program takes less memory while running ->
more programs run at the same time
• Increased CPU utilization and throughput with no increase
in response time or turnaround time
– Less I/O needed to load or swap programs into
memory -> each user program runs faster
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background (Cont.)
• Virtual memory – separation of user logical memory
from physical memory
– Only part of the program needs to be in memory
for execution
– Logical address space can therefore be much
larger than physical address space
– Allows address spaces to be shared by several
processes
– Allows for more efficient process creation
– More programs running concurrently
– Less I/O needed to load or swap processes
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Background (Cont.)
• Virtual address space – logical view of how
process is stored in memory
– Usually start at address 0, contiguous
addresses until end of space
– Meanwhile, physical memory organized in
page frames
– MMU must map logical to physical
• Virtual memory can be implemented via:
– Demand paging
– Demand segmentation
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Virtual Memory That is Larger Than Physical Memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Virtual-address Space
 Usually design logical address space for
stack to start at Max logical address and
grow “down” while heap grows “up”
 Maximizes address space use
 Unused address space between
the two is hole
 No physical memory needed
until heap or stack grows to a
given new page
 Enables sparse address spaces with
holes left for growth, dynamically linked
libraries, etc
 System libraries shared via mapping into
virtual address space
 Shared memory by mapping pages read-
write into virtual address space
 Pages can be shared during fork(),
speeding process creation
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Shared Library Using Virtual Memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Demand Paging
• Could bring entire process into
memory at load time
• Or bring a page into memory only
when it is needed
– Less I/O needed, no unnecessary
I/O
– Less memory needed
– Faster response
– More users
• Similar to paging system with swapping
(diagram on right)
• Page is needed  reference to it
– invalid reference  abort
– not-in-memory  bring to
memory
• Lazy swapper – never swaps a page
into memory unless page will be
needed
– Swapper that deals with pages is a
pager
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Basic Concepts
• With swapping, pager guesses which pages will be used
before swapping out again
• Instead, pager brings in only those pages into memory
• How to determine that set of pages?
– Need new MMU functionality to implement demand
paging
• If pages needed are already memory resident
– No difference from non demand-paging
• If page needed and not memory resident
– Need to detect and load the page into memory from
storage
• Without changing program behavior
• Without programmer needing to change code
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Valid-Invalid Bit
• With each page table entry a valid–invalid bit is
associated
(v  in-memory – memory resident, i  not-
in-memory)
• Initially valid–invalid bit is set to i on all entries
• Example of a page table snapshot:
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page Table When Some Pages Are Not in Main Memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page Fault
• If there is a reference to a page, first reference to
that page will trap to operating system:page fault
1. Operating system looks at another table to
decide:
– Invalid reference  abort
– Just not in memory
2. Find free frame
3. Swap page into frame via scheduled disk
operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Steps in Handling a Page Fault
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Performance of Demand Paging
• Stages in Demand Paging (worse case)
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Performance of Demand Paging (Cont.)
• Three major activities
– Service the interrupt – careful coding means just several
hundred instructions needed
– Read the page – lots of time
– Restart the process – again just a small amount of time
• Page Fault Rate 0  p  1
– if p = 0 no page faults
– if p = 1, every reference is a fault
• Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in )
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Copy-on-Write
• Copy-on-Write (COW) allows both parent and child processes to initially
share the same pages in memory
– If either process modifies a shared page, only then is the page
copied
• COW allows more efficient process creation as only modified pages are
copied
• In general, free pages are allocated from a pool of zero-fill-on-demand
pages
– Pool should always have free frames for fast demand page execution
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Before Process 1 Modifies Page C
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
After Process 1 Modifies Page C
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
What Happens if There is no Free Frame?
• Used up by process pages
• Also in demand from the kernel, I/O buffers, etc
• How much to allocate to each?
• Page replacement – find some page in memory,
but not really in use, page it out
– Algorithm – terminate? swap out? replace the
page?
– Performance – want an algorithm which will
result in minimum number of page faults
• Same page may be brought into memory several
times
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page Replacement
• Prevent over-allocation of memory by
modifying page-fault service routine to
include page replacement
• Use modify (dirty) bit to reduce overhead
of page transfers – only modified pages are
written to disk
• Page replacement completes separation
between logical memory and physical
memory – large virtual memory can be
provided on a smaller physical memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Need For Page Replacement
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Basic Page Replacement
1. Find the location of the desired page on disk
2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page
replacement algorithm to select a victim frame
- Write victim frame to disk if dirty
3. Bring the desired page into the (newly) free
frame; update the page and frame tables
4. Continue the process by restarting the
instruction that caused the trap
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page Replacement
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page and Frame Replacement Algorithms
• Frame-allocation algorithm determines
– How many frames to give each process
– Which frames to replace
• Page-replacement algorithm
– Want lowest page-fault rate on both first access and re-access
• Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page faults
on that string
– String is just page numbers, not full addresses
– Repeated access to the same page does not cause a page fault
– Results depend on number of frames available
• In all our examples, the reference string of referenced page numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Graph of Page Faults Versus The Number of Frames
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
First-In-First-Out (FIFO) Algorithm
• Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per
process)
Can vary by reference string: consider
1,2,3,4,1,2,5,1,2,3,4,5
– Adding more frames can cause more page faults!
• Belady’s Anomaly
• How to track ages of pages?
– Just use a FIFO queue
15 page faults
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
FIFO Illustrating Belady’s Anomaly
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Optimal Algorithm
• Replace page that will not be used for longest
period of time
– 9 is optimal for the example
• How do you know this?
– Can’t read the future
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Least Recently Used (LRU) Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in
the most amount of time
• Associate time of last use with each page
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
LRU Algorithm (Cont.)
• Counter implementation
– Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
– When a page needs to be changed, look at the counters to find
smallest value
• Search through table needed
• Stack implementation
– Keep a stack of page numbers in a double link form:
– Page referenced:
• move it to the top
• requires 6 pointers to be changed
– But each update more expensive
– No search for replacement
• LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Use Of A Stack to Record Most Recent Page References
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Counting Algorithms
• Keep a counter of the number of
references that have been made to each
page
– Not common
• Lease Frequently Used (LFU) Algorithm:
replaces page with smallest count
• Most Frequently Used (MFU) Algorithm:
based on the argument that the page
with the smallest count was probably just
brought in and has yet to be used
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Page-Buffering Algorithms
• Keep a pool of free frames, always
– Then frame available when needed, not found at fault time
– Read page into free frame and select victim to evict and add to
free pool
– When convenient, evict victim
• Possibly, keep list of modified pages
– When backing store otherwise idle, write pages there and set to
non-dirty
• Possibly, keep free frame contents intact and note what is in them
– If referenced again before reused, no need to load contents again
from disk
– Generally useful to reduce penalty if wrong victim frame selected
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Allocation of Frames
• Each process needs minimum number of frames
• Example: IBM 370 – 6 pages to handle SS MOVE
instruction:
– instruction is 6 bytes, might span 2 pages
– 2 pages to handle from
– 2 pages to handle to
• Maximum of course is total frames in the system
• Two major allocation schemes
– fixed allocation
– priority allocation
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Fixed Allocation
• Equal allocation – For example, if there
are 100 frames (after allocating frames
for the OS) and 5 processes, give each
process 20 frames
– Keep some as free frame buffer pool
• Proportional allocation – Allocate
according to the size of process
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Priority Allocation
• Use a proportional allocation scheme
using priorities rather than size
• If process Pi generates a page fault,
– select for replacement one of its frames
– select for replacement a frame from a
process with lower priority number
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Global vs. Local Allocation
• Global replacement – process selects a
replacement frame from the set of all frames;
one process can take a frame from another
– But then process execution time can vary
greatly
– But greater throughput so more common
• Local replacement – each process selects from
only its own set of allocated frames
– More consistent per-process performance
– But possibly underutilized memory
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Thrashing
• If a process does not have “enough” pages, the page-
fault rate is very high
– Page fault to get page
– Replace existing frame
– But quickly need replaced frame back
– This leads to:
• Low CPU utilization
• Operating system thinking that it needs to
increase the degree of multiprogramming
• Another process added to the system
• Thrashing  a process is busy swapping pages in and
out
Aditya Engineering College(A)
Operating Systems P.SRI LATHA Saturday, 07 October 2023
Thrashing (Cont.)

More Related Content

Similar to UNIT-4.ppt

Similar to UNIT-4.ppt (20)

Memory Management in Operating System
Memory Management in Operating SystemMemory Management in Operating System
Memory Management in Operating System
 
CS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptxCS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptx
 
Data Gaurd Final Thesis for University in Progress (2).docx
Data Gaurd Final Thesis for University in Progress (2).docxData Gaurd Final Thesis for University in Progress (2).docx
Data Gaurd Final Thesis for University in Progress (2).docx
 
memory managment on computer science.ppt
memory managment on computer science.pptmemory managment on computer science.ppt
memory managment on computer science.ppt
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxLecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
 
OS Unit 2.pptx
OS Unit 2.pptxOS Unit 2.pptx
OS Unit 2.pptx
 
UNIT-III EMBEDDED FIRMWARE DESIGNEmbedded Firmware design
UNIT-III  EMBEDDED FIRMWARE DESIGNEmbedded Firmware designUNIT-III  EMBEDDED FIRMWARE DESIGNEmbedded Firmware design
UNIT-III EMBEDDED FIRMWARE DESIGNEmbedded Firmware design
 
Azure Cloud Computing.pptx
Azure Cloud Computing.pptxAzure Cloud Computing.pptx
Azure Cloud Computing.pptx
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
Architecture and implementation issues of multi core processors and caching –...
Architecture and implementation issues of multi core processors and caching –...Architecture and implementation issues of multi core processors and caching –...
Architecture and implementation issues of multi core processors and caching –...
 
Bab 4
Bab 4Bab 4
Bab 4
 
CS403: Operating System :Lec 2 Function of OS.pptx
CS403: Operating System :Lec 2 Function of OS.pptxCS403: Operating System :Lec 2 Function of OS.pptx
CS403: Operating System :Lec 2 Function of OS.pptx
 
Operating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementOperating Systems Part III-Memory Management
Operating Systems Part III-Memory Management
 
CS403PC Operating System Lec 9 scheculing concepts.pptx
CS403PC Operating System Lec 9 scheculing concepts.pptxCS403PC Operating System Lec 9 scheculing concepts.pptx
CS403PC Operating System Lec 9 scheculing concepts.pptx
 
OS.pptx
OS.pptxOS.pptx
OS.pptx
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
GridIIT Open Science Grid
GridIIT Open Science GridGridIIT Open Science Grid
GridIIT Open Science Grid
 
Lecture 44
Lecture 44Lecture 44
Lecture 44
 
Unit iiios Storage Management
Unit iiios Storage ManagementUnit iiios Storage Management
Unit iiios Storage Management
 

More from Sri Latha (11)

key management in cryptography and network security
key management in cryptography and network securitykey management in cryptography and network security
key management in cryptography and network security
 
owasp features in secure coding techniques
owasp  features in secure coding techniquesowasp  features in secure coding techniques
owasp features in secure coding techniques
 
supraja technologies material for secure coding
supraja technologies material for secure codingsupraja technologies material for secure coding
supraja technologies material for secure coding
 
LOW LEVEL DESIGN INSPECTION SECURE CODING
LOW LEVEL DESIGN INSPECTION SECURE CODINGLOW LEVEL DESIGN INSPECTION SECURE CODING
LOW LEVEL DESIGN INSPECTION SECURE CODING
 
Ml Fundamentals and applications using python
Ml Fundamentals and applications using pythonMl Fundamentals and applications using python
Ml Fundamentals and applications using python
 
COMPUTER ENGINEERING WORKSHOP MATERIALS UNIT-1
COMPUTER ENGINEERING WORKSHOP MATERIALS UNIT-1COMPUTER ENGINEERING WORKSHOP MATERIALS UNIT-1
COMPUTER ENGINEERING WORKSHOP MATERIALS UNIT-1
 
CNS PPT.ppt
CNS PPT.pptCNS PPT.ppt
CNS PPT.ppt
 
Network-20210426203825.ppt
Network-20210426203825.pptNetwork-20210426203825.ppt
Network-20210426203825.ppt
 
unit5 graphs (DS).pptx
unit5 graphs (DS).pptxunit5 graphs (DS).pptx
unit5 graphs (DS).pptx
 
Network-20210426203825.ppt
Network-20210426203825.pptNetwork-20210426203825.ppt
Network-20210426203825.ppt
 
introdution-to-html.ppt
introdution-to-html.pptintrodution-to-html.ppt
introdution-to-html.ppt
 

Recently uploaded

Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

UNIT-4.ppt

  • 1. OPERATING SYSTEMS UNIT-IV Memory Management & Virtual Memory Management ADITYA ENGINEERING COLLEGE (A) By P.SRI LATHA Asst.Professor Dept of Computer Science and Engineering Aditya Engineering College(A) Surampalem.
  • 2. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Memory Management • Background • Swapping • Contiguous Memory Allocation • Segmentation • Paging • Structure of the Page Table
  • 3. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Objectives • To provide a detailed description of various ways of organizing memory hardware. • To discuss various memory-management techniques, including paging and segmentation.
  • 4. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background • Program must be brought (from disk) into memory and placed within a process for it to run. • Main memory and registers are only storage entities that a CPU can access directly. • The CPU fetches instructions from main memory according to the value of the program counter. • Typical instruction execution cycle – fetch instruction from memory, decode the instruction, operand fetch, possible storage of result in memory.
  • 5. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background (Cont.) • Memory unit only sees a stream of one of the following: – address + read requests (e.g., load memory location 20010 into register number 8) – address + data and write requests (e.g., store content of register 6 into memory location 1090) • Memory unit does not know how these addresses were generated. • Register access can be done in one CPU clock (or less).
  • 6. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background (Cont.) • Completing a memory access may take many cycles of the CPU clock. In such a case the processor needs to stall since it does not have the data required to complete the instruction it is executing. • Cache sits between main memory and CPU registers to deal with the “stall” issue. • Protection of memory is required to ensure correct operation: – User process cannot access OS memory – One user process cannot access the memory of another user process.
  • 7. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Physical Address • The physical address refers to a location in the memory. It allows access to data in the main memory. • A physical address is not directly accessible to the user program hence, a logical address needs to be mapped to it to make the address accessible. This mapping is done by the MMU.
  • 8. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Logical Address • A logical address or virtual address is an address that is generated by the CPU during program execution. • A logical address doesn't exist physically. The logical address is used as a reference to access the physical address. • This logical address combines with the base address generated by the MMU to form the physical address.
  • 9. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 • 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.
  • 10. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 mapping between logical and physical addresses
  • 11. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Memory Management unit(MMU) • The Memory Management Unit is a combination of 2 registers – • Base Register (Relocation Register) • Limit Register.
  • 12. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Hardware Address Protection
  • 13. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 • The binding is necessary to link the logical memory to the physical memory. To know where the program is stored is necessary in order to access it. The binding may be of three different types. • Compile Time Binding:Address where the program is stored is known at compile time. • Load Time Binding:Address is not known at compile time but known at loading of program i.e,before running. • Run Time Binding:Address is known at running of executable program. Address Binding
  • 14. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Multi step Processing of a User Program
  • 15. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Static Linking • Linker:A linker is a program that takes one or more object files generated by a compiler and combines them into a single executable file • Static linking: In static linking, the linker combines all necessary program modules into a single executable program. Some operating systems support only static linking, in which system language libraries are treated like any other object module.
  • 16. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Dynamic linking • In dynamic linking, “Stub” is included for each appropriate library routine reference. • A stub is a small piece of code. When the stub is executed, it checks whether the needed routine is already in memory or not. If not available then the program loads the routine into memory.
  • 17. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Schematic View of Swapping
  • 18. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Swapping (contd..) • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution • Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images • Roll out, roll in – 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
  • 19. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Contiguous Allocation • In Contiguous memory allocation which is a memory management technique, whenever there is a request by the user process for the memory then a single section of the contiguous memory block is given to that process according to its requirement.
  • 20. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Multiple-partition allocation – Degree of multiprogramming limited by number of partitions – Variable-partition sizes for efficiency (sized to a given process’ needs) – Hole – block of available memory; 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)
  • 21. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Memory allocation techniques First fit:- In the first fit, the first available free hole fulfills the requirement of the process allocated
  • 22. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Best fit:- In the best fit, allocate the smallest hole that is big enough to process requirements. For this, we search the entire list, unless the list is ordered by size.
  • 23. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Worst fit:-In the worst fit, allocate the largest available hole to process. This method produces the largest leftover hole.
  • 24. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Fragmentation • External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous • Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used.
  • 25. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Fragmentation (Cont.) • Reduce external fragmentation by compaction – Shuffle memory contents to place all free memory together in one large block – Compaction is possible only if relocation is dynamic, and is done at execution time
  • 26. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Segmentation • Memory-management scheme that supports user view of memory • A program is a collection of segments – A segment is a logical unit such as: main program procedure function method object local variables, global variables common block stack symbol table arrays
  • 27. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 User’s View of a Program
  • 28. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Logical View of Segmentation 1 3 2 4 1 4 2 3 user space physical memory space
  • 29. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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
  • 30. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Segmentation Hardware
  • 31. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Paging • Physical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available – Avoids external fragmentation – Avoids problem of varying sized memory chunks • Divide physical memory into fixed-sized blocks called frames – Size is power of 2, between 512 bytes and 16 Mbytes • 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
  • 32. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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
  • 33. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Paging Hardware
  • 34. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Paging Model of Logical and Physical Memory
  • 35. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Free Frames Before allocation After allocation
  • 36. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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)
  • 37. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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 • TLBs typically small (64 to 1,024 entries) • On a TLB miss, value is loaded into the TLB for faster access next time
  • 38. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Paging Hardware With TLB
  • 40. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Memory Protection • Memory protection 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. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Valid (v) or Invalid (i) Bit In A Page Table
  • 42. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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
  • 43. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Shared Pages Example
  • 44. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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
  • 46. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Two-Level Page-Table Scheme
  • 47. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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:
  • 48. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Address-Translation Scheme
  • 49. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Three-level Paging Scheme
  • 50. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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)
  • 51. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Hashed Page Table
  • 52. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 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
  • 53. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Inverted Page Table Architecture
  • 54. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Virtual Memory • Background • Demand Paging • Page Replacement • Thrashing
  • 55. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Objectives • To describe the benefits of a virtual memory system • To explain the concepts of demand paging, page-replacement algorithms, and allocation of page frames
  • 56. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background • Code needs to be in memory to execute, but entire program rarely used – Error code, unusual routines, large data structures • Entire program code not needed at same time • Consider ability to execute partially-loaded program – Program no longer constrained by limits of physical memory – Each program takes less memory while running -> more programs run at the same time • Increased CPU utilization and throughput with no increase in response time or turnaround time – Less I/O needed to load or swap programs into memory -> each user program runs faster
  • 57. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background (Cont.) • Virtual memory – separation of user logical memory from physical memory – Only part of the program needs to be in memory for execution – Logical address space can therefore be much larger than physical address space – Allows address spaces to be shared by several processes – Allows for more efficient process creation – More programs running concurrently – Less I/O needed to load or swap processes
  • 58. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Background (Cont.) • Virtual address space – logical view of how process is stored in memory – Usually start at address 0, contiguous addresses until end of space – Meanwhile, physical memory organized in page frames – MMU must map logical to physical • Virtual memory can be implemented via: – Demand paging – Demand segmentation
  • 59. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Virtual Memory That is Larger Than Physical Memory
  • 60. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Virtual-address Space  Usually design logical address space for stack to start at Max logical address and grow “down” while heap grows “up”  Maximizes address space use  Unused address space between the two is hole  No physical memory needed until heap or stack grows to a given new page  Enables sparse address spaces with holes left for growth, dynamically linked libraries, etc  System libraries shared via mapping into virtual address space  Shared memory by mapping pages read- write into virtual address space  Pages can be shared during fork(), speeding process creation
  • 61. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Shared Library Using Virtual Memory
  • 62. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Demand Paging • Could bring entire process into memory at load time • Or bring a page into memory only when it is needed – Less I/O needed, no unnecessary I/O – Less memory needed – Faster response – More users • Similar to paging system with swapping (diagram on right) • Page is needed  reference to it – invalid reference  abort – not-in-memory  bring to memory • Lazy swapper – never swaps a page into memory unless page will be needed – Swapper that deals with pages is a pager
  • 63. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Basic Concepts • With swapping, pager guesses which pages will be used before swapping out again • Instead, pager brings in only those pages into memory • How to determine that set of pages? – Need new MMU functionality to implement demand paging • If pages needed are already memory resident – No difference from non demand-paging • If page needed and not memory resident – Need to detect and load the page into memory from storage • Without changing program behavior • Without programmer needing to change code
  • 64. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Valid-Invalid Bit • With each page table entry a valid–invalid bit is associated (v  in-memory – memory resident, i  not- in-memory) • Initially valid–invalid bit is set to i on all entries • Example of a page table snapshot:
  • 65. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page Table When Some Pages Are Not in Main Memory
  • 66. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page Fault • If there is a reference to a page, first reference to that page will trap to operating system:page fault 1. Operating system looks at another table to decide: – Invalid reference  abort – Just not in memory 2. Find free frame 3. Swap page into frame via scheduled disk operation 4. Reset tables to indicate page now in memory Set validation bit = v 5. Restart the instruction that caused the page fault
  • 67. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Steps in Handling a Page Fault
  • 68. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Performance of Demand Paging • Stages in Demand Paging (worse case) 1. Trap to the operating system 2. Save the user registers and process state 3. Determine that the interrupt was a page fault 4. Check that the page reference was legal and determine the location of the page on the disk 5. Issue a read from the disk to a free frame: 1. Wait in a queue for this device until the read request is serviced 2. Wait for the device seek and/or latency time 3. Begin the transfer of the page to a free frame 6. While waiting, allocate the CPU to some other user 7. Receive an interrupt from the disk I/O subsystem (I/O completed) 8. Save the registers and process state for the other user 9. Determine that the interrupt was from the disk 10. Correct the page table and other tables to show page is now in memory 11. Wait for the CPU to be allocated to this process again 12. Restore the user registers, process state, and new page table, and then resume the interrupted instruction
  • 69. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Performance of Demand Paging (Cont.) • Three major activities – Service the interrupt – careful coding means just several hundred instructions needed – Read the page – lots of time – Restart the process – again just a small amount of time • Page Fault Rate 0  p  1 – if p = 0 no page faults – if p = 1, every reference is a fault • Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + swap page out + swap page in )
  • 70. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Copy-on-Write • Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory – If either process modifies a shared page, only then is the page copied • COW allows more efficient process creation as only modified pages are copied • In general, free pages are allocated from a pool of zero-fill-on-demand pages – Pool should always have free frames for fast demand page execution
  • 71. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Before Process 1 Modifies Page C
  • 72. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 After Process 1 Modifies Page C
  • 73. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 What Happens if There is no Free Frame? • Used up by process pages • Also in demand from the kernel, I/O buffers, etc • How much to allocate to each? • Page replacement – find some page in memory, but not really in use, page it out – Algorithm – terminate? swap out? replace the page? – Performance – want an algorithm which will result in minimum number of page faults • Same page may be brought into memory several times
  • 74. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page Replacement • Prevent over-allocation of memory by modifying page-fault service routine to include page replacement • Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk • Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory
  • 75. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Need For Page Replacement
  • 76. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Basic Page Replacement 1. Find the location of the desired page on disk 2. Find a free frame: - If there is a free frame, use it - If there is no free frame, use a page replacement algorithm to select a victim frame - Write victim frame to disk if dirty 3. Bring the desired page into the (newly) free frame; update the page and frame tables 4. Continue the process by restarting the instruction that caused the trap
  • 77. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page Replacement
  • 78. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page and Frame Replacement Algorithms • Frame-allocation algorithm determines – How many frames to give each process – Which frames to replace • Page-replacement algorithm – Want lowest page-fault rate on both first access and re-access • Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string – String is just page numbers, not full addresses – Repeated access to the same page does not cause a page fault – Results depend on number of frames available • In all our examples, the reference string of referenced page numbers is 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
  • 79. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Graph of Page Faults Versus The Number of Frames
  • 80. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 First-In-First-Out (FIFO) Algorithm • Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 • 3 frames (3 pages can be in memory at a time per process) Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5 – Adding more frames can cause more page faults! • Belady’s Anomaly • How to track ages of pages? – Just use a FIFO queue 15 page faults
  • 81. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 FIFO Illustrating Belady’s Anomaly
  • 82. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Optimal Algorithm • Replace page that will not be used for longest period of time – 9 is optimal for the example • How do you know this? – Can’t read the future
  • 83. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Least Recently Used (LRU) Algorithm • Use past knowledge rather than future • Replace page that has not been used in the most amount of time • Associate time of last use with each page
  • 84. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 LRU Algorithm (Cont.) • Counter implementation – Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter – When a page needs to be changed, look at the counters to find smallest value • Search through table needed • Stack implementation – Keep a stack of page numbers in a double link form: – Page referenced: • move it to the top • requires 6 pointers to be changed – But each update more expensive – No search for replacement • LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly
  • 85. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Use Of A Stack to Record Most Recent Page References
  • 86. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Counting Algorithms • Keep a counter of the number of references that have been made to each page – Not common • Lease Frequently Used (LFU) Algorithm: replaces page with smallest count • Most Frequently Used (MFU) Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used
  • 87. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Page-Buffering Algorithms • Keep a pool of free frames, always – Then frame available when needed, not found at fault time – Read page into free frame and select victim to evict and add to free pool – When convenient, evict victim • Possibly, keep list of modified pages – When backing store otherwise idle, write pages there and set to non-dirty • Possibly, keep free frame contents intact and note what is in them – If referenced again before reused, no need to load contents again from disk – Generally useful to reduce penalty if wrong victim frame selected
  • 88. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Allocation of Frames • Each process needs minimum number of frames • Example: IBM 370 – 6 pages to handle SS MOVE instruction: – instruction is 6 bytes, might span 2 pages – 2 pages to handle from – 2 pages to handle to • Maximum of course is total frames in the system • Two major allocation schemes – fixed allocation – priority allocation
  • 89. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Fixed Allocation • Equal allocation – For example, if there are 100 frames (after allocating frames for the OS) and 5 processes, give each process 20 frames – Keep some as free frame buffer pool • Proportional allocation – Allocate according to the size of process
  • 90. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Priority Allocation • Use a proportional allocation scheme using priorities rather than size • If process Pi generates a page fault, – select for replacement one of its frames – select for replacement a frame from a process with lower priority number
  • 91. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Global vs. Local Allocation • Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another – But then process execution time can vary greatly – But greater throughput so more common • Local replacement – each process selects from only its own set of allocated frames – More consistent per-process performance – But possibly underutilized memory
  • 92. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Thrashing • If a process does not have “enough” pages, the page- fault rate is very high – Page fault to get page – Replace existing frame – But quickly need replaced frame back – This leads to: • Low CPU utilization • Operating system thinking that it needs to increase the degree of multiprogramming • Another process added to the system • Thrashing  a process is busy swapping pages in and out
  • 93. Aditya Engineering College(A) Operating Systems P.SRI LATHA Saturday, 07 October 2023 Thrashing (Cont.)