title: Releasing Shared Memory
body: <p>So the program below accesses a piece of shared memory at location "shmid" and attaches it to pointer "total." A child process is then created that accesses and modifies this piece of memory and then once complete, another child process is created and does the same, and then a 3rd child. Once these 3 children have finished modifying the value and finished executing, the parent process releases the shared memory and then the program exits. I was under the impression that this "shmctl" block would have the desired effect, but it doesn't seem to. </p>
  
  <pre><code>if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
  {
    perror ("shmctl");
   exit (-1);
  }
  </code></pre>
  
  <p>I don't believe it does, because following this block I have the following which during execution printed out the same value as before. Doesn't this indicate that the pointer is still accessing the same bit of memory, or am I mistaken?</p>
  
  <pre><code>printf("value after memory release:%d\n", total-&gt;value);
  </code></pre>
  
  <p>Thank you for your input!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  /* change the key number */
  #define SHMKEY ((key_t) 5600) //7890
  
  typedef struct
  {
    int value;
  } shared_mem;
  
  shared_mem *total;
  
  /*----------------------------------------------------------------------*
   * This function increases the value of shared variable "total"
   *  by one all the way to 100000
   *----------------------------------------------------------------------*/
  
  void process1 ()
  {
    int k = 0;
  
    while (k &lt; 100000)
    {
        k++;
        total-&gt;value = total-&gt;value + 1;
    }
    printf ("From process1 total = %d\n", total-&gt;value); //process 1 prints out total and  returns to main() function
  }
  
  /*----------------------------------------------------------------------*
   * This function increases the vlaue of shared memory variable "total"
   *  by one all the way to 170000
   *----------------------------------------------------------------------*/
  
  void process2 ()
  {
    int k = 0;
  
    while (k &lt; 170000)
    {
    k++;
    total-&gt;value = total-&gt;value + 1;
    }
    printf ("From process2 total = %d\n", total-&gt;value); //process 2 prints out total and returns to main() function
  }
  /*----------------------------------------------------------------------*
   * This function increases the vlaue of shared memory variable "total"
   *  by one all the way to 200000
   *----------------------------------------------------------------------*/
  
  void process3 ()
  {
    int k = 0;
  
    while (k &lt; 200000)
    {
      k++;
      total-&gt;value = total-&gt;value + 1;
    }
    printf ("From process3 total = %d\n", total-&gt;value); //process 3 prints out total and returns to main() function
  }
  
  /*----------------------------------------------------------------------*
   * MAIN()
   *----------------------------------------------------------------------*/
  
  int main()
  {
    int shmid;
    int pid1;
    int pid2;
    int pid3;
    int ID;
    int status;
  
    char *shmadd;
    shmadd = (char *) 0;
  
  /* Create and connect to a shared memory segmentt*/
  
  if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) &lt; 0)
  {
    perror ("shmget");
    exit (1);
  }
  
  
  if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1)
  {
    perror ("shmat");
    exit (0);
  }
  
  total-&gt;value = 0;
  
  if ((pid1 = fork()) == 0) //first child created
  {
    process1(); //first child process begins
  }
  
  while((ID = wait(&amp;status)) != -1) //parent loops until 1st child is finished
  printf("child %d is finished\n", ID); //parent prints out returned value after child is finished
  
  
  if ((pid1 != 0) &amp;&amp; ((pid2 = fork()) == 0)) //second child created
  {
    process2(); //second child process begins
  }   
  
  while((ID = wait(&amp;status)) != -1) //parent loops until 2nd child is finished
  printf("child %d is finished\n", ID); //parent prints out returned value after child is finished
  
  if ((pid1 != 0) &amp;&amp; (pid2 != 0) &amp;&amp; ((pid3 = fork()) == 0)) //third child created
  {
    process3(); //third child process begins
  }
  
  while((ID = wait(&amp;status)) != -1) //parent loops until 3rd child is finished
  printf("child %d is finished\n", ID); //parent prints out returned value after child is finished
  
  
  
  if ((pid1 != 0) &amp;&amp; (pid2 != 0) &amp;&amp; (pid3 != 0)) 
  {
    if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
    {
   perror ("shmctl");
   exit (-1);
    }
  
    printf ("\t\t  End of Program.\n"); //prints after all children have finished
  
    printf("value after memory release:%d\n", total-&gt;value);
  }
  return 0;
  } 
  
  /***** Note:  loop for parent to wait for child processes to finish and print ID of each child*****/
  </code></pre>