Where is linux shared memory actually located?

12,116

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.

For example consider the following :

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

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

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

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.


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 (PFN stands for Page Frame Number).

Virtual memory maps for 2 processes

Share:
12,116
skanzariya
Author by

skanzariya

Updated on June 05, 2022

Comments

  • skanzariya
    skanzariya almost 2 years

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

    I am aware about the process's virtual memory send boxes, they are different from process to process and processes don't see each other's 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 returned from shmat function, however, the processes have different addresses but same values.

    Here is the write program.

    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;
    }
    

    Here is the reader program.

    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?

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

    • user2760375
      user2760375 over 10 years
      their real physical addresses are shared
    • tez
      tez over 9 years
      As you've understood the explanation given in the answer, please accept it
  • skanzariya
    skanzariya over 10 years
    OK. Got it. Thanks for the detailed explanation. Now clear. But In the link shared by me in that I was trying to share a process address using shared memory and in second example I was trying to read the address and value on that address, but I can only either share address or value, not both.
  • TheCodeArtist
    TheCodeArtist over 10 years
    A virtual address is valid only in it process context. Once a context-switch occurs and a different process is running, the new page tables loaded may not contain a physical location mapping for that particular address causing a segfault. Even if con-incidentally a physical memory location is indeed mapped to the same virtual address in the new process, it is NOT guaranteed to point to the same physical memory location as in the original process. Go through this answer to properly understand the problem you are facing.