title: Confused about mmap() return - two different pointers?
body: <p>Currently trying to understand how memory mapping works in Linux (or in general, really), and I'm following with this one example of Shared Memory in POSIX systems from Operating System Concepts. The two files are as follows:</p>
  <p><strong>Producer file</strong></p>
  <pre><code>// This is the producer file for the Shared memory object
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  
  #include &lt;sys/mman.h&gt;
  
  int main()
  {
  // ---------------- PRODUCER ESTABLISHES SHARED MEMORY OBJECT AND WRITES TO IT ----------------
  
      // Specifying the size in bytes of the shared memory object
      const int SIZE = 4096;
      
      // Name of the shared memory space
      const char *name = &quot;OS&quot;;
      
      // The actual strings to write to shared memory
      const char *message_0 = &quot;Hello&quot;;
      const char *message_1 = &quot;World!&quot;;
      
      // Shared memory file descriptor will be stored in this
      int fd;
      
      // Pointer to the shared memory object will be stored in this
      char *ptr;
      
      // Checking error
      int errnum;
      
      // Create a shared memory object. This opens (establishes a connection to) a shared memory object.
      fd = shm_open(name, O_CREAT | O_RDWR,0666);
      
      if (fd == -1)
      {
          errnum = errno;
          fprintf(stdout, &quot;Value of errno: %d\n&quot;, errno);
          perror(&quot;Error printed by perror&quot;);
          fprintf(stdout, &quot;Error opening file: %s\n&quot;, strerror(errnum));
          return 1;
      }
      
      // Configure the size of the shared-memory object to be 4096 bytes
      ftruncate(fd, SIZE);
      
      // Memory map the shared memory object
      ptr = (char *) mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      
      printf(&quot;Ptr is: %p\n&quot;, ptr);
      
      if (ptr == MAP_FAILED)
      {
          errnum = errno;
          fprintf(stdout, &quot;Value of errno in ptr: %d\n&quot;, errno);
          perror(&quot;Error printed by perror&quot;);
          fprintf(stdout, &quot;Error opening file: %s\n&quot;, strerror(errnum));
          return 1;
      }
      
      // Write to the shared memory object
      sprintf(ptr, &quot;%s&quot;, message_0);
      ptr += strlen(message_0);
      sprintf(ptr, &quot;%s&quot;, message_1);
      ptr += strlen(message_1);
      
      return 0;
  }
  </code></pre>
  <p><strong>Consumer file</strong></p>
  <pre><code>// This is the consumer file for the Shared memory object, in which it reads what is in the memory object OS
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  
  #include &lt;sys/mman.h&gt;
  
  int main()
  {
      // Size in bytes of shared memory object
      const int SIZE = 4096;
      
      // Name of the shared memory space
      const char *name = &quot;OS&quot;;
      
      // Shared memory file descriptor will be stored in this
      int fd;
      
      // Pointer to the shared memory object will be stored in this
      char *ptr;
      
      // Checking error
      int errnum;
      
      // Open the shared memory object
      fd = shm_open(name, O_RDWR, 0666);
      
      // If error in shm_open()
      if (fd == -1)
      {
          errnum = errno;
          fprintf(stdout, &quot;Value of errno: %d\n&quot;, errno);
          perror(&quot;Error printed by perror&quot;);
          fprintf(stdout, &quot;Error opening file: %s\n&quot;, strerror(errnum));
          return 1;
      }
      
      
      // Memory-map the shared memory object
      ptr = (char *) mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      
      printf(&quot;Ptr is: %p\n&quot;, ptr);
      
      if (ptr == MAP_FAILED)
      {
          errnum = errno;
          fprintf(stdout, &quot;Value of errno in ptr: %d\n&quot;, errno);
          perror(&quot;Error printed by perror&quot;);
          fprintf(stdout, &quot;Error opening file: %s\n&quot;, strerror(errnum));
          return 1;
      }
      
      // Read from the shared memory object
      printf(&quot;%s\n&quot;, (char *) ptr);
              
      // Remove the shared memory object (delete it)
      shm_unlink(name);
      
      return 0;
  }
  </code></pre>
  <p>When I print the pointer to the shared memory object (<code>printf(&quot;Ptr value: %p\n, ptr)</code>), I get two different values for the consumer and producer files. Why does this happen?</p>
  <p>As I understand, the pointer <code>ptr</code> points at the shared memory object, which is in the physical memory. This physical memory is just shared amongst two processes, by mapping it onto their address space. However, this would require that the pointer point to the same address in physical memory, no? Or is it pointing to the virtual memory (i.e. the address space of the processes)? If so, does that itself point to the physical memory?</p>
  <p>Thanks!</p>