0% found this document useful (0 votes)
4 views

OS Unit 4 Virtual Memory

Unit 7 discusses virtual memory, a memory management feature that allows systems to use disk storage to compensate for limited physical memory, enabling larger programs to run simultaneously. It covers demand paging, page replacement strategies, and various algorithms for managing memory, including FIFO and Optimal replacement. Additionally, it addresses challenges like thrashing and the complexities of implementing effective page replacement algorithms.

Uploaded by

mohitpatil2810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OS Unit 4 Virtual Memory

Unit 7 discusses virtual memory, a memory management feature that allows systems to use disk storage to compensate for limited physical memory, enabling larger programs to run simultaneously. It covers demand paging, page replacement strategies, and various algorithms for managing memory, including FIFO and Optimal replacement. Additionally, it addresses challenges like thrashing and the complexities of implementing effective page replacement algorithms.

Uploaded by

mohitpatil2810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Contents

Unit 7: Virtual Memory .......................................................................................................................... 2


7.1 Background ................................................................................................................................... 2
7.2 Demand Paging ............................................................................................................................. 4
7.3 Page Replacement ......................................................................................................................... 6
7.4 Page Replacement Algorithms ...................................................................................................... 7
7.4.1 What if the algorithm is not optimal? .................................................................................... 8
4.7.2 Types of Page Replacement Algorithm ................................................................................. 8
7.5 Allocation of frames ................................................................................................................... 11
7.5.1 Frame allocation algorithm .................................................................................................. 12
7.5.2 Global versus Local Allocation –......................................................................................... 12
7.6 Thrashing .................................................................................................................................... 13
7.6.1 Reasons for Thrashing ......................................................................................................... 14
7.6.2 Methods to Handle Thrashing .............................................................................................. 14
Unit 7: Virtual Memory

7.1 Background
 Virtual memory is a memory management functionality of an operating system (OS)
that utilize hardware and software to enable a computer to make up for physical
memory deficiencies by incidentally moving data from random access memory
(RAM) to disk storage.
 Virtual address space is expanded utilizing active memory in RAM and inactive
memory in hard disk drives (HDDs) to frame contiguous addresses that hold both the
application and its data.
 Virtual Memory is a storage allocation plot in which secondary memory can be
tended to just as it were a piece of main memory.
 The addresses a program may use to reference memory are recognized from the
addresses the memory system uses to distinguish physical storage locales, and
program created addresses are made an interpretation of consequently to the relating
machine addresses.
 The size of virtual storage is constrained by the addressing plan of the computer
system and measure of secondary memory is accessible not by the genuine number of
the main storage locations.
 Virtual memory was created when physical memory - the installed RAM - was costly.
 Computers have a limited measure of RAM, so memory can run out, particularly
when numerous programs keep running simultaneously.
 A system utilizing virtual memory utilizes a segment of the hard drive to copy RAM.
With virtual memory, a system can stack bigger programs or various programs
running simultaneously, enabling everyone to work as though it has vast memory and
without obtaining more RAM.
 While duplicating virtual memory into physical memory, the OS partitions memory
into page files or swap files with a fixed number of addresses. Each page is put away
on a disk and when the page is required, the OS copies it from the disk to fundamental
memory and makes an interpretation of the virtual addresses into real addresses.
 Current microprocessors expected for universally useful use, a memory management
unit, or MMU, is incorporated with the hardware.
 The MMU's main responsibility is to make an interpretation of virtual address into
physical address. A fundamental model is given underneath −
 It is a procedure that is actualized utilizing both hardware and software. It maps
memory addressed utilized by a program, called virtual addresses computers, into
physical addresses in c memory.
1. All memory references inside a process are logical addresses that are
powerfully converted into physical addresses at run time. This implies a
process can be swapped all through fundamental memory with the end goal
that it involves better places in primary memory at various times over the span
of execution.
2. A process might be broken into number of pieces and these pieces need not be
constantly situated in the main memory during execution. The blend of
dynamic run-time address interpretation and utilization of page or portion
table allows this.
 On the off chance that these attributes are available, at that point, it isn't essential that
every one of the pages or portions are available in the main memory during execution.
This implies the required pages should be stacked into memory at whatever point
required.
 Virtual memory is designed utilizing Demand Paging or Demand Segmentation.
7.2 Demand Paging
 As per the idea of Virtual Memory, to execute some process, just a piece of the
process should be available in the main memory which implies that a couple of pages
may be available in the main memory whenever.
 The difficult task is to decide which pages should be kept in the main memory and
which should be kept in the secondary memory, because of the fact that we can't state
ahead of time that a process will require a specific page at specific time.
 Subsequently, to beat this issue, there is an idea called Demand Paging is presented. It
proposes keeping all pages of the frames in the secondary memory until they are
required. That means, it says that don't stack any page in the main memory until it is
required.
 Demand paging says that pages should possibly be brought into memory if the
executing process requests them. This is frequently known as lazy evaluation as just
those pages requested by the process are swapped from secondary storage to main
memory.

 On the other hand in pure swapping, where all memory for a process is swapped from
secondary storage to main memory during the process start execution.
 To implement this process needs a page table is utilized. The page table maps logical
memory to physical memory.
 The page table uses a bitwise administrator to stamp if a page is valid or invalid. A
valid page is one that is present in main memory. An invalid page is one that is
present in secondary memory.
 At the point when a process attempts to get to a page, following steps are followed as
shown in figure below:

1. In the event that CPU attempt to call a page that is as of now not accessible in
the main memory, it creates an interrupt showing memory access fault.
2. The OS places the interrupted process in a blocking state. For the execution to
continue the OS must call the required page into the memory.
3. The OS will scan for the required page in the logical address space.
4. The required page will be brought from logical address space to physical
address space. The page replacement algorithms are utilized for deciding
which page is to be replace with the new page in physical address space.
5. Updation will be made to the page table accordingly.
6. The sign will be sent to the CPU to proceed with the program execution and it
will put the process again into ready state.

 Henceforth at whatever point a page fault happens these steps are performed by the
operating system and the required page is brought into memory.

Advantages
 Demand paging, instead of loading all pages right away:
 Just loads pages that are requested by the executing process.
 As there is more space in main memory, more processes can be stacked, decreasing
the context switching time, which uses a lot of resources.
 Less loading latency happens at program startup, as less data is gotten to from
secondary storage and less data is brought into main memory.
 As main memory is costly contrasted with secondary memory, this method helps
altogether decrease the bill of material (BOM) cost in advanced cells for instance.
Symbian OS had this element.

Disadvantage
 Individual programs face additional latency when they access page at first time.
 Minimal effort, low-control implanted systems might not have a memory
management unit that supports page replacement.
 Memory management with page replacement algorithms turns out to be somewhat
increasingly intricate.
 Conceivable security dangers, including weakness to timing attacks.
 Thrashing which may happen because of repeated page faults.

7.3 Page Replacement


 Page replacement adopts the accompanying strategy. In the event that no frame is
free, we find one that isn't as of now being utilized and free it.
 We can free a frame by writing its substance to swap space and changing the page
table (and every single other table) to show that the page is no longer in memory
(Figure below).
 We would now be able to utilize the freed frame to hold the page for which the
process faulted.
 We adjust the page-fault administration routine to incorporate page replacement:
1. Discover the location of the ideal page on the disk.
2. Locate a free frame:
a. On the off chance that there is a free frame, use it.
b. On the off chance that there is no free frame, utilize a page-
replacement algorithm to choose a victim frame.
c. Write the victim frame to the disk; change the page and frame tables
likewise.
3. Read the ideal page into the recently freed frame; change the page and frame
tables.
4. Restart the user process.
 Notice that, if no frames are free, two page moves (one out and one in) are required.
This circumstance adequately duplicates the page-fault service time and expands the
successful access time as needs be.
 Page replacement is fundamental to demand paging. It finishes the partition between
logical memory and physical memory.
 With this component, a huge virtual memory can be given to programmers on a littler
physical memory.
 With no demand paging, user addresses are mapped into physical addresses, so the
two arrangements of addresses can be unique.
 Every one of the pages of a process still should be in physical memory, in any case.
 In an operating system that utilizes paging for memory management, a page
replacement algorithm is expected to choose which page should be replaced when
new page comes in.

Page Replacement

7.4 Page Replacement Algorithms


 Page replacement algorithms are the procedures utilizing which an Operating System
chooses which memory pages to swap out, write to disk when a page of memory
should be allocated.
 Paging happens at whatever point a page fault happens and a free page can't be
utilized for allocation reason accounting to reason that pages are not available or the
quantity of free pages is lower than required pages.
 At the point when the page that was chosen for replacement and was paged out, is
referenced once more, it needs to read in from disk, and this requires for I/O
completion.
 This process decides the nature of the page replacement algorithm: the lesser the time
waiting for page-ins, the better is the algorithm.
 A page replacement algorithm takes a gander at the restricted data about getting to the
pages given by hardware, and attempts to choose which pages ought to be replaced to
limit the complete number of page misses, while offsetting it with the expenses of
primary storage and processor time of the algorithm itself.
 There are a wide range of page replacement algorithms. We assess a algorithms by
running it on a specific string of memory reference and registering the quantity of
page issues,
 There are two main parts of virtual memory, Frame allocation and Page Replacement.
It is imperative to have the ideal frame allocation and page replacement algorithm.
Frame allocation is about what number of frames are to be designated to the process
while the page replacement is tied in with deciding the page number which should be
supplanted so as to make space for the mentioned page.

7.4.1 What if the algorithm is not optimal?


1. In the event that the quantity of frames which are dispensed to a process isn't adequate
or exact at that point there can be an issue of thrashing. Because of the absence of
frames, a large portion of the pages will reside in the main memory and consequently
more page faults will happen.
Nonetheless, on the off chance that OS allots more frames to the process, at that point
there can be internal discontinuity.
2. On the off chance that the page replacement orithmalgorithm isn't ideal, at that point
there will likewise be the issue of thrashing. On the off chance that the quantity of
pages that are supplanted by the mentioned pages will be referred sooner rather than
later at that point there will be progressively number of swap-in and swap-out and
along these lines the OS needs to perform more replacements then common which
causes execution inadequacy.
3. In this manner, the assignment of an ideal page replacement calculation is to pick the
page which can restrict the thrashing.

4.7.2 Types of Page Replacement Algorithm


 There are different page replacement algorithms. Every algorithm has an alternate
strategy by which the pages can be replaced.

4.7.2.1 First in First out (FIFO) –


 This is the least difficult page replacement algorithm. In this algorithm, the operating
system monitors all pages in the memory in a queue, the most oldest page is in the
front of the line. At the point when a page should be replaced page in the front of the
queue is chosen for removal.
 Example Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames. Find
number of page faults.
 At first all slots are vacant, so when 1, 3, 0 came they are distributed to the unfilled
spaces — > 3 Page Faults.
 At the point when 3 comes, it is in memory so — > 0 Page Faults.
 At that point 5 comes, it isn't available in memory so it replaces the most oldest page
space i.e 1. — >1 Page Fault.
 6 comes, it is likewise not available in memory so it replaces the most established
page space i.e 3 — >1 Page Fault.
 At long last when 3 come it isn't avilable so it replaces 0 — > 1 page fault

 Belady's anomaly – Belady's anomaly demonstrates that it is conceivable to have


more page faults when expanding the quantity of page frames while utilizing the First
in First Out (FIFO) page replacement algorithm. For instance, on the off chance that
we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9 all out
page faults, however on the off chance that we increment spaces to 4, we get 10 page
faults.

4.7.2.2 Optimal Page replacement


 This algorithm replaces the page which won't be alluded for such a long time in
future. Despite the fact that it cannot be for all intents and purposes implementable
however it very well may be utilized as a benchmark. Different algorithms are
contrasted with this regarding optimality.
 Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page
frame. Discover number of page faults.
 At first all openings are vacant, so when 7 0 1 2 are assigned to the unfilled spaces —
> 4 Page faults
 0 is as of now there so — > 0 Page fault.
 At the point when 3 came it will replace 7 since it isn't utilized for the longest length
of time in future— >1 Page fault.
 0 is as of now there so — > 0 Page fault.
 4 will replace of 1 — > 1 Page Fault.

 Presently for the further page reference string — > 0 Page fault since they are as of
now accessible in the memory.
 Optimal page replacement is flawless, yet unrealistic by and by as the operating
system can't know future requests. The utilization of Optimal Page replacement is to
set up a benchmark with the goal that other replacement calculations can be examined
against it.

4.7.2.3 Least Recently Used


 This algorithm replaces the page which has not been alluded for quite a while. This
algorithm is only inverse to the optimal page replacement algorithm. In this, we take a
look at the past as opposed to gazing at future.
 Example- 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.
 At first all openings are vacant, so when 7 0 1 2 are apportioned to the vacant spaces
— > 4 Page fault
 0 is now their so — > 0 Page fault.
 at the point when 3 came it will replace 7 since it is least as of late utilized — >1 Page
fault.
 0 is now in memory so — > 0 Page fault.
 4 will happens of 1 — > 1 Page Fault
 Presently for the further page reference string — > 0 Page fault since they are as of
now accessible in the memory.

7.5 Allocation of frames


 A significant part of operating systems, virtual memory is designed utilizing demand
paging.
 Demand paging requires the advancement of a page-replacement algorithm and a
frame allocation algorithm.
 Frame allocation algorithms are utilized in the event when we have different
processes; it chooses what number of frames to assign to each process.
 There are different imperatives to the techniques for the allotment of frames:
o You can't assign more than the available number of frames.
o In any event a minimum number of frames need to be assigned to each
process. This imperative is upheld by two reasons. The main reason is, as less
number of frames is allotted, there is a rise in the page fault ratio, minimizing
the throughput of the execution of the process. Also, there should be sufficient
frames to hold all the various pages that any single instruction can reference.
7.5.1 Frame allocation algorithm
 At point when a page fault happens, there is a free frame accessible to store another
page into a frame.
 While the page swap is occurring, a substitution can be chosen, which is kept in disk
as the user process keeps on executing.
 The operating system dispenses all its buffer and tablespace from the free-frame list
for the new page.
 Two noteworthy allocation Algorithm/plans.
1. Equal allocation
2. Proportional allocation

7.5.1.1 Equal allocation


 The simplest method to split m frames among n processes is to give everybody an
equivalent share, m/n frames. This is known as equal allocation.
 In a system with x frames and y processes, each process gets equivalent number of
frames, i.e. x/y.
 For example, if the system has 37 frames and 7 processes, each process will get 5
frames. The two frames which are not allotted to any process can be utilized as a free-
frame buffer pool.
 Drawback: In systems with processes of differing sizes, it doesn't fit well to give
each process equivalent frames. Assignment of an enormous number of frames to a
little process will in the long run lead to the wastage of countless designated unused
frames.

7.5.1.2 Proportional allocation


 Frames are assigned to each process as per the process size.
 For a process pi of size si, the quantity of assigned frames is ai = (si/S)*m, where S is
the sum of all the processes size and m is the quantity of frames in the system. For
example, in a system with 62 frames, in the event that there is a process of 10KB and
another process of 127KB, at that point the first process will be assigned (10/137)*62
= 4 edges and the different process will get (127/137)*62 = 57 frames.
 Benefit: All the processes share the accessible casings as per their needs, instead of
similarly.

7.5.2 Global versus Local Allocation –


The quantity of frames apportioned to a process can likewise progressively change depending
upon whether you have utilized global replacement or local replacement for replacing pages
if there should be an occurrence of a page fault.
1. Local replacement: When a process needs a page which isn't in the memory, it can get
the new page and assign it a frame from its own allocated frames list.
 Advantage: The pages in memory for a specific process and the page fault
proportion is influenced by the paging behaviour of just that process.
 Drawback: A low priority process may prevent a high priority process by not
making accessible to the high need process its frames.
2. Global Replacement: When a process needs a page which isn't in the memory, it can
acquire the new page and distribute it a frame from the set of all frames, regardless of
whether that frame is right now allotted to some different process; that is, one process
can take a frame from another.
 Advantage: Does not impede the exhibition of processes and subsequently
brings about more prominent system throughput.
 Drawback: The page fault ratio of a process cannot be exclusively constrained
by the process itself. The pages in memory for a process relies upon the
paging behaviour of different processes also.

7.6 Thrashing
 Thrashing is computer action that allows little or no progress, as memory or different
resources have turned out to be depleted or too limited to even perform required
functions.
 At this point, a pattern normally creates in which a request made to the operating
system by a process or program, then the operating system attempts to find resources
by taking them from some different process, which in turn makes new demands that
can't be fulfilled.
 In a virtual storage system (an operating system that deals with its local storage or
memory in units called pages), thrashing is a condition wherein extreme paging
operations are occurring.
 A system that is thrashing can be seen as either an exceptionally moderate system or
one that has stopped.
 Thrashing is a condition or a circumstance when the system is spending a noteworthy
segment of its time in adjusting the page faults; however the genuine processing done
is truly insignificant.
 The fundamental idea included is that in the event that a process is assigned too few
frames, at that point there will be an excess of and too successive page faults.
 Thus, no work would be finished by the CPU and the CPU use would fall definitely.
 The long term scheduler would then attempt to improve the CPU usage by loading
some more processes into the memory in this way expanding the level of
multiprogramming.
 This would bring about a further decline in the CPU usage setting off a chained
response of higher page faults followed by an expansion in the level of
multiprogramming, called Thrashing.

7.6.1 Reasons for Thrashing


It brings about extreme performance issues.
1. If CPU use is too low then we increase the level of multiprogramming by acquainting
another process with the system. A global page replacement algorithm is utilized. The
CPU scheduler sees the diminishing CPU utiliation and builds the level of
multiprogramming.
2. CPU use is plotted against the level of multiprogramming.
3. As the level of multiprogramming expands, CPU use likewise increase.
4. If the level of multiprogramming is expanded further, thrashing sets in and CPU use
drops sharply.
5. So, now, to expand CPU usage and to quit thrashing, we should diminish the level of
multiprogramming.

7.6.2 Methods to Handle Thrashing


We previously observed Local replacement is superior to Global replacement to abstain from
thrashing. Be that as it may, it likewise has drawbacks and not suggestible. Some more
strategies are
7.6.2.1 Working Set Model
 This model depends on locality. What region is stating, the page utilized as of recently
can be utilized again and furthermore the pages which are adjacent this page will
likewise be utilized.
 Working set methods states that set of pages in the latest D time.
 The page which finished its D amount of time in working set naturally dropped from
it.
 So precision of working set relies upon D we have picked.
 This working set model abstain from thrashing while at the same time keeping the
level of multiprogramming as high as could be expected under the circumstances.

7.6.2.2 Page Fault Frequency


 It is some immediate methodology than working set model.
 When thrashing happens we realize that it has few number of frames. Also, on the off
chance that it isn't thrashing that implies it has an excessive number of frames.
 In light of this property we allocate an upper and lower bound for the ideal page fault
rate.
 As indicated by page fault rate we assign or remove pages.
 On the off chance that the page fault rate become less than the limit, frames can be
expelled from the process.
 So, if the page fault rate become more than the limit, progressively number of frames
can be distributed to the process.
Furthermore, if no frames available because of high page fault rate, we will simply suspend
the processes and will restart them again when frames will be available.

You might also like