什么是计算机科学中的分段和分页? [英] What is segmentation and paging in Computer Science?

查看:332
本文介绍了什么是计算机科学中的分段和分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索了很长时间,但是我仍然不知道它是如何工作的,因为大多数解释都是非常技术性的,并且没有插图可以使它更清晰.我的主要困惑是,它与虚拟内存的区别是什么?

I Googled for a long time but I still don't understand how it works as most of the explanation are very technical and there are no illustrations to make it clearer. My primary confusion is that what is its'difference with virtual memory?

我希望这个问题在这里能得到很好的解释,以便其他提出相同问题的人在搜索Google时都能在这里找到它.

I hope this question will have a very good explanation here so that other people who ask the same question can find it here when they Google it.

推荐答案

我必须承认,这两个概念在开始时看起来可能非常复杂且相似.有时,它们也会被令人困惑地教导.我认为可以在osdev.org上找到一个很好的参考:细分

I have to admit, those two concepts can seem quite complicated and similar at the beginning. Sometimes they are also taught confusingly. A good reference in my opinion can be found on osdev.org: Segmentation Paging

为了完整起见,我也会尝试在这里进行解释,但是由于我已经几个月没有开发OS了,所以我不能保证正确性.

For sake of completion, I'll try to explain it here too, but I cannot guarantee correctness, as I have not developed OS for some months.

细分是这两个概念中的较老版本,我认为它更加令人困惑.细分适用-顾名思义-细分.段是特定大小的连续内存块.要访问每个段中的内存,我们需要一个偏移量.这总共有两个地址成分,它们实际上存储在两个寄存器中.分段的一种想法是扩大仅具有16位寄存器的内存.另一个是某种保护,但不像分页中的保护那么详细.

Segmentation is the older of both concepts and it is in my opinion the more confusing. Segmentation works on - as the name says - segments. A segment is a continuous block of memory of a specific size. To access memory within each segment we need an offset. This makes a total of two address components, which are in fact stored in two registers. One idea of segmentation was to enlarge memory having only 16-bit registers. The other was some sort of protection, but not as elaborate as that one of paging.

因为我们现在使用两个寄存器来访问内存,所以我们可以将内存分成多个块-如上所述,即所谓的段.考虑1MB(2 ^ 20)的内存.可以将其划分为每16个字节的65536个(2 ^ 16,因为有16位寄存器)段.当然,我们也有16位的偏移量寄存器.用16位寻址16个字节是没有用的,因此决定了段可以重叠(那时候我也有性能和编程上的原因).

Because we use two registers to access memory now, we can split memory into chunks - as said above, the so called segments. Consider a memory of 1MB (2^20). This can be split into 65536 (2^16, because 16 bits registers) segments of each 16 bytes. Of course, we also have 16 bits registers for the offset. Addressing 16 bytes with 16 bits is quite useless, so it was decided that segments can overlap (which I think also had performance and programming reasons back then).

以下公式用于通过分段访问1MB内存:

The following formula is used to access 1MB of memory with segmentation:

Physical address = (A * 0x10) + B

这意味着该段将是偏移量的16倍.这也意味着可以通过多种方式访问​​地址0x0100,例如A = 0x010和B = 0x0,但A = 0x0和B = 0x0100.

This means the segment will be 16 times the offset. This also means that the address 0x0100 can be accessed in many ways, e.g. by A=0x010 and B=0x0, but also by A=0x0 and B=0x0100.

这是过去16位时代的细分.

This was segmentation in the old 16bit days.

如果您查看汇编程序或自己尝试一下,您会发现它们甚至在汇编程序中都有所谓的寄存器:CS和DS(代码段和数据段).

If you look at assembler programs or try something yourself, you'll see they even have so called registers in assembler: CS and DS (code segment and data segment).

后来又引入了所谓的全局描述符表(GDT).这是一个全局表(在RAM中的特定位置),其中给出了段号和内存地址以及每个段的其他几个选项.这使我们更接近分页的概念,但是仍然不一样.

Later a so called Global Descriptor Table (GDT) was introduced. This is a global table (at a specific position in your RAM) in which segment numbers and memory addresses and several other options for each segment are given. This brings us nearer to the concept of paging, but it's still not the same.

因此,程序员本人现在可以决定应从何处开始.还有一个新的概念是,在GDT中,人们可以决定一个段应该持续多长时间.因此,并非每个段都必须长64kB(由于16位寄存器,所以长度为2 ^ 16),但是限制可以由程序员定义.您可以有重叠的细分,也可以有完全分开的细分.

So now the programmer himself can decide where segments should start. A new concept also was that in the GDT one could decide how long a segment should be. So not each segment had to be 64kB long (2^16, because of 16 bit registers), but the limit could be defined by the programmer. You could have overlapping segments or also purely separated segments.

现在访问A:B(还有两个用于访问内存的寄存器)时,A将成为GDT中的条目.因此,我们将在GDT中查找第A个条目,并查看该段从哪个存储位置开始以及该段的大小.然后,我们检查B(偏移量)是否在允许的内存区域内.

When accessing A:B now (still two registers used for accessing memory), A will be the entry in the GDT. So we'll look up the A'th entry in the GDT and see at which memory location the segment starts and how large it is. We then check if B (offset) is within the allowed memory area.

现在分页与较新的分段方法没有太大不同,但是在分页时,每个页面的大小都是固定的.因此,该限制不再可编程,每个页面(当前)为4kb.此外,与分段不同,逻辑地址空间可以连续,而物理地址不连续.

Now paging is not so different from the newer segmentation approach, but at paging each page has a fixed size. So the limit is no longer programmable, each page has (currently) 4kb. Furthermore, unlike at segmentation, the logical address space can be continuous without the physical addresses being continuous.

分页还使用表来查找内容,您仍然将逻辑地址拆分为多个部分.第一部分是页表中条目的编号,第二部分是偏移量.但是,现在偏移量具有固定的12位长度,可以访问4kb.您也可以有两个以上的部分,然后将使用多个页表.两级页表很常见,对于64位系统,我认为甚至三级页表也很常见.

Paging also uses tables to look up stuff and you still split the logical address into parts. The first part is the number of the entry in the page table, the second part is the offset. However, now the offset has a fixed length of 12 bits to access 4kb. You can also have more than two parts, then multiple page tables will be used. Two level page tables are quite common, for 64bit systems I think even three level page tables are common.

我希望我至少能够解释一下,但是我认为我的大腿也很混乱.最好的办法是进入内核编程,并在启动OS时尝试实现最基本的东西.然后,您将找到所有内容,因为由于向后兼容性,所有内容仍在我们的现代PC上.

I hope I was able to explain it at least a bit, but I think my exaplanation was also confusing. Best thing is to dive into kernel programming and try to implement the most basic stuff when booting an OS. Then you'll find out everything, because due to backwards compability everything is still on our modern PCs.

这篇关于什么是计算机科学中的分段和分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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