简体   繁体   English

为什么共享内存(在ipc中)不需要上下文切换? 它是从内核空间映射到用户空间的内存吗?

[英]Why does a shared memory (in ipc) not require context switching ? Is it a memory from kernel space that gets mapped to user space?

I want to understand if shared memory gets memory allocation from kernel space then why it is not going through context switching ? 我想了解共享内存是否从内核空间获取内存分配,那么为什么不通过上下文切换呢? And if it is not from kernel space then from where this memory gets allocated. 如果不是来自内核空间,则从那里分配内存。

In most modern computers memory isn't allocated from kernel space. 在大多数现代计算机中,内存不是从内核空间分配的。 Rather the kernel finds a page of physical memory and then maps it into a process at a virtual address that the process is not currently using. 内核会找到物理内存的页面,然后将其映射到进程当前未使用的虚拟地址的进程。 The physical address and the virtual address in the process are not the same. 进程中的物理地址和虚拟地址不同。 So the memory is always "user space" memory. 因此,内存始终是“用户空间”内存。 This is all part of the Virtual Memory subsystem. 这是虚拟内存子系统的全部。

To share the physical page between processes the kernel maps the page into both processes. 为了在进程之间共享物理页面,内核将页面映射到两个进程中。 Usually at the same virtual address in both. 通常都在两个相同的虚拟地址上。 Once this is done the kernel is no longer involved as both processes have the same physical memory mapped at that location. 完成此操作后,将不再涉及内核,因为两个进程在该位置具有相同的物理内存映射。 Thus any change will show up for both. 因此,任何更改都会同时出现。

Note: Kernel memory is memory usually only accessible to the kernel and is a different concept. 注意:内核内存是通常只能由内核访问的内存,这是一个不同的概念。

When shmget() call is made with a requirement of some memory then it makes context switching from user to kernel space, system call service routine runs in kernel with the arguments passed from the user space to revert back with the required memory space [and this memory page is not a part of kernel space its just that its not yet mapped into process memory yet] which gets mapped to process local address space 如果在需要一些内存的情况下进行shmget()调用,然后将上下文从用户空间切换到内核空间,则系统调用服务例程将在内核中运行,并带有从用户空间传递来的参数,以使用所需的内存空间还原[内存页不是内核空间的一部分,只是它尚未映射到进程内存中],该页已映射到进程本地地址空间

So this means there is a reserved memory in memory management which is not a part of kernel memory nor it is mapped in process local address space and this memory is used to facilitate such request. 因此,这意味着在内存管理中有一个保留的内存,它不是内核内存的一部分,也不被映射到进程本地地址空间中,并且该内存用于促进这种请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM