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

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

问题描述

在说3程序(可执行文件)加载到内存中的布局可能看起来是这样的:

我有以下几个问题:


  1. 虚拟内存的概念仅限于用户进程?因为,我不知道哪里操作系统内核,驱动程序住在哪里?如何是它的内存布局?我想知道更多有关内核端内存。我知道它的操作系统特定做出选择(Windows / Linux版)。


  2. 每个进程的基础虚拟内存的概念?我的意思是正确的,我说的过程1 + 4GB的过程2 + 4GB的process3 4GB = 12GB的虚拟内存(​​所有进程)。这个简化版,声音的权利。或从总4GB空间1GB的采取的是内核和放大器;其余3GB共享的B / W的所有进程。


  3. 他们说,在4GB的地址空间中的32位计算机上。它(或最近1GB),一半是由内核占用。为0xffffffff(= 1 GB) - 我这个图是内核虚拟内存正处在0xC0000000的看到。他们在谈论这个?抑或是其他什么东西?只是想确认。


  4. 什么究竟请问内核虚拟每个流程的记忆包含哪些内容?什么是它的布局?


  5. 当我们做IPC我们谈论共享内存。我没有看到这些进程间共享的内存。它在哪里住?


  6. 资源(在W​​indows中的文件,注册表)是全局的所有进程。因此,资源/文件句柄表必须在一些全球性的空间。哪个地区会是这样吗?


  7. 我在哪里可以更多地了解该内核端的东西。



解决方案

  1. 当系统使用虚拟内存,内核使用的虚拟内存。 Windows将使用上2GB(1GB或者如果您指定在Windows启动加载器/ 3GB开关),供自己使用。这包括内核code,数据(或者至少是在分页中的数据​​ - 这是正确的,Windows可以在内核地址空间以硬盘的部分页面出来),和页表


  2. 每个进程都有自己的虚拟地址空间。当一个进程开关发生时,页表通常与另一进程的页表换出。这是简单的x86处理器上做的 - 改变在 CR3页表基址控制寄存器就足够了。整个4GB的地址空间是由表取代一个完全不同的4GB的地址空间取代。话虽如此,通常会出现的那些进程间共享地址空间的区域。这些区域被标记为具有特殊标志的页表指示到那些区域不需要在处理器的翻译后备将被无效的处理器缓冲


  3. 正如我前面提到的,内核的code,数据和页表本身必须位于某处。此信息位于内核地址空间。这是可能的内核的code,数据和页表的某些部分可以根据需要自行交换到磁盘。某些部分被认为比其他人更重要,不会被交换出去的。


  4. 请参阅(3)


  5. 这要看情况。用户模式共享存储器位于用户模式的地址空间。内核模式地址空间的部分很可能会在进程间共享为好。例如,它不会是少见到系统中的所有进程间共享内核​​的code。凡内存坐落不是precise。我使用的任意地址在这里,但共享内存在一个进程可能位于 0x101000 在另一个位于的0x100000 处理。在不同地址空间的两页,在完全不同的地址,可以指向相同的物理内存。


  6. 我不知道你的意思在这里。打开文件句柄不是全局性的所有进程。存储在硬盘上的文件系统是全球的所有过程。在Windows中,文件句柄由内核管理,并且将对象存储在内核地址空间和由内核对象管理器管理


  7. 有关基于Windows NT的系统,我建议你 Windows内部,5ED 通过标记Russinovich和大卫所罗门


回应评论:


  

和现在这个3GB共享的B / W的所有
  流程?或者每个进程都有4GB
  空间?


这取决于操作系统。有些内核(如 L4 微内核)使用相同的页面表中多个进程并分开使用分割的地址空间。在Windows中每个进程都有自己的页表。请记住,即使每个进程可能会获得自己的虚拟的地址空间,这并不意味着的物理的内存始终是不同的。例如, KERNEL32.DLL 进行中的加载的图像与 KERNEL32.DLL 过程B.许多共享内核地址空间也进程间共享。


  

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


要想到这一点的最好办法就是问自己:怎么会一个内核工作,如果它没有使用虚拟内存执行?在这个假设的情况下,每个程序引起的上下文切换时间进入内核(假设你做了一个系统调用),虚拟内存就必须在CPU在内核空间执行被禁用。有在做一个成本,有一个成本,把它回来时,你切换回用户空间。

此外,假设在一个指针传递给一些数据的系统调用用户程序。这个指针是一个虚拟地址。你有虚拟内存关闭,因此该指针需要被翻译为物理地址之前,内核可以用它做任何事情。如果你有虚拟内存开启时,你会得到免费多亏了CPU的内存管理单元。相反,你必须在软件中手动转换的地址。有各种各样的示例和方案,我可以描述(包括一些硬件,一些涉及页表的维护,等等),但依据的是,它更容易有一个均匀的内存管理方案。如果用户空间使用虚拟内存,它会更容易,如果你保持在内核空间的方案编写内核。至少,这是我的经验。


  

会有操作系统的只有一个instnace
  内核吧?那么为什么每个进程
  拥有独立内核的虚拟空间?


正如我上面提到的,相当多的地址空间将被跨进程共享。存在着在该获取进程之间的上下文切换期间换出内核空间每个进程的数据,但它的很多是共享的,因为仅存在一个内核。

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

I've following questions:

  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).

  2. 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.

  3. 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.

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

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

  6. 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?

  7. Where can I know more about this kernel side stuff.

解决方案

  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.

  2. 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.

  3. 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.

  4. See (3)

  5. 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.

  6. 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.

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

Response to comment:

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

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?

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.

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天全站免登陆