物理/逻辑/虚拟内存地址的区别 [英] Difference between physical/logical/virtual memory address

查看:30
本文介绍了物理/逻辑/虚拟内存地址的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对操作系统中的物理/逻辑/虚拟地址这两个术语有点困惑(我使用 Linux-open SU​​SE)

I am a little confused about the terms physical/logical/virtual addresses in an Operating System(I use Linux- open SUSE)

以下是我的理解:

  1. 物理地址- 当处理器处于系统模式时,处理器使用的地址是物理地址.

  1. Physical Address- When the processor is in system mode, the address used by the processor is physical address.

逻辑地址——当处理器处于用户模式时,使用的地址是逻辑地址.这些无论如何都通过添加具有偏移值的基址寄存器映射到某个物理地址.它在某种程度上提供了一种内存保护.

Logical Address- When the processor is in user mode, the address used is the logical address. these are anyways mapped to some physical address by adding a base register with the offset value.It in a way provides a sort of memory protection.

我遇到过关于虚拟和逻辑地址/地址空间相同的讨论.这是真的吗?

I have come across discussion that virtual and logical addresses/address space are the same. Is it true?

非常感谢任何帮助.

推荐答案

我的回答适用于在现代 Linux 系统上运行的 Intel CPU,我说的是用户级进程,而不是内核代码.不过,我认为它会给你一些足够的洞察力来思考其他可能性

关于问题 3:

我遇到过关于虚拟和逻辑的讨论地址/地址空间是相同的.是真的吗?

I have come across discussion that virtual and logical addresses/address space are the same. Is it true?

据我所知,它们是相同的,至少在运行在英特尔处理器之上的现代操作系统中是这样.

As far as I know they are the same, at least in modern OS's running on top of Intel processors.

在解释更多之前,让我尝试定义两个概念:

Let me try to define two notions before I explain more:

  • 物理地址:某些东西在 RAM 芯片中的物理位置.
  • 逻辑/虚拟地址:您的程序用于访问其事物的地址.它通常稍后由硬件芯片转换为物理地址(大多数情况下,甚至 CPU 都不知道这种转换).
  • Physical Address: The address of where something is physically located in the RAM chip.
  • Logical/Virtual Address: The address that your program uses to reach its things. It's typically converted to a physical address later by a hardware chip (mostly, not even the CPU is aware really of this conversion).

虚拟地址很好,一个虚拟地址,操作系统以及称为 MMU(内存管理单元)的硬件电路欺骗你的程序,它在系统中单独运行,它有整个地址空间(有 32 位)system 意味着您的程序会认为它有 4 GB 的 RAM;粗略地说).

The virtual address is well, a virtual address, the OS along with a hardware circuit called the MMU (Memory Management Unit) delude your program that it's running alone in the system, it's got the whole address space(having 32-bits system means your program will think it has 4 GBs of RAM; roughly speaking).

显然,如果您同时运行多个程序(您总是这样做,GUI、Init 进程、Shell、时钟应用程序、日历等等),这将不起作用.

Obviously, if you have more than one program running at the time (you always do, GUI, Init process, Shell, clock app, calendar, whatever), this won't work.

将会发生的情况是操作系统会将您的大部分程序内存放在硬盘中,它使用最多的部分将出现在 RAM 中,但是,嘿,这并不意味着它们将拥有您的地址并且您的程序知道.

What will happen is that the OS will put most of your program memory in the hard disk, the parts it uses the most will be present in the RAM, but hey, that doesn't mean they'll have the address you and your program know.

示例:您的进程可能有一个名为 (counter) 的变量,它的虚拟地址为 0xff(可以想象……)和另一个名为 (oftenNotUsed) 的变量,它的虚拟地址为 (0xaa).

Example: Your process might have a variable named (counter) that's given the virtual address 0xff (imaginably...) and another variable named (oftenNotUsed) that's given the virtual address (0xaa).

如果您在所有链接发生后阅读已编译代码的程序集,您将使用这些地址访问它们,但是,(oftenNotUsed) 变量不会真正存在于 RAM 中的 0xaa,它将在硬盘,因为进程没有使用它.

If you read the assembly of your compiled code after all linking's happened, you'll be accessing them using those addresses but well, the (oftenNotUsed) variable won't be really there in RAM at 0xaa, it'll be in the hard disk because the process is not using it.

此外,变量(计数器)可能不会物理上位于 (0xff),它会在 RAM 中的其他位置,当您的 CPU 尝试获取 0xff 中的内容时,MMU 和操作系统的一部分,将进行映射并从 RAM 中真正可用的位置获取该变量,CPU 甚至不会注意到它不在 0xff 中.

Moreover, the variable (counter) probably won't be physically at (0xff), it'll be somewhere else in RAM, when your CPU tries to fetch what's in 0xff, the MMU and a part of the OS, will do a mapping and get that variable from where it's really available in the RAM, the CPU won't even notice it wasn't in 0xff.

现在如果您的程序要求使用 (oftenNotUsed) 变量会怎样?MMU+OS 会注意到这个未命中",并将它从硬盘中提取到 CPU 中,然后将它交给 CPU,就像它在地址 (0xaa) 中一样;这种获取意味着一些存在于 RAM 中的数据将被发送回硬盘.

Now what happens if your program asks for the (oftenNotUsed) variable? The MMU+OS will notice this 'miss' and will fetch it for the CPU from the Harddisk into RAM then hand it over to the CPU as if it were in the address (0xaa); this fetching means some data that was present in RAM will be sent back to the Harddisk.

现在想象一下为您系统中的每个进程运行.每个进程都认为他们有 4GB 的 RAM,实际上没有人拥有,但一切正常,因为每个人的程序的某些部分都在 RAM 中物理可用,但大部分程序驻留在硬盘中.不要将HD中的这部分程序存储器与您可以通过文件操作访问的程序数据混淆.

Now imagine this running for every process in your system. Every process thinks they have 4GB of RAMs, no one actually have that but everything works because everyone has some parts of their program available physically in the RAM but most of the program resides in the HardDisk. Don't confuse this part of the program memory being put in HD with the program data you can access through file operations.

虚拟地址:您在程序中使用的地址,即您的 CPU 用于获取数据的地址,不是真实的,而是通过 MMU 转换为某个物理地址;每个人都有一个,它的大小取决于你的系统(运行 32 位的 Linux 有 4GB 地址空间)

Virtual address: The address you use in your programs, the address that your CPU use to fetch data, is not real and gets translated via MMU to some physical address; everyone has one and its size depends on your system(Linux running 32-bit has 4GB address space)

物理地址:如果您在操作系统上运行,您将永远无法到达的地址.无论其虚拟地址如何,它都是您的数据驻留在 RAM 中的位置.如果您的数据来回发送到硬盘以便为其他进程提供更多空间,这种情况就会发生变化.

Physical address: The address you'll never reach if you're running on top of an OS. It's where your data, regardless of its virtual address, resides in RAM. This will change if your data is sent back and forth to the hard disk to accommodate more space for other processes.

我上面提到的所有内容,虽然它是整个概念的简化版本,但就是所谓的计算机系统的内存管理部分.

All of what I have mentioned above, although it's a simplified version of the whole concept, is what's called the memory management part of the the computer system.

  • 进程不能相互访问内存,每个进程都有自己的虚拟地址,每个进程对不同区域的转换不同,即使有时您可能会发现两个进程试图访问同一个虚拟地址.
  • 该系统作为缓存系统运行良好,您通常不会使用可用的全部 4GB,那么为什么要浪费它呢?让其他人分享并让他们也使用它;当您的进程需要更多时,操作系统会从 HD 中获取您的数据并替换其他进程的数据,当然需要付出一定的代价.

这篇关于物理/逻辑/虚拟内存地址的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆