title: shm_open and ftruncate() in Shared Memory Programming
body: <p>I want to create a Shared Memory Object and truncate it to a specific size. </p>
  
  <p>SHMSIZE is defined with 512</p>
  
  <p>MODE is set with S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH</p>
  
  <p>Here is my Code</p>
  
  <pre class="lang-c prettyprint-override"><code>char *shm_name = "SharedMemory";    
  int fd; 
  
  /* Open an Shared Memory Object for Read-/Write-Access */    
  if((fd = shm_open(shm_name, O_RDWR | O_CREAT, MODE) &lt; 0)) {     
       perror("\nshm_open() in Caretaker failed");    
       exit(EXIT_FAILURE);
  }
  
  /* Truncate Shared Memory Object to specific size */
  if((ftruncate(fd, SHMSIZE) &lt; 0)) {
      perror("\nftruncate() in Caretaker failed");
      exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>While Debugging i watched that the return value of shm_open() is 0 every time, but i can see this object in /dev/shm. And while executing ftruncate() it returns every time with error "invalid argument".</p>
  
  <p>Why fd is 0 every time and why ftruncate doesn't work?
  What should i do?</p>