More Related Content Similar to operating systems module 4 engineering.pptx (20) operating systems module 4 engineering.pptx2. 8.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● Concept of address spaces
● Swapping
● Contiguous Memory Allocation
● Fixed and variable partitions
● Segmentation
● Paging
● Virtual Memory
● Demand Paging
● Page Replacement Algorithms
Module 4
3. 8.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
5. ● 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
6. 8.6 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● Hardware address protection with base and limit registers
7. 8.7 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
9. 8.9 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
10. 8.10 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Memory Allocation Techniques
● Contiguous Allocation
● Non-contiguous Allocation
11. 8.11 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
12. 8.12 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
14. 8.14 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
15. 8.15 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● Problem with fixed sized partition
● Internal Fragmentation
16. 8.16 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
17. 8.17 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
18. 8.18 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● 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
Partition Allocation Strategies
19. 8.19 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
First Fit
❑ The first hole that is found to be large enough for a process to
accommodate is selected.
20. 8.20 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Best Fit
❑ The smallest hole that is large enough for the process to
accommodate is selected from the list of free holes.
21. 8.21 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Worst Fit
❑ The largest hole among the free holes is selected.
22. 8.22 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
23. 8.23 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
25. 8.25 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
27. 8.27 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
28. 8.28 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Paging Model of Logical and Physical Memory
30. 8.30 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Paging Example
Physical address =
frame number * page size + offset
31. 8.31 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Paging Hardware With TLB
❑ TLB – Translation Lookaside Buffer
❑ Special cache contains page table entries that have been most
recently used.
32. 8.32 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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).
33. 8.33 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● 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
Effective Access Time
34. 8.34 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
35. 8.35 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
37. 8.37 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Virtual Memory That is Larger Than Physical Memory
38. 8.38 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
39. 8.39 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
40. 8.40 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
42. 8.42 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
43. 8.43 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Page Table When Some Pages Are Not in Main Memory
44. 8.44 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
45. 8.48 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
46. 8.49 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
1. Bring the desired page into the (newly) free frame; update the page
and frame tables
2. Continue the process by restarting the instruction that caused the trap
48. 8.51 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
49. 8.52 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
50. 8.53 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.
51. 8.54 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● 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.
53. 8.56 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
54. 8.57 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● 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.
55. 8.58 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.
56. 8.59 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
● 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.
57. 8.60 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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.