title: Reader Writer Problem With Writer Priority Problem
body: <p>I came across this problem as I am learning more about operating systems. In my code, I've tried making the reader having priority and it worked, so next I modified it a bit to make the writer have the priority. When I ran the code, the output was exactly the same and it seemed like the writer did not have the priority. Here is the code with comments. I am not sure what I've done wrong, since I modified a lot of the code but the output remains the same if I did not change it at all.</p>
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;stdio.h&gt;
  
  /*
  This program provides a possible solution for first readers writers problem using mutex and semaphore.
  I have used 10 readers and 5 producers to demonstrate the solution. You can always play with these values.
  */
  
  // Semaphore initialization for writer and reader 
  sem_t wrt;
  sem_t rd;
  
  // Mutex 1 blocks other readers, mutex 2 blocks other writers
  pthread_mutex_t mutex1;
  pthread_mutex_t mutex2;
  
  // Value the writer is changing, we are simply multiplying this value by 2
  int cnt = 2;
  
  int numreader = 0;
  int numwriter = 0;
  
  void *writer(void *wno)
  {   
      pthread_mutex_lock(&amp;mutex2);
      numwriter++;
      if(numwriter == 1){
          sem_wait(&amp;rd);      
      }
      pthread_mutex_unlock(&amp;mutex2);
      sem_wait(&amp;wrt); 
  
      // Writing Section
      cnt = cnt*2;
      printf(&quot;Writer %d modified cnt to %d\n&quot;,(*((int *)wno)),cnt);
  
      sem_post(&amp;wrt);
      pthread_mutex_lock(&amp;mutex2);
      numwriter--;
      if(numwriter == 0){
          sem_post(&amp;rd);
      }
      pthread_mutex_unlock(&amp;mutex2); 
  
  }
  void *reader(void *rno)
  {   
      sem_wait(&amp;rd);
      pthread_mutex_lock(&amp;mutex1);
      numreader++;
      if(numreader == 1){
          sem_wait(&amp;wrt);
      }
      pthread_mutex_unlock(&amp;mutex1);
      sem_post(&amp;rd); 
  
      // Reading Section
      printf(&quot;Reader %d: read cnt as %d\n&quot;,*((int *)rno),cnt);
  
      
      pthread_mutex_lock(&amp;mutex1);
      numreader--;
      if(numreader == 0){
          sem_post(&amp;wrt);
      }
      pthread_mutex_unlock(&amp;mutex1); 
  }
  
  int main()
  {   
  
      pthread_t read[10],write[5];
      pthread_mutex_init(&amp;mutex1, NULL);
      pthread_mutex_init(&amp;mutex2, NULL);
      sem_init(&amp;wrt,0,1);
      sem_init(&amp;rd,0,1);
  
      int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the writer and reader
  
      for(int i = 0; i &lt; 5; i++) {
          pthread_create(&amp;write[i], NULL, (void *)writer, (void *)&amp;a[i]);
      }
  
      for(int i = 0; i &lt; 10; i++) {
          pthread_create(&amp;read[i], NULL, (void *)reader, (void *)&amp;a[i]);
      }
  
      for(int i = 0; i &lt; 5; i++) {
          pthread_join(write[i], NULL);
      }
  
      for(int i = 0; i &lt; 10; i++) {
          pthread_join(read[i], NULL);
      }
  
      pthread_mutex_destroy(&amp;mutex1);
      pthread_mutex_destroy(&amp;mutex2);
      sem_destroy(&amp;wrt);
      sem_destroy(&amp;rd);
  
      return 0;
      
  }
  </code></pre>
  <p>Output (for both is the same. I think if writer had priority it will change first, then will be read):
  <a href="https://i.stack.imgur.com/v2mLu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v2mLu.png" alt="enter image description here" /></a></p>