title: inter process communication using shared memory(mmap) and semaphores
body: <p>I am trying to improvise a program I had written for single producer multiple consumer multi threading using counting semaphores. I want to implement inter process communication using shared memory (mmap() system call). I want to use anonymous mapping with no backing file. </p>
  
  <p>This is the structure I want to share between the parent and its multiple child processes.</p>
  
  <pre><code>typedef struct Buffer{
      char **Tuples;
      sem_t buf_mutex,empty_count,fill_count;
  } Buffer;
  Buffer buffer[100];
  </code></pre>
  
  <p>The parent process is the <code>mapper()</code> function which does produces something and puts it in buffer[i], based on some inputs. The child processes go to <code>reducer()</code> function which consumes what is put in its buffer[j]. Each reducer or child process should have access to its buffer. The child processes are forked() in the main function and then the parent process control goes to <code>mapper()</code>. I have initialized the synchronization primitives to be process shared.</p>
  
  <p>Is my <code>main()</code> method the correct way of doing it ? I am also getting type casting errors for return value of <code>mmap()</code>, which is a pointer, but I am not sure how to handle it and then use it. I also think <code>malloc()</code> should not be used in line 47 for allocating space to tuples but instead <code>mmap()</code> itself should be used. Can anyone please help ? 
  This is my program - </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;sys/mman.h&gt;
  
  typedef struct Buffer{
      char **Tuples;
      // int count;
      sem_t buf_mutex,empty_count,fill_count;
  } Buffer;
  Buffer buffer[100];
  int numOfSlots;
  int numOfReducers;
  
  void mapper(){
      //Synchronization primitives (counting semaphores) used for synchronization
      //Produce something and put it in buffer[i]
  }
  
  void reducer(long b){
      //Synchronization primitives (counting semaphores) for synchronization
      //Consume from buffer[b]
  }
  
  
  int main (int argc, char *argv[]){
      if(argc != 3) {
          if(argc &lt; 3)
              printf("Insufficient arguments passed\n");
          else
              printf("Too many arguments passed\n");
      return 1;
      }
      int i;
      long r;
      numOfSlots = atoi(argv[1]);
      numOfReducers = atoi(argv[2]);
  
      for(i=0; i&lt;numOfReducers; i++){
          buffer[i] = (struct Buffer *) mmap(NULL, sizeof(buffer[i]), PROT_READ | PROT_WRITE,
                  MAP_SHARED | MAP_ANONYMOUS, -1, 0);
          if (buffer[i] == MAP_FAILED)
              errExit("mmap");    
  
          buffer[i].Tuples = malloc(numOfSlots * sizeof(char *));
          sem_init(&amp;buffer[i].buf_mutex, 1, 1);
          sem_init(&amp;buffer[i].fill_count, 1, 0);
          sem_init(&amp;buffer[i].empty_count, 1, numOfSlots);
      }
  
      for(r=0;r&lt;numOfReducers;r++){ // loop will run n times (n=5)
          if(fork() == 0){
              printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
              Reducer(r);
              exit(0);
          }
      }
      mapper();
      for(r=0;r&lt;numOfReducers;r++) // loop will run n times (n=5)
      wait(NULL);
  }
  </code></pre>
  
  <p>These are the links I am trying to follow - 
  <a href="https://computing.llnl.gov/tutorials/pthreads/man/pthread_mutexattr_init.txt" rel="nofollow noreferrer">https://computing.llnl.gov/tutorials/pthreads/man/pthread_mutexattr_init.txt</a>
   <a href="https://github.com/bradfa/tlpi-dist/blob/master/mmap/anon_mmap.c" rel="nofollow noreferrer">https://github.com/bradfa/tlpi-dist/blob/master/mmap/anon_mmap.c</a></p>
  
  <p>Thanks,
  Harrish</p>