title: How to detect starvation in reader-writer problem
body: <p>I have a question about this piece of code I have. It is the classic readers-writers problem. I followed the pseudo-code found on this wikipedia page for the first problem that has writers starving. I would like to know how I would actually notice the starvation of the writers going on.</p>
  
  <p>I tried putting print statements of the shared_variable in various places, but this didn't give me much insight. But maybe I just didn't understand what was going on. Would someone be able to explain to me how I could visually see the starvation happening? Thank you!
  The number of attempts that the reader or writer would attempt to read or write is given as a command line argument.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;pthread.h&gt;
  
  
  // Compile it like so: gcc assignment2.c -lpthread
  
  
  // Shared variables (semaphore and integer)
  static sem_t rw_mutex;
  static sem_t mutex;
  static int read_count = 0;
  
  // Shared variable
  int shared_variable = 0;
  
  static void *writerAction(void *arg){
      int number_attempt = *((int *) arg);
      int attempt = 0;
      do{
          sem_wait(&amp;rw_mutex);
          shared_variable = shared_variable + 10;
          sem_post(&amp;rw_mutex);
          attempt++;
      }while(attempt &lt; number_attempt);
  }
  
  static void *readerAction(void *arg){
      int number_attempt = *((int *) arg);
      int attempt = 0;
     do{
          sem_wait(&amp;mutex);
          read_count++;
          // waiting to be able to read for the possible writer
          if (read_count == 1 ){
              sem_wait(&amp;rw_mutex); // get the lock so that writter can't write!
          }
  
          // Release the read_count variable
          sem_post(&amp;mutex);
          sem_wait(&amp;mutex);
          read_count--;
  
          if (read_count == 0){
              sem_post(&amp;rw_mutex); // release the lock so that writter can write
          }
          sem_post(&amp;mutex);
          attempt++;
      } while(attempt &lt; number_attempt);
  }
  
  
  
  
  int main(int argc, char *argv[]) {
  
      int number_writers = 10;
      int number_readers = 500;
      int reader_repeat_count = atoi(argv[2]);
      int writer_repeat_count = atoi(argv[1]);
  
      // Instantiating the threads for the writters and readers
      pthread_t writer_threads[number_writers];
      pthread_t reader_threads[number_readers];
  
      // Initation of semaphores
      sem_init(&amp;rw_mutex, 0, 1);
      sem_init(&amp;mutex, 0, 1);
  
      printf("Start creation of Readers\n");
      for(int i = 0; i &lt;number_readers; i++){
          pthread_create(&amp;reader_threads[i], NULL, readerAction, &amp;reader_repeat_count);
      }
      printf("Start creation of Writers\n");
      for(int i = 0; i &lt; number_writers; i++){
          pthread_create(&amp;writer_threads[i], NULL, writerAction, &amp;writer_repeat_count);
      }
  
      // All the actions is hapenning here
      printf("Wait for Readers\n");
      for(int i = 0; i &lt; number_readers; i++){
          printf("Waiting for : %d\n",i);
          pthread_join(reader_threads[i], NULL);
      }
  
      printf("Wait for Writers\n");
      // Collect all the writers
      for(int i = 0; i &lt; number_writers; i++){
          printf("Waiting for : %d\n",i);
          pthread_join(writer_threads[i], NULL);
      }
  
      // Results
      printf("The shared variable is : %d\n",shared_variable);
     }
  </code></pre>