Silberschatz, Galvin and Gagne ©2013 Operating System Concepts - 9 Edition

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

Module 4

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Module 4

● Concept of address spaces

● Swapping

● Contiguous Memory Allocation

● Fixed and variable partitions

● Segmentation

● Paging

● Virtual Memory

● Demand Paging

● Page Replacement Algorithms

Operating System Concepts – 9th Edition 8.2 Silberschatz, Galvin and Gagne ©2013
Background
● The memory consist of a large array or group of words or bytes, each with
its own address.
● The primary motive of a computer system is to execute programs.
● These programs, along with the information they access, should be in the
main memory during execution.
● The CPU fetches instructions from memory according to the value of the
program counter.

Operating System Concepts – 9th Edition 8.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 8.4 Silberschatz, Galvin and Gagne ©2013
● Each process has a separate memory space(memory address).
● Separate per-process memory space protects the processes from each other and is
fundamental to have multiple processes loaded in main memory for concurrent
execution.
● To separate the memory spaces, we need the ability to determine the range of legal
addresses that the process may access and to ensure that the process can access
only these legal addresses.
● This protection can be provided by using two registers
● Base register – holds the smallest legal physical memory address
● Limit register – size of the range
● Hardware address protection with base and limit registers

Operating System Concepts – 9th Edition 8.6 Silberschatz, Galvin and Gagne ©2013
Logical vs. Physical Address Space

● Logical address
● A logical address is generated by CPU while a program is running.
● Since a logical address does not physically exists it is also known as a
virtual address.
● This address is used as a reference by the CPU to access the actual
physical memory location.
● There is a hardware device called Memory-Management Unit is used
for mapping logical address to its corresponding physical address.
● The user program generates the logical address and believes that the
program is running in this logical address space, but the program
needs physical memory for its execution, therefore, the logical address
must be mapped to the physical address by the MMU before the
addresses are used.
● Physical address
● A physical address identifies the physical location of a specific data
element in memory.

Operating System Concepts – 9th Edition 8.7 Silberschatz, Galvin and Gagne ©2013
MMU

Operating System Concepts – 9th Edition 8.8 Silberschatz, Galvin and Gagne ©2013
Swapping
❑ Swapping is a mechanism in which a process can be swapped temporarily
out of main memory to secondary storage and make that memory available
to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory.
❑ The system maintains a ready queue consisting of all processes who are
ready to run
❑ The dispatcher checks whether the next process in the queue is in memory
or not.

Operating System Concepts – 9th Edition 8.9 Silberschatz, Galvin and Gagne ©2013
Memory Allocation Techniques
● Contiguous Allocation
● Non-contiguous Allocation

Operating System Concepts – 9th Edition 8.10 Silberschatz, Galvin and Gagne ©2013
Contiguous Memory Allocation
● Contiguous memory allocation is a memory allocation method that allocates
a single contiguous section of memory to a process.

● Advantage:
● Less access time
● Disadvantage
● External fragmentation

Operating System Concepts – 9th Edition 8.11 Silberschatz, Galvin and Gagne ©2013
Non-Contiguous Memory Allocation
● Allocates the memory space present in different locations to the process as per its
requirement
● Contrary to contiguous allocation

● Advantage
● No External Fragmentation
● Disadvantage
● More access time compared to contiguous allocation

Operating System Concepts – 9th Edition 8.12 Silberschatz, Galvin and Gagne ©2013
Contiguous Memory Allocation

Operating System Concepts – 9th Edition 8.13 Silberschatz, Galvin and Gagne ©2013
Fixed Size Partition
● Also known as Static partitioning.
● In this scheme, the system divides the memory into fixed-size partitions.
● The partitions may or may not be the same size.
● The size of each partition is fixed and it cannot be changed.
● In this partition scheme, each partition is allowed to contain exactly one
process.

Operating System Concepts – 9th Edition 8.14 Silberschatz, Galvin and Gagne ©2013
● Problem with fixed sized partition
● Internal Fragmentation

Operating System Concepts – 9th Edition 8.15 Silberschatz, Galvin and Gagne ©2013
Variable Partition
● Also called as Dynamic Partition
● It performs the allocation dynamically, there is no predefined partition.
● When a process arrives, a partition of size equal to the size of process is created.
● Then that partition is allocated to the process.

● No internal fragmentation

Operating System Concepts – 9th Edition 8.16 Silberschatz, Galvin and Gagne ©2013
Fragmentation
● Internal Fragmentation
● allocated memory may be slightly larger than requested memory; this size
difference is memory internal to a partition, but not being used

● External Fragmentation
● total memory space exists to satisfy a request, but it is not contiguous

Operating System Concepts – 9th Edition 8.17 Silberschatz, Galvin and Gagne ©2013
Partition Allocation Strategies

● Used to decide which hole is to be allocated to the arrived process.


● The free blocks of memory are known as holes

● Algorithms/Startegies:
● FIRST Fit
● BEST fit
● WORST Fit

Operating System Concepts – 9th Edition 8.18 Silberschatz, Galvin and Gagne ©2013
First Fit
❑ The first hole that is found to be large enough for a process to accommodate is
selected.

Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne ©2013
Best Fit
❑ The smallest hole that is large enough for the process to accommodate is
selected from the list of free holes.

Operating System Concepts – 9th Edition 8.20 Silberschatz, Galvin and Gagne ©2013
Worst Fit
❑ The largest hole among the free holes is selected.

Operating System Concepts – 9th Edition 8.21 Silberschatz, Galvin and Gagne ©2013
Non-Contiguous Memory Allocation
● Allocates the memory space present in different locations to the process as per its
requirement
● Contrary to contiguous allocation

● Approaches
● Segmentation
● Paging

Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne ©2013
Segmentation
● Non-contiguous memory allocation method
● 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 etc
● User view of a program

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

Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne ©2013
Segmentation Hardware/Architecture
● Logical address consists of a two tuple:
<segment-number, offset>

● Segment table – maps two-dimensional logical addresses into one


dimensional physical address ;
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

Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne ©2013
Segmentation Hardware

Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne ©2013
Paging
● Non-contiguous memory allocation
● Divide physical memory(main memory) into fixed-sized blocks called
frames
● Size is power of 2, 512 bytes and 16 Mbytes

● Divide logical memory(secondary memory) into blocks of same size called


pages
● Page size = frame size
● This technique keep track of all free frames

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

Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne ©2013
Paging Hardware

Operating System Concepts – 9th Edition 8.29 Silberschatz, Galvin and Gagne ©2013
Paging Example

Physical address =
frame number * page size + offset

Operating System Concepts – 9th Edition 8.30 Silberschatz, Galvin and Gagne ©2013
Paging Hardware With TLB
❑ TLB – Translation Lookaside Buffer
❑ Special cache contains page table entries that have been most
recently used.

Operating System Concepts – 9th Edition 8.31 Silberschatz, Galvin and Gagne ©2013
Steps in TLB hit:

1.CPU generates logical address.

2.It is checked in TLB (present).

3.Corresponding frame number is retrieved, combining page number and


offset tells where in the main memory page lies.

Steps in TLB miss:

1.CPU generates logical address.

2.It is checked in TLB (not present).

3.Now the page number is matched to page table residing in main memory

4.Corresponding frame number is retrieved, combining page number and offset


tells where in the main memory page lies.

5.The TLB is updated with new PTE (if space is not there, one of the
replacement technique).
Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne ©2013
Effective Access Time
● Effective access time(EAT) = h*m + (1-h)*2m
where, h = hit ratio of TLB
m = Memory access time

● If hit ratio is 80%, memory access time is 100 nanosecond,


EAT = 0.80 x 100 + 0.20 x 200 = 120ns
● If hit ratio is 99%, memory access time is 100 nanosecond,
EAT = 0.99 x 100 + 0.01 x 200 = 101ns

Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne ©2013
Virtual Memory
● Virtual Memory is a storage scheme that provides user an illusion of
having a very big main memory.
● This is done by treating a part of secondary memory as the main
memory.
● A computer can address more memory than the amount physically
installed on the system. This extra memory is actually called virtual
memory

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

Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne ©2013
Virtual Memory That is Larger Than Physical Memory

Operating System Concepts – 9th Edition 8.37 Silberschatz, Galvin and Gagne ©2013
Virtual Address Space
● Virtual address space of a process refers to the
logical(virtual) 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
● Enables sparse address spaces with holes left for
growth, dynamically linked libraries, etc

Operating System Concepts – 9th Edition 8.38 Silberschatz, Galvin and Gagne ©2013
Shared Library Using Virtual Memory
❑ In addition to separating logical memory from physical memory,
virtual memory allows files and memory to be shared by 2 or more
processes through page sharing.

Operating System Concepts – 9th Edition 8.39 Silberschatz, Galvin and Gagne ©2013
Demand Paging

● Pages are loaded only when they are


demanded during program execution.

● A demand paging system is similar to a


paging system with swapping.

● Lazy swapper – never swaps a page into


memory unless page will be needed

Operating System Concepts – 9th Edition 8.40 Silberschatz, Galvin and Gagne ©2013
Requirement – 5, 2, 4, 6, 1, 3

Operating System Concepts – 9th Edition 8.41 Silberschatz, Galvin and Gagne ©2013
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 8.42 Silberschatz, Galvin and Gagne ©2013
Page Table When Some Pages Are Not in Main Memory

Operating System Concepts – 9th Edition 8.43 Silberschatz, Galvin and Gagne ©2013
Steps in Handling a Page Fault

❑ A page fault occurs when a program attempts to access data or code


that is in its address space, but is not currently located in the system
RAM.

Operating System Concepts – 9th Edition 8.44 Silberschatz, Galvin and Gagne ©2013
Page Replacement
● Page replacement is a process of swapping out an existing page from the frame of a
main memory and replacing it with the required page.

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

Operating System Concepts – 9th Edition 8.49 Silberschatz, Galvin and Gagne ©2013
Need For Page Replacement

Operating System Concepts – 9th Edition 8.50 Silberschatz, Galvin and Gagne ©2013
Page Replacement Algorithms
● Page Replacement Algorithms:
● Page replacement algorithms help to decide which page must be swapped out
from the main memory to create a room for the incoming page.

● Algorithms:
● FIFO Page Replacement Algorithm
● LRU Page Replacement Algorithm
● Optimal Page Replacement Algorithm

❑ A good page replacement algorithm is one that minimizes the number of page
faults

Operating System Concepts – 9th Edition 8.51 Silberschatz, Galvin and Gagne ©2013
FIFO Page Replacement Algorithm
• Works on the principle of “First in First out“.
• It replaces the oldest page that has been present in the main memory
for the longest time.
• Simplest Algorithm
• In this algorithm, the operating system keeps track of all pages
in the memory in a queue, the oldest page is in the front of the
queue.
• When a page needs to be replaced page in the front of the
queue is selected for removal.

Operating System Concepts – 9th Edition 8.52 Silberschatz, Galvin and Gagne ©2013
● Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.

Operating System Concepts – 9th Edition 8.53 Silberschatz, Galvin and Gagne ©2013
● Belady’s anomaly
● Bélády’s anomaly is the name given to the phenomenon where
increasing the number of page frames results in an increase in the
number of page faults for a given memory access pattern.
● Beladys anomaly can occur in FIFO page replacement algorithm.
● For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2,
1, 0, 4 and 3 slots, we get 9 total page faults, but if we increase
slots to 4, we get 10 page faults.

Operating System Concepts – 9th Edition 8.54 Silberschatz, Galvin and Gagne ©2013
Eg:

Operating System Concepts – 9th Edition 8.55 Silberschatz, Galvin and Gagne ©2013
LRU Page Replacement Algorithm
• Least Recently Used
• In this algorithm page will be replaced which is least recently
used
• That is, it replaces the page that has not been referred by the CPU for
the longest time

Operating System Concepts – 9th Edition 8.56 Silberschatz, Galvin and Gagne ©2013
● Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3,
2 with 4 page frames. Find number of page faults.

Operating System Concepts – 9th Edition 8.57 Silberschatz, Galvin and Gagne ©2013
Optimal Page Replacement Algorithm
● In this algorithm, pages are replaced which would not be used for the
longest duration of time in the future.
● Although it can not be practically implementable but it can be used as a
benchmark. Other algorithms are compared to this in terms of optimality.

Operating System Concepts – 9th Edition 8.58 Silberschatz, Galvin and Gagne ©2013
● Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4
page frame. Find number of page fault.

Operating System Concepts – 9th Edition 8.59 Silberschatz, Galvin and Gagne ©2013
Thrashing
● In case, if the page fault and swapping happens very frequently at a higher
rate, then the operating system has to spend more time swapping these
pages.
● This state in the operating system is termed thrashing.
● Because of thrashing the CPU utilization is going to be reduced.

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

You might also like