L-5.19: Virtual Memory | Page fault | Significance of virtual memory | Operating System

Gate Smashers
11 Apr 201820:17

Summary

TLDRThis video script delves into the concept of virtual memory, explaining its significance in modern computing. It provides an illusion of limitless process size and multiprogramming capacity, despite finite main memory. The script covers how virtual memory allows larger processes to run by swapping pages in and out of RAM, utilizing the principle of locality of reference. It also touches on the potential downsides, such as page faults leading to performance issues like thrashing, and the importance of effective memory access time. The explanation aims to clarify the advantages and challenges of virtual memory in operating systems.

Takeaways

  • 💡 Virtual memory is a concept that provides the illusion of an unlimited memory space to programmers, allowing processes larger than the physical memory to be executed.
  • 🖥️ The main memory, such as RAM in a laptop, is finite, but virtual memory allows for the execution of processes that exceed the size of the main memory.
  • 🔄 Virtual memory operates on the principle of paging, dividing processes into pages that are loaded into the main memory as needed, based on the locality of reference principle.
  • 🔑 The process of loading pages into the main memory and replacing them is known as swap-in and swap-out, or roll-in and roll-out, which is managed by the operating system.
  • 🔍 The operating system uses a page table to keep track of which pages of a process are currently in the main memory and which are stored in secondary storage.
  • 🚫 A page fault occurs when the CPU requests a page that is not currently in the main memory, triggering a page fault service process.
  • 🛑 When a page fault happens, control is transferred from the user process to the operating system, which then handles the page replacement or loading from secondary storage.
  • 🔒 The operating system checks for user authentication before servicing a page fault to ensure security.
  • ⏱ The effective memory access time is calculated considering the probability of page faults and the time taken for page fault service versus direct memory access.
  • 🔍 The time taken to service a page fault is significantly higher than accessing the main memory, which can lead to performance degradation if there are too many page faults.
  • ⚠️ Excessive page faults can lead to a phenomenon called thrashing, where the system spends too much time swapping pages rather than executing processes, severely impacting performance.

Q & A

  • What is virtual memory?

    -Virtual memory is a concept that provides the illusion to programmers that a process, even larger than the physical main memory, can be executed. It allows systems with limited RAM to handle larger processes by using hard disk space as an extension of the main memory.

  • How does virtual memory enable the execution of processes larger than the main memory?

    -Virtual memory allows the execution of large processes by dividing them into smaller units called pages and only loading the necessary pages into the main memory as required, while the rest reside on the hard disk.

  • What is the significance of virtual memory in terms of process size?

    -Virtual memory eliminates the limitation on the size of processes that can be executed. Regardless of the main memory size, any process can be executed as long as the necessary pages are loaded into the main memory when needed.

  • How does virtual memory affect the degree of multiprogramming?

    -Virtual memory allows for a higher degree of multiprogramming by enabling the main memory to hold parts of multiple processes simultaneously, rather than whole processes, thus maximizing the use of limited main memory.

  • What is the concept of locality of reference in the context of virtual memory?

    -Locality of reference is the principle that a process tends to access a small subset of its address space frequently. When a page is loaded into the main memory, pages that are likely to be accessed next are also loaded to optimize performance.

  • What is a page fault in the context of virtual memory?

    -A page fault occurs when the CPU requests a page that is not currently in the main memory. This triggers a page fault service, where the operating system loads the required page from the hard disk into the main memory.

  • How is a page fault serviced in a system using virtual memory?

    -When a page fault occurs, control is transferred from the user process to the operating system, which then locates the required page in the hard disk, loads it into an available frame in the main memory, and updates the page table before returning control to the user process.

  • What is the impact of page faults on system performance?

    -Frequent page faults can significantly degrade system performance due to the time-consuming process of loading pages from the hard disk into the main memory. This can lead to a phenomenon known as thrashing, where the CPU spends most of its time servicing page faults rather than executing processes.

  • What is the effective memory access time and how is it calculated?

    -The effective memory access time is the average time taken to access a memory location, considering both page hits (when the page is already in the main memory) and page faults. It is calculated as the product of the probability of a page fault and the page fault service time, plus the product of the probability of a page hit and the main memory access time.

  • What are some common page replacement algorithms mentioned in the script?

    -The script mentions First In First Out (FIFO), Least Recently Used (LRU), and Most Recently Used (MRU) as examples of page replacement algorithms that determine which pages to replace when the main memory is full.

  • How does the script explain the difference in time between accessing main memory and servicing a page fault?

    -The script explains that accessing the main memory is very fast, typically in nanoseconds, whereas servicing a page fault is much slower, usually taking milliseconds, due to the slower speed of the hard disk compared to the main memory.

Outlines

00:00

💡 Virtual Memory Concept and Its Benefits

Virtual memory is introduced as a concept that extends the capabilities of physical memory (RAM). It provides programmers with the illusion that processes larger than the available RAM can be executed. This is possible because processes are divided into pages, and only the necessary pages are loaded into the main memory, thanks to the principle of locality of reference. This allows for a greater degree of multiprogramming, as the main memory can hold parts of multiple processes simultaneously. The concept is crucial as it overcomes the limitations of physical memory size, enabling the execution of larger processes and increasing the number of processes that can be active at one time.

05:04

🔄 Swap Operations and Page Replacement

This paragraph delves into the mechanics of virtual memory management, focusing on swap-in and swap-out operations, also known as roll-in and roll-out. These operations are essential for handling the limited main memory by replacing pages that are not currently needed with those that are. The paragraph introduces the concept of page replacement algorithms, such as FIFO, LRU, and MRU, which determine which pages to swap out when the main memory is full. The advantages of virtual memory are highlighted, including the ability to handle processes of any size and any number of processes, despite the physical limitations of the main memory.

10:09

🚨 Understanding Page Faults and Their Impact

The third paragraph explains the process that occurs when a CPU demands a page not present in the main memory, an event known as a page fault. It describes the role of the page table and the memory management unit in handling page faults. The operating system's response to a page fault includes authentication checks, searching for the required page in the logical address space (hard disk), and updating the page table once the page is loaded into the main memory. The paragraph also introduces the concept of effective memory access time, which is influenced by the probability of page faults and the time taken for page fault service versus direct memory access.

15:22

⏱️ Effective Memory Access Time and Threshing

The final paragraph discusses the calculation of effective memory access time, taking into account the probability of page faults (P) and the time taken for page fault service versus main memory access. It emphasizes the significant difference in access times between main memory (nanoseconds) and hard disk (milliseconds), and how frequent page faults can lead to performance degradation, a phenomenon known as threshing. The importance of reducing page faults and increasing page hits to improve effective memory access time is highlighted, concluding the significance and operation of virtual memory.

Mindmap

Keywords

💡Virtual Memory

Virtual memory is a concept in computing that provides the illusion of a larger amount of memory than is physically available. It allows systems to run programs that are larger than the available RAM by using a combination of physical memory and disk space. In the video, virtual memory is central to the discussion as it enables the execution of processes larger than the main memory size, exemplified by the script's mention of processes that can exceed the 8GB or 16GB RAM capacity of a laptop.

💡Main Memory

Main memory, often referred to as RAM (Random Access Memory), is the physical memory available in a computer for temporarily storing data that is being processed. The script discusses the limitations of main memory, such as the common 8GB or 16GB capacity, and how virtual memory overcomes these limitations by allowing more processes to be executed than could fit in physical RAM.

💡Process

A process in computing is a program that is currently being executed by the CPU. The video script explains that virtual memory allows for the execution of processes that are larger than the available main memory, which is significant as it demonstrates the utility of virtual memory in handling large or numerous processes.

💡Paging

Paging is a memory management technique that divides the process and the main memory into fixed-size units called pages and frames, respectively. The script mentions paging as a concept where processes are divided into pages, allowing for efficient memory management and the use of virtual memory.

💡Locality of Reference

Locality of reference is the tendency of a process to access a small subset of memory locations frequently. The script uses the concept of locality of reference to explain how virtual memory brings related pages into the main memory, anticipating that if one page is accessed, nearby pages are likely to be accessed soon.

💡Page Table

A page table is a data structure used in virtual memory systems to store the mapping of virtual addresses to physical addresses. The script explains how the page table is used to check which pages of a process are currently loaded into the main memory and to handle page faults.

💡Page Fault

A page fault occurs when a program requests access to a page that is not currently in the main memory. The video script describes the process of handling a page fault, which includes the CPU generating a trap, the operating system taking control, and the page being loaded from disk into the main memory.

💡Trap

A trap is a type of interrupt that occurs when the CPU encounters a condition that it cannot handle, such as a page fault. The script uses the term 'trap' to illustrate the point at which control is transferred from the user process to the operating system to handle a page fault.

💡Context Switching

Context switching is the process of storing and reloading the state of a process or thread so that execution can be resumed from the same point at a later time. The script refers to context switching when discussing how control moves from a user process to the operating system during a page fault and then back to the user process once the fault is serviced.

💡Effective Memory Access Time

Effective memory access time is the average time it takes to access data in memory, considering both page hits and page faults. The script explains how this metric is calculated by considering the probability of a page fault and the time taken for page fault service versus the time taken to access the main memory.

💡Threshing

Threshing, also known as thrashing, occurs when a system spends more time handling page faults than doing actual work, leading to degraded performance. The script warns about the potential problem of threshing due to excessive page faults, which can significantly impact system performance.

Highlights

Virtual memory provides the illusion that a process larger than the main memory can be executed.

Main memory is finite, but processes can still be executed even if they exceed the size of the main memory.

Virtual memory allows for an increased degree of multiprogramming by maximizing the number of processes in the main memory.

Processes are divided into pages, with the page size equal to the frame size, to manage memory efficiently.

The concept of locality of reference is used to bring related pages into the main memory for efficient processing.

Virtual memory enables parts of multiple processes to be kept in the main memory, giving the user the illusion of their presence.

Swap-in and swap-out operations, also known as roll-in and roll-out, are used to manage page replacement in the main memory.

Page replacement algorithms such as FIFO, LRU, and MRU are used to efficiently manage memory.

Virtual memory provides two main advantages: no limitation on process size and the number of processes.

Virtual memory is used in all modern operating systems, including laptops and PCs.

A page fault occurs when the CPU demands a page not present in the main memory, triggering a trap and context switching.

Operating systems check user authentication before servicing a page fault for security purposes.

Page faults are serviced by fetching the required page from the hard disk and updating the page table.

Effective memory access time is calculated considering the probability of page faults and main memory access time.

Excessive page faults can lead to thrashing, significantly degrading system performance.

The time difference between page fault service time in milliseconds and main memory access time in nanoseconds is significant.

Reducing page faults and increasing page hits can greatly improve effective memory access time.

Transcripts

play00:00

- Virtual memory. Virtual memory is actually a concept.

play00:03

- Till now we've discussed a lot about the main memory that is a physical memory

play00:08

- in this video we will see about how the virtual memory actually works

play00:12

- and what is the significance of the virtual memory

play00:15

- now, the first significance of the virtual memory is that it provides the illusion to the programmer

play00:22

- that a process whose size is larger than the size of main memory

play00:27

- can also be execute

play00:29

- means, if I have a process whose size is even more than the size of main memory

play00:36

- we can execute that process also

play00:40

- How is that possible? Actually, our main memory is finite

play00:46

- Means, if I talk about my laptop, so in our laptop we get RAM upto 8gb or 16gb

play00:52

- so if the main memory is of 18gb or 16gb but there is no limitations for the size of processes.

play00:59

- the size of processes are increasing day by day. So if a process whose size is more than the size of the main memory.

play01:07

- So if we see normally, so when a process won't fit in the main memory then how we will provide it's pages or data to the CPU

play01:17

- if we see normally, when a process won't fit in this main memory then how we will provide the data to CPU and how will CPU execute that data

play01:28

- but no. Even if the main memory here is finite but the concept of virtual memory is providing the first illusion

play01:37

- that the process whose size is much larger than the size of main memory, we can execute that process also.

play01:46

- second point here is, In degree of multi programming we talk about that more and more no. processes must come in the main memory

play01:55

- So here I'm saying that main memory is limited. So we have to bring maximum no. of processes in the main memory

play02:03

- so for that we should have more main memory

play02:06

- but we've limited the size of the main memory that the size of main memory is, let say, 16gb

play02:12

- so how we will bring maximum processes in main memory and how we will execute that, this is also a illusion which is provided by the virtual memory - so how virtual memory actually works here, that we will see now

play02:26

- so what we're doing here basically, it's a logical address space, logical address space means we are talking about the hard disk

play02:33

- where there are multiple processes, we have divided that processes into pages.

play02:40

- we've already seen the concept of paging where we divide processes into pages. Page size is equal to size of frame.

play02:49

- so I already divided multiple processes into pages. Now, one whole process

play02:56

- means, instead of bringing one full process in main memory we are bringing only required processes/pages of required process in the main memory.

play03:10

- means, the processes/pages that are required now we will bring only that in main memory

play03:21

- means, there's a simple concept, locality of reference

play03:25

- what we do in locality of reference is that when we bring a page in the main memory

play03:30

- we don't bring that page only, we bring the related pages along with that. Let say, in school class teacher is telling to call roll no.10,

play03:42

- then we call roll no.10 but we call roll no.8,9,11,12, the nearby roll no. students, along with it,

play03:51

- so that, actually we are thinking that it may be possible that he have called roll no.10 now, next time it may call roll no.9 or 11.

play04:00

- this is one of the example that we follow in this that we bring pages in the main memory but

play04:08

- rather than entire process into the main memory, we keep some of the pages in the main memory.

play04:15

- The advantage we get from that is, like in this main memory, some page of P1, some pages of P2, some pages of P3, some pages of P4

play04:26

- this is how we can keep the main memory, that means we don't bring the whole process in main memory

play04:33

- we will keep the process that are required in the main memory

play04:39

- so what will happen, rather than entire process comes into the main memory only some part of it is coming in the main memory

play04:47

- so it only some parts come in, so we can keep some parts of many processes in the main memory

play04:55

- so what is the advantage of this, the advantage is that parts of maximum processes can be kept in main memory

play05:04

- so by this user thinks, whenever I call process 1 it is there, 2, it is also there, 3, it is also there, 4,5, 6, 7, so many processes are there in main memory

play05:17

- but these processes are not full, only there some pages are kept in the main memory

play05:25

- and how we do this, by using the swap-in and swap-out operation that we also call roll-in and roll-out.

play05:32

- Roll-in

play05:34

- Roll-out

play05:37

- Roll-in, Roll-out means, the meaning of swap-in is that we will bring page in the main memory and after some time we

play05:46

- because we've limited main memory, so where we will have limitation, so I can't keep that page in permanent main memory

play05:55

- I'll have to take out that page after some time and I'll have to bring some new process page.

play06:04

- so we call this page replacement.

play06:07

- that how we replace pages and we bring in new pages. So there are many algorithms of page replacement

play06:14

- like first in first out, LRU, MRU, that we will see in further videos

play06:20

- here we are just talking about what is the advantage of virtual memory. So what is the advantage here, there are 2 advantage, means it is providing illusion

play06:27

- the size of process is as much wanted and 2nd, the no. of processes is also as much wanted. Means, there's no limitation of no. of processes

play06:36

- there's no limitation of size of process but the main memory here is already limited.

play06:42

- so the main memory is limited, let it be, but whatever the size of process is, we can bring it into the main memory

play06:48

- and no matter how many are the number of processes, we can bring it into main memory

play06:53

- so this is a very big advantage, the virtual memory, all the laptops and PCs and operating systems we are using nowadays

play07:03

- actually the concept of virtual memory is used in all these

play07:06

- So it is providing this big advantage. So is there any problem in this?

play07:12

- Can there be a problem? Let's analyse this.

play07:16

- Let say, CPU is executing a process P1. If CPU is executing a process P1

play07:23

- then first thing CPU will do, it will demand for any page or it will demand for any data. Let say,

play07:29

- CPU made a demand that it wants page no.1, the data of page no.1, means CPU generated a logical address.

play07:40

- from that logical address I get to know that the data that CPU is demanding of is on page no.1 of process P1

play07:50

- so where is page no.1 of process P1 in main memory

play07:55

- how do we do that, by using the page table, we check that through memory management unit, what does memory management unit do

play08:04

- it loads the page table, we loaded the page table from the main memory and checked which pages are lying in which frames

play08:13

- So here we're talking about P1 page

play08:17

- So as soon as CPU demands the data it wants, the logical address told that page is P1 page

play08:24

- but if you look here carefully, P1 page is not present.

play08:30

- Means, I looked here in page table entry, we've already discussed page table entry.

play08:36

- So in page table entry, there are valid and invalid bits

play08:40

- we also call them present or absent. That tells actually that page is present or absent in the main memory.

play08:48

- So if a page that CPU wants now, if that page is absent in the main memory

play08:56

- we call this page fault. If the page is present, that is the best case means the page that CPU is demanding of is already in main memory, then you'll pick that page from main memory and provide data to the CPU

play09:10

- but if that page is not in main memory, it is absent is main memory then it will actually generate a page fault

play09:22

- page fault

play09:23

- so how we service page fault, means how we remove page fault or if page fault occurs what we will do after that, this is the concept

play09:33

- so if the CPU generated the first step that, CPU demanded that it wants page 1 of some P1 process

play09:41

- before checking in main memory, I checked in page table that actually the page is lying in which frame

play09:48

- then I didn't found any entry here because my valid bit is stating that it's 0. That means that page is absent.

play09:55

- so in this case, what will happen here, trap will generate here. Trap. Trap means, it is a kind of interrupt

play10:03

- Now as soon as trap is generated, before this the control was in the hands of user

play10:09

- means, because we are executing the process of user but as soon as trap generates here, control came from user to operating system

play10:20

- means, you can call this context switching. Like we do context switching in round robin that means one process that is going on

play10:27

- we send that into ready state. And we load another process in running state

play10:32

- so this is called context switching. So here also context switching is happening.

play10:37

- because user had the control, some P1 process of user was executing but as soon as its page wasn't found here

play10:46

- so the control that user had was lost because trap is generated here.

play10:52

- Trap means, it is a kind of interrupt. So interrupt occurred, so operating system took the control in it's hands.

play11:00

- Now, what operating system will do here, first of all operating system will check that the user who is demanding of this page,

play11:08

- that user is authenticate in actual or not. First of all, operating system will activate here, operating system will check authentication.

play11:19

- authentication, authorization, kind of. That is just for a security purpose that the person/the user who is demanding of the particular process/page through CPU

play11:33

- is a validate user or invalidate. So if that user is validate, then CPU will say ok, I'll now provide the page here

play11:41

- but where's that page lying actually, that page is actually lying in the hard disk, if all the data isn't in main memory, then where that data will be

play11:53

- in hard disk. Means, in logical address space. So operating system will first go in logical address space

play12:03

- After getting in logical address space, it will search where that process is actually. So we're talking about page 1 of P1 process.

play12:12

- So page 1 of P1 process is kept here. So here what operating system will do is that, we have to take out this page from here and enter it in some empty slot of main memory.

play12:26

- So this process, I took out P1 page of this process and here I, Page 1 of process P1,

play12:38

- this is the meaning of page 1 of process P1. We kept the page of that process here.

play12:45

- And the address here, what it is, frame no.7. So I updated frame no.7 here.

play12:53

- what it meant to say, that the first step was that what CPU generated, second step here is, trap generated, third step

play13:01

- CPU, sorry, operating system first searched for the page in logical address space, when it was found there picked it up from there

play13:11

- and the fourth step is that we placed that page in main memory. After that, the process of that page, where's that page, in main memory

play13:21

- but you also have to update it in page table because the address that CPU generated, we've to first find it from here

play13:28

- it may be possible that CPU again demands for that page. So for that I've to update address in the page table.

play13:36

- So I updated the address here, means, page no.1 is present at frame no.7

play13:43

- and then I'll give control back to the user. Means, after this

play13:50

- after this step, this is your 4th, 5th, 6th step, sorry, this is the 6th step. So after 6th step,

play13:58

- control will again be returned to user level from operating system. Means, here user mode to operating system, and the control from operating system again retuned back to

play14:08

- the user. But here if we talk about time. What is the time actually we've used?

play14:16

- So we can calculate the effective memory access time. This is called the effective memory access time.

play14:29

- so what is the effective memory access time? Let say, there is a probability P

play14:36

- that, P is a probability which tells that page fault occurs, P is the probability of page fault occurring

play14:48

- so how we will calculate effective memory access time, if there's P probability of page table occurring, so as soon as page table occurs, so what I did in that case, we did page fault service

play15:02

- the time used in page fault service is page fault service time

play15:07

- that we can call page fault service time. Means, if P times, I've, P times or P is a probability that

play15:22

- that how much page faults will occur. So as often page fault occurs, you have to do page service that much times.

play15:30

- And, 1-P. What is 1-P? That is a best case, means there is no page fault.

play15:37

- if page fault isn't occurring 1-P times, if page fault isn't occurring so you know in that case as soon as CPU demands of some page

play15:47

- we will pick reference of that page from here, let say, it is kept in frame no.1, we will directly get to frame no.1

play15:53

- So the time that is taken in accessing main memory, that time will be used here. So that means, 1-P times we are using

play16:02

- main memory access

play16:07

- main memory access time that we can call MA

play16:12

- means the access time of main memory. So P time is taken in servicing page fault. 1-P into the time taken in accessing main memory

play16:22

- and the important point here is that the time of accessing the main memory, in most of the cases, it is in nanoseconds.

play16:32

- because main memory is very fast. So when we will find page in main memory,

play16:37

- and I know that page is in main memory, then it will, we will access that data within nanoseconds.

play16:44

- But as soon as page fault occurs, what we do in page fault is that first control is given to operating system

play16:52

- when operating system have, when context switching will happen from user to operating system, first time will be wasted there.

play16:59

- Because the control is transferred from user to operating system. Now, where the operating system is searching?

play17:06

- In hard disk. Hard disk is very slow as compared to the main memory.

play17:11

- so if hard disk is slow, So obviously it takes so much time to pick data from there and keep it in main memory

play17:20

- and yes, this time is generally in milliseconds.

play17:26

- This page fault service time is generally in milliseconds. So we wasted milliseconds of time to bring that page

play17:35

- in main memory. And if that page fault haven't occurred, in that case we will access data in nanoseconds.

play17:43

- One more point is here that when we are servicing page fault, in that case time taken is in milliseconds

play17:50

- but sometimes it can happen that main memory time is also added in that, the time taken to access main memory

play17:57

- why do we generally add that? When we've serviced page fault, means the page that was missing, we, from back end,

play18:07

- means from hard disk, we kept that in main memory. But CPU didn't get it yet. CPU have to search again, and now it will get to know that, Ok, that page is in frame no.7

play18:21

- main memory access time is in nanoseconds, so that's why this time, is very less as compared to this time. So that's why we don't consider this time here.

play18:34

- so here I can say, effective memory access time actually is, P into page fault service time plus 1-P into main memory access time

play18:47

- here you can get many numericals based on this that if this much percent page fault is occurring, main memory access time is this, then find what is the effective memory access time.

play18:58

- and a very big problem here is that if there are too many page faults. If in case there are too many page faults

play19:07

- so there can be the problem of threshing. You are using CPU utilisation too much.

play19:12

- If page fault becomes too much, so the threshing and performance in that case will suddenly degrade.

play19:20

- so generally, service time of page fault is in milliseconds and access time of main memory is in nanoseconds. And I, you all already know that 1 millisecond = 10 lakh

play19:32

- or 1 million nanoseconds. Means there are 10 lakh nanoseconds in a millisecond

play19:39

- so obviously, time gap will increase here so much just because of the page fault

play19:45

- but there are advantages too here, it is providing illusion, there's no limitation of page size

play19:53

- there's no limitation of no. of processes. So If I lessen the no. of the page faults and if we increase no. of page hits

play20:01

- so in that case my effective memory access time will perform very well.

play20:06

- so this is how actually the virtual memory works and this is the significance of the virtual memory

play20:12

- so thanks a lot, if you liked my video please share it with your friends. Thank you.

Rate This

5.0 / 5 (0 votes)

関連タグ
Virtual MemoryMain MemoryPagingProcess ExecutionMemory ManagementPage FaultPage ReplacementLogical AddressOperating SystemHardware Limitations
英語で要約が必要ですか?