title: Readers - writers in C - where am I wrong?
body: <p>I would like to ask for help with this task. </p>
  
  <p>My task is write an easy program in C which simulates readers-writers problem. The program requirements are:</p>
  
  <ol>
  <li><p>After program start, the user will be asked for enter count of writers and readers.</p></li>
  <li><p>Program will continously inform the user about status of the threads.</p></li>
  <li><p>After end of the program, little statistic (how many times had every reader read and every writer write).</p></li>
  </ol>
  
  <p>I have done some basic structure of the program (dealing with the critical section through semaphores etc.), but I think that the program does not do, what it should do. Program runs without fault or unexpected behavior, but the counts, how many times had every reader read and every writer write are always one for each reader or writer).</p>
  
  <p>Where am i wrong? Maybe I don't understand the task well.</p>
  
  <p>Thank You very much for response.</p>
  
  <p>Program code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  
  sem_t w;    // write access
  sem_t m;    // mutex
  int rc=0;   // readers count
  
  int writersCount;
  int readersCount;
  pthread_t writersThread[10], readersThread[10];
  int writeCount[10], readCount[10];
  int i;
  
  void *writer(void *i) {
      int a = *((int *) i);
  
      sem_wait(&amp;w);   // P(w)
      printf("Writer %d writes to DB.\n",a+1);
      writeCount[a+1]++;  
      sem_post(&amp;w);   // V(w)
  
      free(i);
  }
  
  void *reader(void *i) {
      int a = *((int *) i);
  
      sem_wait(&amp;m);   // P(m)
      rc++;
      if (rc == 1) {
          sem_wait(&amp;w);   // P(w)
      }
      sem_post(&amp;m);   // V (m)
  
      printf("Reader %d reads from DB.\n",a+1);
      readCount[a+1]++;   
  
      sem_wait(&amp;m);   // P(m)
      rc--;
      if (rc == 0) {
          sem_post(&amp;w);   // V(w)
      }
      sem_post(&amp;m);   // V(m)
  
      free(i);
  }
  
  int main() {
      sem_init(&amp;w,0,1);
      sem_init(&amp;m,0,1);
  
      printf("Enter count of writers:");
      scanf("%d",&amp;writersCount);
      printf("Enter count of readers:");
      scanf("%d",&amp;readersCount);
  
      for (i=0; i&lt;readersCount; i++) {
          int *arg = malloc(sizeof(*arg));
          *arg = i;
          pthread_create(&amp;readersThread[i], NULL, reader, arg);
      }
      for (i=0; i&lt;writersCount; i++) {
          int *arg = malloc(sizeof(*arg));
          *arg = i;
          pthread_create(&amp;writersThread[i], NULL, writer, arg);
      }
      for (i=0; i&lt;writersCount; i++) {
          pthread_join(writersThread[i], NULL);
      }   
      for (i=0; i&lt;readersCount; i++) {
          pthread_join(readersThread[i], NULL);
      }
  
      printf("--------------\n");
      for (i=0; i&lt;readersCount; i++) {
          printf("Reader %d read %d times\n",i+1,readCount[i+1]);
      }
  
      for (i=0; i&lt;writersCount; i++) {
          printf("Writer %d wrote %d times\n",i+1,writeCount[i+1]);
      }
  
      sem_destroy(&amp;w);
      sem_destroy(&amp;m);
      return 0;
  }
  </code></pre>
  
  <p>Output:
      Enter count of writers:4
      Enter count of readers:4</p>
  
  <pre><code>Reader 1 reads from DB.
  Reader 3 reads from DB.
  Reader 4 reads from DB.
  Reader 2 reads from DB.
  Writer 1 writes to DB.
  Writer 2 writes to DB.
  Writer 3 writes to DB.
  Writer 4 writes to DB.
  --------------
  Reader 1 read 1 times
  Reader 2 read 1 times
  Reader 3 read 1 times
  Reader 4 read 1 times
  Writer 1 wrote 1 times
  Writer 2 wrote 1 times
  Writer 3 wrote 1 times
  Writer 4 wrote 1 times
  </code></pre>