0% found this document useful (0 votes)
34 views68 pages

Osc Unit 5

This document discusses memory management techniques used in operating systems, including logical and physical address spaces, memory allocation strategies like contiguous allocation and paging, and issues related to fragmentation. Paging is described as a technique that divides both physical and logical memory into fixed-size blocks to allow non-contiguous allocation and reduce external fragmentation.

Uploaded by

SRH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views68 pages

Osc Unit 5

This document discusses memory management techniques used in operating systems, including logical and physical address spaces, memory allocation strategies like contiguous allocation and paging, and issues related to fragmentation. Paging is described as a technique that divides both physical and logical memory into fixed-size blocks to allow non-contiguous allocation and reduce external fragmentation.

Uploaded by

SRH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 68

Chapter 8: Main Memory

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne


Chapter 8: Memory Management
 Memory Management: Basic concept, Logical and
Physical address map,
 Memory allocation: Contiguous Memory allocation –
Fixed and variable partition
 Internal and External fragmentation and Compaction;
Paging: Principle of operation – Page allocation
 Hardware support for paging, Protection and sharing,
Disadvantages of paging.

Operating System Concepts – 9th Edition 8.2 Silberschatz, Galvin and Gagne
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 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

Operating System Concepts – 9th Edition 8.3 Silberschatz, Galvin and Gagne
Memory-Management Unit (MMU)
 Hardware device that at run time maps virtual to
physical address
 To start, consider simple scheme where the 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
 The user program deals with logical addresses; it
never sees the real physical addresses

Operating System Concepts – 9th Edition 8.4 Silberschatz, Galvin and Gagne
Dynamic relocation using a relocation register

 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
 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
Operating System Concepts –dynamic
9th Edition loading 8.6 Silberschatz, Galvin and Gagne
Swapping
 A process can be swapped temporarily out of memory to a
backing store, and then brought back into memory for
continued execution
 Total physical memory space of processes can exceed
physical memory
 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
 System maintains a ready queue of ready-to-run processes
which have memory images on disk

Operating System Concepts – 9th Edition 8.7 Silberschatz, Galvin and Gagne
Schematic View of Swapping

Operating System Concepts – 9th Edition 8.9 Silberschatz, Galvin and Gagne
4.Limited resource, must allocate efficiently
Contiguous Allocation
 Main memory must support both OS and user
processes
 Limited resource, must allocate efficiently
 Contiguous allocation is one early method
 Main memory usually divide into two partitions:
 Resident operating system, usually held in low
memory with interrupt vector
 User processes usually , held in high memory
 Each process contained in single contiguous
section of memory

Operating System Concepts – 9th Edition 8.12 Silberschatz, Galvin and Gagne
Multiple-partition allocation
 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)

Operating System Concepts – 9th Edition 8.14 Silberschatz, Galvin and Gagne
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?

 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
First-fit and best-fit are better than worst-fit in terms of
speed and storage utilization

Operating System Concepts – 9th Edition 8.16 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 8.18 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne
Paging :
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

Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne
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 page
unitnumber page offset
p d
m -n n

 For given logical address space 2m and page size 2n

Operating System Concepts – 9th Edition 8.23 Silberschatz, Galvin and Gagne
Paging Hardware

Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne
Paging Model of Logical and Physical Memory

Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne
Free Frames

Before allocation After allocation

Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne
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)

Operating System Concepts – 9th Edition 8.27 Silberschatz, Galvin and Gagne
Paging Hardware With TLB

Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne
User’s View of a Program

Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne
Logical View of Segmentation

4
1

3 2
4

user space physical memory space

Operating System Concepts – 9th Edition 8.34 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne
Segmentation Hardware

Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne
Chapter 9: Virtual Memory

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne


Chapter 9: Virtual Memory
 Virtual Memory: Basics of Virtual Memory – Hardware and
control structures –
 Locality of reference, Page fault , Working Set , Dirty
page/Dirty bit – Demand paging,
 Page Replacement algorithms: Optimal, First in First Out
(FIFO), Second Chance (SC),
 Not recently used (NRU) and Least Recently used (LRU).

Operating System Concepts – 9th Edition 9.40 Silberschatz, Galvin and Gagne
Background
 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

Operating System Concepts – 9th Edition 9.41 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 9.42 Silberschatz, Galvin and Gagne
Virtual Memory That is Larger Than Physical Memory

Operating System Concepts – 9th Edition 9.43 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 9.44 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 9.45 Silberschatz, Galvin and Gagne
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:

 During MMU address translation, if valid–invalid bit in


page table entry is i  page fault
Operating System Concepts – 9th Edition 9.46 Silberschatz, Galvin and Gagne
Page Table When Some Pages Are Not in Main Memory

Operating System Concepts – 9th Edition 9.47 Silberschatz, Galvin and Gagne
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
 Find free frame
 Swap page into frame via scheduled disk operation
 Reset tables to indicate page now in memory
Set validation bit = v
 Restart the instruction that caused the page fault

Operating System Concepts – 9th Edition 9.48 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 9.49 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition 9.50 Silberschatz, Galvin and Gagne
Working-Set Model
   working-set window  a fixed number of page references
Example: 10,000 instructions
 WSSi (working set of Process Pi) =
total number of pages referenced in the most recent  (varies
in time)
 if  too small will not encompass entire locality
 if  too large will encompass several localities
 if  =   will encompass entire program
 D =  WSSi  total demand frames
 Approximation of locality
 if D > m  Thrashing
 Policy if D > m, then suspend or swap out one of the
processes

Operating System Concepts – 9th Edition 9.51 Silberschatz, Galvin and Gagne
Keeping Track of the Working Set
 Approximate with interval timer + a reference bit
 Example:  = 10,000
 Timer interrupts after every 5000 time units
 Keep in memory 2 bits for each page
 Whenever a timer interrupts copy and sets the values
of all reference bits to 0
 If one of the bits in memory = 1  page in working set
 Why is this not completely accurate?
 Improvement = 10 bits and interrupt every 1000 time
units

Operating System Concepts – 9th Edition 9.52 Silberschatz, Galvin and Gagne
Page-Fault Frequency
 More direct approach than WSS
 Establish “acceptable” page-fault frequency (PFF)
rate and use local replacement policy
 If actual rate too low, process loses frame
 If actual rate too high, process gains frame

Operating System Concepts – 9th Edition 9.53 Silberschatz, Galvin and Gagne
Working Sets and Page Fault Rates
 Direct relationship between working set of a
process and its page-fault rate
 Working set changes over time
 Peaks and valleys over time

Operating System Concepts – 9th Edition 9.54 Silberschatz, Galvin and Gagne
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

Operating System Concepts – 9th Edition 9.55 Silberschatz, Galvin and Gagne
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

Note now potentially 2 page transfers for page fault – increasing


EAT

Operating System Concepts – 9th Edition 9.56 Silberschatz, Galvin and Gagne
Page Replacement

Operating System Concepts – 9th Edition 9.57 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition 9.58 Silberschatz, Galvin and Gagne
•The oldest page in memory is the one replaced.

Belady's Anomaly is a real-world phenomenon that can occur in certain page replacement
algorithms, leading to unexpected behavior where increasing the number of page frames can
result in more page faults. It has implications for system performance and resource
allocation, and while it cannot be completely eliminated, it can be minimized by using
certain algorithms and careful experimentation.

Operating System Concepts – 9th Edition 9.59 Silberschatz, Galvin and Gagne
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)

15 page faults

 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
Operating System Concepts – 9th Edition 9.60 Silberschatz, Galvin and Gagne
FIFO Illustrating Belady’s Anomaly

Operating System Concepts – 9th Edition 9.61 Silberschatz, Galvin and Gagne
Principle : Look for Right most element & use FIFO at the end if no element exist

•Replaces the page that will not be used for the longest duration in the future.
•Provides the best performance, but it is not practical as it requires future
knowledge of page references
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
 Used for measuring how well your algorithm performs

Operating System Concepts – 9th Edition 9.63 Silberschatz, Galvin and Gagne
Principle : Look for left most element

•Replaces the page that has not been used for the longest period of time.
Provides good performance, but it requires a significant overhead to implement and is not
always practical
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

 12 faults – better than FIFO but worse than OPT


 Generally good algorithm and frequently used
 But how to implement?

Operating System Concepts – 9th Edition 9.65 Silberschatz, Galvin and Gagne
Use Of A Stack to Record Most Recent Page References

Operating System Concepts – 9th Edition 9.66 Silberschatz, Galvin and Gagne
•Second Chance Algorithm: Based on the FIFO algorithm, but it gives a second chance
to a page before replacing it.
•It is more efficient than FIFO as it considers the recent usage of pages
•Not Recently Used (NRU): NRU is a page replacement algorithm used
in operating systems to determine which pages to replace.
•It divides pages into four categories based on their usage history and
selects a page for replacement from the category with the lowest priority.

NRU replaces a page from the lowest category, giving priority to pages that
have not been recently used.

Class 3 is given highest priority followed by class 2 , class 1 and last class 0

You might also like