title: How does one keep an int and an array in shared memory in C?
body: <p>I'm attempting to write a program in which children processes communicate with each other on Linux.</p>
  
  <p>These processes are all created from the same program and as such they share code.</p>
  
  <p>I need them to have access to two integer variables as well as an integer array.</p>
  
  <p>I have no idea how shared memory works and every resource I've searched has done nothing but confuse me.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Edit: Here is an example of some code I've written so far just to share one int but it's probably wrong.</p>
  
  <pre><code>int segmentId;  
  int sharedInt;  
  const int shareSize = sizeof(int);  
  /* Allocate shared memory segment */  
  segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR);  
  
  /* attach the shared memory segment */    
  sharedInt = (int) shmat(segmentId, NULL, 0);  
  
  /* Rest of code will go here */  
  
  /* detach shared memory segment */  
  shmdt(sharedInt);  
  /* remove shared memory segment */  
  shmctl(segmentId, IPC_RMID, NULL);
  </code></pre>