title: Read Write Integer Array Into Shared Memory
body: <p>The following is the READER-WRITER code for my shared memory.</p>
  
  <p>Read Code-</p>
  
  <pre><code>int main(){
  int shmid;
  int *array;
  int count = 5;
  int i = 0;
  key_t key = 12345;
  
  shmid = shmget(key, count*sizeof(int), IPC_EXCL);
  
  array = shmat(shmid, 0, SHM_RDONLY);
  
  for(i=0; i&lt;5; i++)
      {
          printf("\n%d---\n", array[i] );
      }
  
      printf("\nRead to memory succesful--\n");
  
      shmdt((void *) array);
      return 0;
  }
  </code></pre>
  
  <p>Write Code-</p>
  
  <pre><code>int main()
  {
  int shmid;
  int *array;
  int count = 5;
  int i = 0;
  int SizeMem;
  key_t key = 12345;
  
  SizeMem = sizeof(*array)*count;
  
  shmid = shmget(key, count*sizeof(int), IPC_CREAT);
  
  array = (int *)shmat(shmid, 0, 0);
  
  array = malloc(sizeof(int)*count);
  
  for(i=0; i&lt;5; i++)
  {
      array[i] = i;
  }
  
  for(i=0; i&lt;count; i++)
  {
      printf("\n%d---\n", array[i]);
  }
  
  printf("\nWritting to memory succesful--\n");
  
  shmdt((void *) array);
  
  return 0;
  }
  </code></pre>
  
  <p>After writing to the memory when I try to read, the output are garbage values. Can someone please explain what I have done wrong(The output shows all zeros) Thankyou</p>