title: C - Shared memory and semaphores
body: <p>I want to create a C program with shared memory and semaphores. There should work two child processes. Both childs got a different int number. Then there is a goal number which should be written in the shared memory. Now both childs should subtract their numbers from the goal number until the goal number is lower or equal 0. I dont want that there appear race conditions. Thats why I try to use semaphores. But it dont work for me. Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/sem.h&gt;
  
  #define SEG_SIZE sizeof(int)
  #define NUM_OF_CHILDS 2
  
  int main(int argc, char *argv[]){
  
      int i, shm_id, sem_id, *shar_mem;
      int pid[NUM_OF_CHILDS];
      long waittime = 100;
      unsigned short marker[1];
  
      /* Define the numbers and the goal number */
  
      int numbers[2] = {28, 23};
      int goal = (numbers[0] + numbers[1]) * 4;   
  
      /* Create semaphor */
  
      if((sem_id = semget(IPC_PRIVATE, 1, IPC_CREAT|0644)) == -1){
  
          perror("semget()");
          exit(EXIT_FAILURE);
  
      }
  
      marker[0] = 1;
  
      /* All sem's to 1 */
  
      semctl(sem_id, 1, SETALL, marker);
  
      /* Create shared memory */
  
      if((shm_id = shmget(IPC_PRIVATE, SEG_SIZE, IPC_CREAT|0600)) == -1){
  
          perror("shmget()");
              exit(EXIT_FAILURE); 
  
      }
      if((shar_mem = (int *)shmat(shm_id, 0, 0)) == (int *) -1){
  
          perror("shmat()");
          exit(EXIT_FAILURE); 
  
      }
      *shar_mem = goal;
  
      /* Create child processes */
  
      for(i = 0; i &lt; NUM_OF_CHILDS; i++){
  
          pid[i] = fork();
          if(pid[i] &lt; 0){
  
              printf("Error!\n");
              exit(1);
  
          }
          if(pid[i] == 0){
              int count = 0;  
              /* Child processes */
  
              /* Structs for semaphor */
  
              struct sembuf enter, leave;
  
              enter.sem_num = leave.sem_num = 0;      
              enter.sem_flg = leave.sem_flg = SEM_UNDO;
              enter.sem_op = -1;              /* DOWN-Operation */
              leave.sem_op = 1;               /* UP-Operation */
  
              /* Join critical area */
  
              semop(sem_id, &amp;enter, 1);
  
              while(*shar_mem &gt; 0){
  
                  usleep(waittime);
                  *shar_mem -= numbers[i];
  
                  count++;
              }
  
              printf("%i\n", count);
  
              /* Leave critical area */
  
              semop(sem_id, &amp;leave, 1);
  
              exit(0);
  
          }
  
      }
  
      /* Wait for childs. */
  
      for(i = 0; i &lt; NUM_OF_CHILDS; i++){
  
          waitpid(pid[i], NULL, 0);
  
      }
  
      /* Is goal equal 0 or lower? */
  
      int returnv;
  
      if(*shar_mem == 0){
  
          /* No race conditions */
  
          returnv = 0;
  
      }
      else {
  
          /* Race conditions */
  
          returnv = 1;
  
      }
  
      /* Close shared memory and semaphores */
  
      shmdt(shar_mem);
      shmctl(shm_id, IPC_RMID, 0);
      semctl(sem_id, 0, IPC_RMID);
  
      return returnv;
  
  }
  </code></pre>
  
  <p>When the shared memory value is 0 at the end, there should be no race conditions. If it is lower than 0, there were race conditions. And I always get lower than 0. On top of that I count how much times each child subtract his number. The result: First Child 8 times and the second one 0 times. Can anyone help me with it?</p>