title: Where is linux shared memory actually located?
body: <p>I just wanted to know where shared memory resides in a Linux system? Is it in physical memory or virtual memory?</p>
  
  <p>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 <code>shmat</code> function, however, the processes have different addresses but same values.</p>
  
  <p>Here is the write program.</p>
  
  <p><strong>write.c</strong></p>
  
  <pre><code>#include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  
  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 *)&amp;ptr);
    printf("Shm value is %d \n", *(int *)shm);
    return;
  }
  </code></pre>
  
  <p>Here is the reader program.</p>
  
  <p><strong>read.c</strong></p>
  
  <pre><code>#include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  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 *)&amp;p, *p);
    printf("Shared value is %d \n", *(int *)shm);
    return 0;
  }
  </code></pre>
  
  <p>It would be great if someone could please explain in detail how the processes see the same value despite having different addresses?</p>
  
  <p>This question comes from <a href="https://stackoverflow.com/questions/21279725/c-pass-void-pointer-using-shared-memory">C pass void pointer using shared memory</a>.</p>