title: Why do I get segmentation fault sometimes, and sometimes not, using semaphores?
body: <p>Like title says, when I start ubuntu terminal first time after I start my computer it works fine but after I compile my program I'm getting segmentation fault error. Not sure if it's worth to mention but I use ubuntu on windows 10.<br>
  My semaphore.c:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;fcntl.h&gt; /* for O_* constants */
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdlib.h&gt; /* random */
  #include &lt;unistd.h&gt; /* sleep */
  #define SHMSIZE 128
  #define SHM_R 0400
  #define SHM_W 0200
  
  
  float getRandom(float min, float max) {
      // this function returns a random number between min - max
      float scale = rand() / (float) RAND_MAX;
      return min + scale * (max - min);
  }
  
  
  int main(int argc, char **argv)
  {
      const char *semName1 = &quot;my_sema1&quot;;
      const char *semName2 = &quot;my_sema2&quot;;
  
      struct shm_struct {
          int buffer[10];
          // unsigned empty;
          int next_in, next_out;
      };
      volatile struct shm_struct *shmp = NULL;
      char *addr = NULL;
      pid_t pid = -1;
      int var1 = 0, var2 = 0, shmid = -1;
      struct shmid_ds *shm_buf;
  
      /* init semaphores */
      sem_t *sem_id1 = sem_open(semName1, O_CREAT, O_RDWR, 10);
      sem_t *sem_id2 = sem_open(semName2, O_CREAT, O_RDWR, 0);
      int i, status;
  
      /* allocate a chunk of shared memory */
      shmid = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | SHM_R | SHM_W);
      shmp = (struct shm_struct *) shmat(shmid, addr, 0);
      // shmp-&gt;empty = 0;
      pid = fork();
  
      /* index in buffer */
      shmp-&gt;next_in = 0;
      shmp-&gt;next_out = 0;
  
      if (pid != 0) {
          /* here's the parent, acting as producer */
          while (var1 &lt; 100) {
              float random_time = getRandom(0.1, 2.0); /* random between [0.1s - 2.0s] */
              usleep(random_time * 1000000); // sleep
  
              sem_wait(sem_id1); // locks the sem, decreases its value    
              var1++;
              printf(&quot;Sending %d\n&quot;, var1); fflush(stdout);
              shmp-&gt;buffer[shmp-&gt;next_in] = var1;
  
              shmp-&gt;next_in = shmp-&gt;next_in + 1 % 10;
              sem_post(sem_id2);
          }
          sem_close(sem_id1);
          sem_close(sem_id2);
          wait(&amp;status);
          sem_unlink(semName1);
          sem_unlink(semName2);
          shmdt(addr);
          shmctl(shmid, IPC_RMID, shm_buf);
      } else {
          /* here's the child, acting as consumer */
          while (var2 &lt; 100) {
              float random_time2 = getRandom(0.1, 2.0); /* random between [0.1s - 2.0s] */
              usleep(random_time2 * 1000000); // sleep
              sem_wait(sem_id2);
              var2 = shmp-&gt;buffer[shmp-&gt;next_out];
              printf(&quot;Received %d\n&quot;, var2); fflush(stdout);
              shmp-&gt;buffer[shmp-&gt;next_out] = -1; // remove
              shmp-&gt;next_out = shmp-&gt;next_out + 1 % 10;
              sem_post(sem_id1);
          }
          sem_close(sem_id1);
          sem_close(sem_id2);
          shmdt(addr);
          shmctl(shmid, IPC_RMID, shm_buf);
      }
  }
  </code></pre>
  <p>Other than that, I'm trying to implement a bounded buffer with two semaphores where the producer puts items in buffer and customer reads them, does this code make sense? I don't see why I'm getting segmentation error.</p>