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

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

问题描述

我对操作系统中的物理/逻辑/虚拟地址这些术语有些困惑(我使用 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?

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

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 位系统意味着您的程序会认为它有 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).

如果您在所有链接发生后阅读编译代码的程序集,您将使用这些地址访问它们,但是(通常未使用)变量不会真正存在于 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 会注意到这个未命中"并将其从硬盘中提取到 RAM 中以供 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天全站免登陆