哪里是Linux共享内存实际上是在什么位置? [英] Where is linux shared memory actually located?

查看:155
本文介绍了哪里是Linux共享内存实际上是在什么位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道共享内存驻留在Linux系统?它是在物理内存或虚拟内存?

I just wanted to know where shared memory resides in a Linux system? Is it in physical memory or virtual memory?

我知道有关该进程的虚拟内存发送中,他们从不同的工艺处理和过程不看对方的内存,但我们可以利用IPC进程之间传递数据。为了实现简单的场景我刚刚创建了一个简单的共享内存的程序,并尝试打印从的shmat 函数的共享内存地址和价值回报,但是这两个过程有不同的地址,但相同的值。

I am aware about the process's virtual memory send box, they are different from process to process and processes don't see each other memory, but we can pass the data between processes using IPC. To implement the simple scenario I have just created a simple shared memory program and try to print the shared memory address and value return from shmat function, however both the processes has different address but same value.

下面是写程序。

为write.c

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int main() {

  key_t key=1235;
  int shm_id;
  void *shm;

  int *ptr = 83838;

  shm_id = shmget(key,10,IPC_CREAT | 0666);
  shm = shmat(shm_id,NULL,NULL);

  sprintf(shm,"%d",ptr);

  printf("Address is %p, Value is %p \n", (void *)shm, (void *)&ptr);
  printf("Shm value is %d \n", *(int *)shm);
  return;
}

下面是阅读器程序。

read.c

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

  key_t key=1235;
  int shm_id;
  void *shm;

  int *p = (int *)malloc(sizeof(int));
  shm_id = shmget(key,10,NULL);
  shm = shmat(shm_id,NULL,NULL);
  if(shm == NULL)
  {
    printf("error");
  }
  sscanf(shm,"%d",p);
  printf("Address is %p %p %p %d\n",(void *)shm, (void *)p, (void *)&p, *p);
  printf("Shared value is %d \n", *(int *)shm);
  return 0;
}

这将是巨大的,如果有人能请详细解释了如何处理同一个值虽然具有不同地址的?

It would be great if someone could please explain in detail how the processes see the same value despite having different addresses?

使用共享内存这问题来自 C中传递空指针

This question comes from C pass void pointer using shared memory.

感谢。

推荐答案

这是致力于所有的内存是物理。

All memory that is committed is physical.

但进程不能直接解决物理内存。他们有哪些内核会解析到物理地址的虚拟地址。当一个共享内存区域是设置,相同的物理存储器位置是由多个进程处理。然而,虚拟地址可以是不同的,但。每个进程使用它仅在其自己的上下文接收的虚拟地址。两个虚拟地址指的是相同的物理存储器中。

However processes cannot address physical memory directly. They have virtual addresses which the kernel will resolve to physical addresses. When a shared memory region is setup, the same physical memory location is addressed by the multiple processes. However the virtual addresses can be different though. Each process uses the virtual address it received only in its own context. Both the virtual addresses refer to the same physical memory.

要详细描述,在共享存储器区域的情况下,在相同的物理存储器地址是寻址多个进程同时既作为进程具有指向相同的物理地址的虚拟地址。

To elaborate, in case of a shared memory region, the same physical memory address is addressable by multiple processes simultaneously as both the processes have virtual addresses that point to the same physical address.

例如考虑以下几点:


  • 虚拟地址的 V1 (适用过程中的 T1 上下文)

  • 虚拟地址的 V2 (适用过程中的 T2 上下文)

  • virtual address V1 ( in process T1 context)
  • virtual address V2 ( in process T2 context)

都指向共享存储器区域中。结果,
这实际上是一种常见的物理地址的 P

are both pointing to the shared memory region.
This is actually a common physical address P.

现在,结果
流程 T1 引用虚拟地址的 V1 结果
的结果
流程 T2 引用虚拟地址的 V2

Now,
Process T1 referencing virtual address V1
OR
Process T2 referencing virtual address V2

将导致到物理地址的访问的 P 作为内核存储器同时转换的虚拟地址的位置,以便在相同的物理位置。

will result in an access to physical address P as the kernel translates both the virtual address locations to the same physical location in memory.

例如,在下面的图中,物理内存的 PFN4 是由流程点¯x共享和Y使用的 VPFN3 VPFN1 在各自的上下文。

For example, in the following diagram, the physical memory PFN4 is shared by processes X and Y using VPFN3 and VPFN1 in their respective contexts.

这篇关于哪里是Linux共享内存实际上是在什么位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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