每个进程的内核虚拟内存包含什么? [英] What does the Kernel Virtual Memory of each process contain?

查看:37
本文介绍了每个进程的内核虚拟内存包含什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 3 个程序(可执行文件)加载到内存中时,布局可能如下所示:

When say 3 programs (executables) are loaded into memory the layout might look something like this:

替代文字 http://img97.imageshack.us/img97/3460/processesm.jpg

我有以下问题:

  1. 虚拟内存的概念是否仅限于用户进程?因为,我想知道操作系统内核、驱动程序在哪里?它的内存布局如何?我想了解更多关于内核端内存的信息.我知道它的操作系统特定于你的选择(windows/linux).

  1. Is the concept of Virtual Memory limited to user processes? Because, I am wondering where does the Operating System Kernel, Drivers live? How is its memory layout? I want to know more about kernel side memory. I know its operating system specific make your choice (windows/linux).

虚拟内存的概念是基于每个进程的吗?我的意思是说 4GB 的 process1 + 4GB 的 process2 + 4GB 的 process3 = 12GB 的虚拟内存(对于所有进程)是否正确.这听起来不对.或者从总共 4GB 的空间中,1GB 被内核占用其余 3GB 与所有进程共享.

Is the concept of Virtual Memory per process basis? I mean is it correct for me to say 4GB of process1 + 4GB of process2 + 4GB of process3 = 12GB of virtual memory (for all processes). This does't sound right. Or from a total of 4GB space 1GB is taken by kernel & rest 3GB is shared b/w all processes.

他们说,在 4GB 地址空间中的 32 位机器上.其中一半(或最近的 1GB)被内核占用.我可以在此图中看到内核虚拟内存"占用 0xc0000000 - 0xffffffff (= 1 GB).他们在谈论这个吗?或者是别的什么?只是想确认一下.

They say, on a 32 bit machine in a 4GB address space. Half of it (or more recently 1GB) is occupied by kernel. I can see in this diagram that "Kernel Virtual memory" is occupying 0xc0000000 - 0xffffffff (= 1 GB). Are they talking about this? or is it something else? Just want to confirm.

每个进程的内核虚拟内存究竟包含什么?它的布局是怎样的?

What exactly does the Kernel Virtual Memory of each of these processes contain? What is its layout?

当我们做 IPC 时,我们谈论的是共享内存.我没有看到这些进程之间共享任何内存.它住在哪里?

When we do IPC we talk about shared memory. I don't see any memory shared between these processes. Where does it live?

资源(文件、Windows 中的注册表)对所有进程都是全局的.因此,资源/文件句柄表必须在某个全局空间中.那会在哪个区域?

Resources (files, registries in windows) are global to all processes. So, the resource/file handle table must be in some global space. Which area would that be in?

我在哪里可以了解更多关于内核方面的信息.

Where can I know more about this kernel side stuff.

推荐答案

  1. 当系统使用虚拟内存时,内核也使用虚拟内存.Windows 将使用上面的 2GB(如果您在 Windows 引导加载程序中指定了/3GB 开关,则为 1GB)供自己使用.这包括内核代码、数据(或者至少是分页的数据——没错,Windows 可以将部分内核地址空间分页到硬盘)和页表.

  1. When a system uses virtual memory, the kernel uses virtual memory as well. Windows will use the upper 2GB (or 1GB if you've specified the /3GB switch in the Windows bootloader) for its own use. This includes kernel code, data (or at least the data that is paged in -- that's right, Windows can page out portions of the kernel address space to the hard disk), and page tables.

每个进程都有自己的虚拟机地址空间.当发生进程切换时,页表通常会与另一个进程的页表交换.这在 x86 处理器上很简单 - 更改 CR3 控制寄存器中的页表基地址就足够了.整个 4GB 地址空间被替换为完全不同的 4GB 地址空间的表.话虽如此,通常会有一些地址空间区域在进程之间共享.这些区域在页表中用特殊标志标记,向处理器指示这些区域不需要在处理器的 翻译后备缓冲区.

Each process has its own VM address space. When a process switch occurs, the page tables are typically swapped out with another process's page table. This is simple to do on an x86 processor - changing the page table base address in the CR3 control register will suffice. The entire 4GB address space is replaced by tables replacing a completely different 4GB address space. Having said that, typically there will be regions of address space that are shared between processes. Those regions are marked in the page tables with special flags that indicate to the processor that those areas do not need to be invalidated in the processor's translation lookaside buffer.

正如我之前提到的,内核的代码、数据和页表本身需要位于某个地方.此信息位于内核地址空间中.内核代码、数据和页表的某些部分可能会根据需要自行换出到磁盘.有些部分被认为比其他部分更重要,根本不会换掉.

As I mentioned earlier, the kernel's code, data, and the page tables themselves need to be located somewhere. This information is located in the kernel address space. It is possible that certain parts of the kernel's code, data, and page tables can themselves be swapped out to disk as needed. Some portions are deemed more critical than others and are never swapped out at all.

见(3)

视情况而定.用户模式共享内存位于用户模式地址空间中.部分内核模式地址空间也很可能在进程之间共享.例如,内核代码在系统中的所有进程之间共享的情况并不少见.该内存所在的位置并不精确.我在这里使用了任意地址,但一个进程中位于 0x100000 的共享内存可能位于另一个进程中的 0x101000 处.不同地址空间中的两个页面,完全不同的地址,可以指向同一个物理内存.

It depends. User-mode shared memory is located in the user-mode address space. Parts of the kernel-mode address space might very well be shared between processes as well. For example, it would not be uncommon for the kernel's code to be shared between all processes in the system. Where that memory is located is not precise. I'm using arbitrary addresses here, but shared memory located at 0x100000 in one process might be located at 0x101000 inside another process. Two pages in different address spaces, at completely different addresses, can point to the same physical memory.

我不确定你的意思.打开文件句柄不是对所有进程都是全局的.存储在硬盘上的文件系统对所有进程都是全局的.在Windows下,文件句柄由内核管理,对象存放在内核地址空间,由内核对象管理器管理.

I'm not sure what you mean here. Open file handles are not global to all processes. The file system stored on the hard disk is global to all processes. Under Windows, file handles are managed by the kernel, and the objects are stored in the kernel address space and managed by the kernel object manager.

对于基于 Windows NT 的系统,我推荐 Windows Internals,第 5 版,作者:Mark Russinovich 和 David Solomon

For Windows NT based systems, I'd recommend Windows Internals, 5ed by Mark Russinovich and David Solomon

对评论的回应:

现在这 3GB 是黑白共享的流程?或者每个进程有4GB空间?

And now this 3GB is shared b/w all processes? or each process has 4GB space?

这取决于操作系统.一些内核(例如 L4 微内核)使用相同的页表用于多个进程并使用 segmentation 分隔地址空间.在 Windows 上,每个进程都有自己的页表.请记住,即使每个进程都可能获得自己的虚拟地址空间,但这并不意味着物理内存总是不同的.例如,在进程 A 中加载的 kernel32.dll 的映像与进程 B 中的 kernel32.dll 共享.大部分内核地址空间也在进程之间共享.

It depends on the OS. Some kernels (such as the L4 microkernel) use the same page table for multiple processes and separate the address spaces using segmentation. On Windows each process gets its own page tables. Remember that even though each process might get its own virtual address space, that doesn't mean that the physical memory is always different. For example, the image for kernel32.dll loaded in process A is shared with kernel32.dll in process B. Much of the kernel address space is also shared between processes.

为什么每个进程都有内核虚拟内存?

Why does each process have kernel virtual memory?

思考这个问题的最好方法是问自己,如果内核不使用虚拟内存执行,它会如何工作?"在这种假设情况下,每次您的程序导致上下文切换到内核(假设您进行了系统调用)时,都必须在 CPU 在内核空间中执行时禁用虚拟内存.这样做是有成本的,当您切换回用户空间时,重新打开它也是有成本的.

The best way to think of this is to ask yourself, "How would a kernel work if it didn't execute using virtual memory?" In this hypothetical situation, every time your program caused a context switch into the kernel (let's say you made a system call), virtual memory would have to be disabled while the CPU was executing in kernel space. There's a cost to doing that and there's a cost to turning it back on when you switch back to user space.

此外,假设用户程序传递了一个指向某些数据的指针以供其系统调用.这个指针是一个虚拟地址.您已经关闭了虚拟内存,因此需要先将指针转换为物理地址,然后内核才能对其进行任何操作.如果您打开了虚拟内存,由于 CPU 上的内存管理单元,您可以免费获得它.相反,您必须手动翻译软件中的地址.我可以描述各种示例和场景(一些涉及硬件,一些涉及页表维护等),但其要点是拥有同质内存管理方案要容易得多.如果用户空间正在使用虚拟内存,那么如果您在内核空间中维护该方案,则编写内核会更容易.至少这是我的经验.

Furthermore, let's suppose that the user program passed in a pointer to some data for its system call. This pointer is a virtual address. You've got virtual memory turned off, so that pointer needs to be translated to a physical address before the kernel can do anything with it. If you had virtual memory turned on, you'd get that for free thanks to the memory-management unit on the CPU. Instead you'd have to manually translate the addresses in software. There's all kinds of examples and scenarios that I could describe (some involving hardware, some involving page table maintenance, and so on) but the gist of it is that it's much easier to have a homogeneous memory management scheme. If user space is using virtual memory, it's going to be easier to write a kernel if you maintain that scheme in kernel space. At least that has been my experience.

只有一个操作系统实例内核对吗?那么为什么每个过程有单独的内核虚拟空间吗?

there will be only one instnace of OS kernel right? then why each process has seperate kernel virtual space?

正如我上面提到的,相当多的地址空间将在进程之间共享.内核空间中的每个进程数据在进程之间的上下文切换期间被换出,但由于只有一个内核,因此其中很多是共享的.

As I mentioned above, quite a bit of that address space will be shared across processes. There is per-process data that is in the kernel space that gets swapped out during a context switch between processes, but lots of it is shared because there is only one kernel.

这篇关于每个进程的内核虚拟内存包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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