物理内存是什么虚拟内存作用_什么是虚拟内存?

物理内存是什么虚拟内存作用

Virtual Memory is a space where large programs can store themselves in form of pages while their execution and only the required pages or portions of processes are loaded into the main memory. This technique is useful as large virtual memory is provided for user programs when a very small physical memory is there.

虚拟内存是一个空间,大型程序可以在其中执行时以页面形式存储自身,并且仅将所需的页面或进程的一部分加载到主内存中。 当存在非常小的物理内存时,为用户程序提供了较大的虚拟内存,因此该技术很有用。

In real scenarios, most processes never need all their pages at once, for following reasons :

在实际情况下,由于以下原因,大多数进程永远不需要一次使用所有页面:

  • Error handling code is not needed unless that specific error occurs, some of which are quite rare.

    除非发生特定错误,否则不需要错误处理代码,其中某些错误很少发生。

  • Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays are actually used in practice.

    在最坏的情况下,阵列通常尺寸过大,实际上只有一小部分阵列被实际使用。

  • Certain features of certain programs are rarely used.

    某些程序的某些功能很少使用。

拥有虚拟内存的好处 (Benefits of having Virtual Memory)

  1. Large programs can be written, as virtual space available is huge compared to physical memory.

    可以编写大型程序,因为与物理内存相比,可用的虚拟空间巨大。

  2. Less I/O required, leads to faster and easy swapping of processes.

    所需的I / O更少,可更快,更轻松地交换进程。

  3. More physical memory available, as programs are stored on virtual memory, so they occupy very less space on actual physical memory.

    由于程序存储在虚拟内存中,因此有更多可用的物理内存,因此它们在实际物理内存上占用的空间非常少。

什么是按需分页? (What is Demand Paging?)

The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at once. Rather they are swapped in only when the process needs them(On demand). This is termed as lazy swapper, although a pager is a more accurate term.

需求分页的基本思想是,当交换一个进程时,它的页面不会一次全部交换。 而是仅在流程需要它们时(按需)才将它们交换。 尽管寻呼机是一个更准确的术语,但它被称为惰性交换程序。

Demand Paging

Initially only those pages are loaded which will be required the process immediately.

最初,只有那些页面被加载,将立即需要该过程。

The pages that are not moved into the memory, are marked as invalid in the page table. For an invalid entry the rest of the table is empty. In case of pages that are loaded in the memory, they are marked as valid along with the information about where to find the swapped out page.

未移动到内存中的页面在页面表中被标记为无效。 对于无效的条目,表的其余部分为空。 如果页面已加载到内存中,则会将它们与有关在何处找到换出页面的信息一起标记为有效。

When the process requires any of the page that is not loaded into the memory, a page fault trap is triggered and following steps are followed,

当该过程需要任何未加载到内存中的页面时,将触发页面错误陷阱并遵循以下步骤,

  1. The memory address which is requested by the process is first checked, to verify the request made by the process.

    首先检查该过程所请求的内存地址,以验证该过程所发出的请求。

  2. If its found to be invalid, the process is terminated.

    如果发现其无效,则该过程终止。

  3. In case the request by the process is valid, a free frame is located, possibly from a free-frame list, where the required page will be moved.

    如果该过程的请求有效,则可能从自由框架列表中找到一个自由框架,并将所需页面移到该框架中。

  4. A new operation is scheduled to move the necessary page from disk to the specified memory location. ( This will usually block the process on an I/O wait, allowing some other process to use the CPU in the meantime. )

    计划执行一项新操作,以将必要的页面从磁盘移动到指定的内存位置。 (这通常会在等待I / O时阻止该进程,同时允许其他进程使用CPU。)

  5. When the I/O operation is complete, the process's page table is updated with the new frame number, and the invalid bit is changed to valid.

    I / O操作完成后,将使用新的帧号更新进程的页表,并将无效位更改为有效。

  6. The instruction that caused the page fault must now be restarted from the beginning.

    现在必须从头开始重新启动导致页面错误的指令。

There are cases when no pages are loaded into the memory initially, pages are only loaded when demanded by the process by generating page faults. This is called Pure Demand Paging.

在某些情况下,最初没有页面加载到内存中,只有在过程需要时才通过生成页面错误来加载页面。 这称为纯需求分页

The only major issue with Demand Paging is, after a new page is loaded, the process starts execution from the beginning. Its is not a big issue for small programs, but for larger programs it affects performance drastically.

需求分页的唯一主要问题是,在加载新页面后,该过程将从头开始执行。 对于小型程序来说,这不是一个大问题,但是对于大型程序,它会极大地影响性能。

页面替换 (Page Replacement)

As studied in Demand Paging, only certain pages of a process are loaded initially into the memory. This allows us to get more number of processes into the memory at the same time. but what happens when a process requests for more pages and no free memory is available to bring them in. Following steps can be taken to deal with this problem :

如需求分页中所述,最初仅将进程的某些页面加载到内存中。 这使我们可以同时将更多数量的进程放入内存。 但是当进程请求更多页面并且没有可用内存将其引入时会发生什么。可以采取以下步骤来解决此问题:

  1. Put the process in the wait queue, until any other process finishes its execution thereby freeing frames.

    将进程放入等待队列,直到其他进程完成执行,从而释放帧。

  2. Or, remove some other process completely from the memory to free frames.

    或者,将其他进程完全从内存中删除以释放帧。

  3. Or, find some pages that are not being used right now, move them to the disk to get free frames. This technique is called Page replacement and is most commonly used. We have some great algorithms to carry on page replacement efficiently.

    或者,找到一些当前未使用的页面,将它们移至磁盘以获取可用的框架。 此技术称为页面替换 ,是最常用的技术。 我们有一些很棒的算法可以有效地进行页面替换。

基本页面替换 (Basic Page Replacement)

  • Find the location of the page requested by ongoing process on the disk.

    查找正在进行的进程在磁盘上请求的页面的位置。

  • 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 any existing frame to be replaced, such frame is known as victim frame.

    寻找一个自由的框架。 如果有自由框架,请使用它。 如果没有可用的帧,请使用页面替换算法来选择要替换的任何现有帧,这种帧称为“ 受害者帧”

  • Write the victim frame to disk. Change all related page tables to indicate that this page is no longer in memory.

    将受害者帧写入磁盘。 更改所有相关的页表以指示该页不再在内存中。

  • Move the required page and store it in the frame. Adjust all related page and frame tables to indicate the change.

    移动所需的页面并将其存储在框架中。 调整所有相关的页面和框架表以指示更改。

  • Restart the process that was waiting for this page.

    重新启动等待该页面的进程。

FIFO页面替换 (FIFO Page Replacement)

  • A very simple way of Page replacement is FIFO (First in First Out)

    页替换的一种非常简单的方法是FIFO(先进先出)

  • As new pages are requested and are swapped in, they are added to tail of a queue and the page which is at the head becomes the victim.

    当请求并交换新页面时,会将它们添加到队列的尾部,并且位于头部的页面将成为受害者。

  • Its not an effective way of page replacement but can be used for small systems.

    它不是页面替换的有效方法,但可用于小型系统。

LRU页面更换 (LRU Page Replacement)

Below is a video, which will explain LRU Page replacement algorithm in details with an example.

以下是一个视频,将通过示例详细说明LRU页面替换算法。

演示地址

脱粒 (Thrashing)

A process that is spending more time paging than executing is said to be thrashing. In other words it means, that the process doesn't have enough frames to hold all the pages for its execution, so it is swapping pages in and out very frequently to keep executing. Sometimes, the pages which will be required in the near future have to be swapped out.

花费大量时间进行分页而不是执行的进程被认为是不正常的。 换句话说,这意味着该进程没有足够的框架来容纳执行的所有页面,因此它非常频繁地换入和换出页面以保持执行。 有时,在不久的将来需要的页面必须换掉。

Initially when the CPU utilization is low, the process scheduling mechanism, to increase the level of multiprogramming loads multiple processes into the memory at the same time, allocating a limited amount of frames to each process. As the memory fills up, process starts to spend a lot of time for the required pages to be swapped in, again leading to low CPU utilization because most of the proccesses are waiting for pages. Hence the scheduler loads more processes to increase CPU utilization, as this continues at a point of time the complete system comes to a stop.

最初,当CPU使用率较低时,进程调度机制(用于提高多编程的级别)会将多个进程同时加载到内存中,从而为每个进程分配有限的帧数。 随着内存的填满,进程开始花费大量时间来交换所需的页面,这又导致CPU使用率降低,因为大多数进程都在等待页面。 因此,调度程序会加载更多进程以提高CPU利用率,因为这会在整个系统停止运行的时间点继续进行。

Thrashing

To prevent thrashing we must provide processes with as many frames as they really need "right now".

为了防止崩溃,我们必须为进程提供真正需要的“立即”帧。

翻译自: https://www.studytonight.com/operating-system/virtual-memory

物理内存是什么虚拟内存作用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值