title: sem_init Segmentation fault error
body: <p>The task was to create simulation of warehouse's work. Producers (in an amount of argv[2]) create random number of "goods" (totally argv[1]), and consumers (in an amount of argv[3]) get random number of these goods. The full code is below.
  I got segmentation error and tried to debug it and got the following:</p>
  
  <p><code>Program received signal SIGSEGV, Segmentation fault.
  __new_sem_init (sem=0x37, pshared=0, value=1) at sem_init.c:44
  44  sem_init.c:  No such file or directory</code></p>
  
  <p>I think the problem is in address-of operator here 
  <code>sem_init(&amp;(shared-&gt;mutex), 0, 1);</code>
  How should I change the code to make it work?
  Thanks a lot! </p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;sys/mman.h&gt;
  #define NBUFF 10
  #define MAXNTHREADS 100
  #define min( a, b ) ( ( a &lt; b) ? a : b ) 
  int nitems, nproducers, nconsumers; 
  
  typedef struct shared_s
  { 
      int buff[NBUFF];
      int nput; 
      int nputval; 
      int nget;
      int ngetval; 
      sem_t mutex, nempty, nstored; 
  } sharedtype;
  
  
  
  
  int main(int argc, char **argv)
  {
      sharedtype * shared;
      key_t key;
      int shmid, semid;
      int i, j, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];
  
      shared = mmap(NULL, sizeof(sharedtype),
                  PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);
  
      /* Wrong argv */
      if (argc != 4)
      {
          printf("usage: newconsumer &lt;#items&gt; &lt;#producers&gt; &lt;#consumers&gt;\n");
          exit(1);
      }
      nitems = atoi(argv[1]);
      nproducers = min(atoi(argv[2]), MAXNTHREADS);
      nconsumers = min(atoi(argv[3]), MAXNTHREADS);
      pid_t chpidpr [nproducers];
      pid_t chpidcon [nconsumers];
      /* initilising semaphores */
      sem_init(&amp;(shared-&gt;mutex), 0, 1);
      sem_init(&amp;(shared-&gt;nempty), 0, NBUFF);
      sem_init(&amp;(shared-&gt;nstored), 0, 0);
  
  
      for (i = 0; i &lt; nproducers; i++) {
          prodcount[i] = 0;
          switch (chpidpr[i] = fork())
          {
              case -1: 
                  printf("fork error"); 
                  return 1;
              case 0: 
                  for (;;) 
                  {
                      sem_wait(&amp;shared-&gt;nempty); 
                      sem_wait(&amp;shared-&gt;mutex);
                      if (shared-&gt;nput &gt;= nitems) 
                      {
                          sem_post(&amp;(shared-&gt;nstored));
                          sem_post(&amp;(shared-&gt;nempty));
                          sem_post(&amp;(shared-&gt;mutex));
                          return 0; 
                      }
                      shared-&gt;buff[(shared-&gt;nput) %NBUFF] = shared-&gt;nputval;
                      (shared-&gt;nput)++;
                      (shared-&gt;nputval)++;
                      sem_post(&amp;(shared-&gt;mutex));
                      sem_post(&amp;(shared-&gt;nstored));
                      prodcount[i] += 1;
                  }
          }
      }
      for (i = 0; i &lt; nproducers; i++) 
      {
          printf("producer count[%d] = %d\n", i, prodcount[i]);
      }
      for (i = 0; i &lt; nconsumers; i++) {
          conscount[i] = 0;
          switch (chpidcon[i] = fork())
          {
              case -1: 
                  printf("error"); 
                  return 1;
              case 0: 
                  for (;;) 
                  {
                      sem_wait(&amp;(shared-&gt;nstored)); 
                      sem_wait(&amp;(shared-&gt;mutex));
                      if (shared-&gt;nget &gt;= nitems) 
                      {
                          sem_post(&amp;(shared-&gt;nstored));
                          sem_post(&amp;(shared-&gt;mutex));
                          return 0;
                      }
                      j = shared-&gt;nget % NBUFF;
                      if (shared-&gt;buff[j] != shared-&gt;ngetval)
                      {
                          printf("error: buff[%d] = %d\n", j, shared-&gt;buff[j]);
                      }
                      shared-&gt;nget++;
                      shared-&gt;ngetval++;
                      sem_post(&amp;(shared-&gt;mutex));
                      sem_post(&amp;(shared-&gt;nempty)); 
                      conscount[i] += 1;
                  }
          }
      }
  
  
      for (i = 0; i &lt; nconsumers; i++) 
      {
          printf("consumer count[%d] = %d\n", i, conscount[i]);
      }
      /* destroying semaphores */
      sem_destroy(&amp;(shared-&gt;mutex));
      sem_destroy(&amp;(shared-&gt;nempty));
      sem_destroy(&amp;(shared-&gt;nstored));
      exit(0);
  }
  </code></pre>