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

问题描述

我正在阅读操作系统概念,我正在阅读第 8 章!但是,我可以使用一些澄清或保证我的理解是正确的.

逻辑地址:根据本书,逻辑地址由 CPU 生成.这到底是什么意思?(在执行生成的地址系统中......)我假设当为程序编译代码时,程序不知道代码将在哪里加载到内存中.编译器所做的只是设置程序布局的总体草图以及图像的布局方式,但不会为其分配任何实际地址.当程序执行时,CPU 获取编译器制作的这个布局图像,并将一些地址(逻辑地址)分发给从代码生成的地址.

物理地址:直到 CPU 生成一些逻辑地址集(由基地址和偏移量组成)后才会生成物理地址.逻辑地址通过 MMU 或其他设备,并且逻辑地址沿线的某处映射到物理 RAM 地址.

那么实际的区别是什么?我可以看到一个好处.使用逻辑地址为应用程序提供了更多的自由.如果物理地址是硬编码的,那么程序的成功将在很大程度上取决于物理计算机机器、可用的 RAM 地址等.

使用转换为物理地址的逻辑地址不是强加了两个步骤,而不是一对一,因此更多的开销吗?

那么逻辑地址在生成后驻留在何处?当 CPU 为进程服务时,它们可能存在于 CPU 上的寄存器中,但在此之前和之后,它们去哪儿了?我知道这取决于实现.我假设它们可能存储在 CPU 上的某些特殊寄存器空间或缓冲区中,例如 TLB,对吗?如果不是,那么该表可能存在于实际的 RAM 中,而 CPU 只保存一个指向 RAM 中表基地址的指针/地址,对吗?

似乎将地址保存在 RAM 中与逻辑内存地址的目的相反.我只能假设我的理解是不正确的.

解决方案

这个答案绝不是详尽无遗的,但它可以解释得足以让事情变得有趣.

在虚拟内存系统中,逻辑地址和物理地址之间存在脱节.

可以为应用程序提供(假设)4G 的虚拟地址空间.这是它的可用内存,它可以随意使用.这是一个很好的连续内存块(从应用程序的角度来看).

然而,它不是唯一运行的应用程序,操作系统必须在它们之间进行调解.在这个漂亮的连续模型之下,有很多映射正在进行,以将逻辑地址转换为物理地址.

通过这种映射,操作系统和硬件(从现在开始我将它们称为较低层)可以自由地将应用程序页面放在任何它想要的位置(在物理内存中或换出到二级存储).

当应用程序尝试访问逻辑地址 50 处的内存时,较低级别可以使用转换表将其转换为物理地址.而且,如果它尝试访问已换出到磁盘的逻辑内存,则会引发页面错误,并且较低级别可以将相关数据带回内存,无论其物理地址是什么.>

在过去只有物理地址的糟糕日子里,代码必须可重定位(或在加载时修复),因为它可以加载到任何地方.使用虚拟内存,该代码(和数据)可以同时在十几个不同的进程中位于逻辑内存位置 50 - 但是它的实际物理地址会有所不同.

它甚至可以共享,以便一个物理副本同时存在于多个进程的地址空间中.这是共享代码(因此我们不会使用比我们需要的更多的物理内存)和共享内存以允许轻松的进程间通信的关键.

当然,它比纯物理地址环境效率低,但 CPU 制造商试图使其尽可能高效,因为它被大量使用.优点大于缺点.

I am reading Operating Systems Concept and I am on the 8th chapter! However I could use some clarification, or reassurance that my understanding is correct.

Logical Addresses: Logical addresses are generated by the CPU, according to the book. What exactly does this mean? (In an execute-generated address system..) I assume when code is compiled for a program, the program has no idea where the code will be loaded in memory. All the compiler does is set up a general sketch of the program layout and how the image should be laid out, but doesn't assign any real addresses to it. When the program is executed the CPU takes this layout image that the compiler made and hands out some addresses (logical ones) to the ones generated from the code.

Physical Addresses: The physical addresses are not generated until after the CPU generates some set of logical addresses (consisting of a base address and an offset). The logical addresses go through the MMU or another device and somewhere along the line the logical addresses are mapped to physical RAM addresses.

What then is the actual difference? I can see one benefit. Using logical addresses gives more freedom to the applications. If the physical addresses were hard coded, then the program success would depend heavily on the physical computer machine, available RAM addresses etc.

Doesn't the use of logical addresses converted to physical address impose two steps instead of a one to one, and therefore more over head?

Where then do the logical addresses reside after generation? They may exist in a register on the CPU while the CPU is servicing a process, but before and after, where do they go? I understand this is implementation dependent. I assume they may be stored in some special register space or buffer on the CPU such as a TLB, correct? If not, then the table may exist in the actual RAM itself, and the CPU only holds a pointer/address to the base address of the table in RAM, correct?

It seems holding the addresses in RAM is counter productive to the purpose of logical memory addresses. I can only assume my understanding is incorrect.

解决方案

This answer is by no means exhaustive but it may explain it enough to make things click.

In virtual memory systems, there is a disconnect between logical and physical addresses.

An application can be given a virtual address space of (let's say) 4G. This is its usable memory and it's free to use it as it sees fit. It's a nice contiguous block of memory (from the point of view of the application).

However, it is not the only application running, and the OS has to mediate between them all. Underneath that nice contiguous model, there is a lot of mapping going on to convert logical to physical addresses.

With this mapping, the OS and hardware (I'll just call these the lower layers from here on in) is free to put the application pages anywhere it wants (either in physical memory or swapped out to secondary storage).

When the application tries to access memory at logical address 50, the lower levels can translate that to a physical address using translation tables. And, if it tries to access logical memory that's been swapped out to disk, a page fault is raised and the lower levels can bring the relevant data back into memory, at whatever physical address it wants.

In the bad old days when physical addresses were all you had, code had to be relocatable (or fixed up on load) since it could load anywhere. With virtual memory, that code (and data) can be at logical memory location 50 in a dozen different processes at the same time - it's actual physical address will be different however.

It can even be shared so that one physical copy exists in the address space of many processes at once. This is the crux of shared code (so we don't use more physical memory than we need) and shared memory to allow easy inter-process communication).

It is, of course, less efficient than a pure physical-address environment but the CPU manufacturers try to make it as insanely efficient as possible, since it's used heavily. The advantages far outweigh the disadvantages.

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

06-30 10:39