title: Mutex and conditionals issue, problem with synchronization between threads
body: <pre class="lang-c prettyprint-override"><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;string.h&gt;
  
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  
  char mutual[100];
  int firstWrote = 0;
  int secondWrote = 0;
  int firstFinished = 0;
  int secondFinished = 0;
  
  void* reader(void* arg)
  {
      FILE* file = fopen((const char*)arg,&quot;r&quot;);
  
      if(file == NULL)
      {
          printf(&quot;Problem with opening file %s&quot;,(const char*)arg);
          exit(4);
      }
      else
      {
          char which[25];
          while(fgets(mutual,100,file))
          {
              pthread_mutex_lock(&amp;mutex);
              strcpy(which,(const char*)arg);
              if(strcmp(which,&quot;/etc/profile&quot;)==0)
              {
                  firstWrote = 1;
                  //printf(&quot;%s\n&quot;,mutual);
                  fflush(stdout);
              }
              if(strcmp(which,&quot;/etc/passwd&quot;)==0)
              {
                  secondWrote = 1;
                  //printf(&quot;%s\n&quot;,mutual);
                  fflush(stdout);
              }
  
              pthread_cond_signal(&amp;cond);
              pthread_mutex_unlock(&amp;mutex);
          }
          if(strcmp(which,&quot;/etc/profile&quot;)==0)
          {
              firstWrote = 0;
              firstFinished= 1;
          }
          else if(strcmp(which,&quot;etc/passwd&quot;)==0)
          {
              secondWrote = 0;
              secondFinished = 1;
          }
      }
      fclose(file);
      pthread_exit(NULL);
  }
  
  int main(void)
  {
      FILE* pawel1 = fopen(&quot;PAWEL1&quot;,&quot;w&quot;);
      FILE* pawel2 = fopen(&quot;PAWEL2&quot;,&quot;w&quot;);
  
  
      if(pawel1 == NULL || pawel2 == NULL)
      {
          printf(&quot;Problem with opening file&quot;);
          return 1;
      }
  
      pthread_t p1,p2;
  
      pthread_attr_t attr;
  
      if(pthread_attr_init(&amp;attr) != 0)
          exit(2);
  
      if(pthread_attr_setd2etachstate(&amp;attr,PTHREAD_CREATE_DETACHED))
          exit(3);
  
      pthread_create(&amp;p1,&amp;attr,reader,(void*)&quot;/etc/profile&quot;);
      pthread_create(&amp;p2,&amp;attr,reader,(void*)&quot;/etc/passwd&quot;);
  
      while(1)
      {
          pthread_mutex_lock(&amp;mutex);
          if(firstFinished == 1 &amp;&amp; secondFinished == 1)
              break;
  
  
          pthread_cond_wait(&amp;cond,&amp;mutex);
          if(firstWrote == 1)
          {
              fprintf(pawel1,&quot;%s&quot;,mutual);
              firstWrote = 0;
          }
          if(secondWrote == 1)
          {
              fprintf(pawel2,&quot;%s&quot;,mutual);
              secondWrote = 0;
          }
          pthread_mutex_unlock(&amp;mutex);
      }
  
      fclose(pawel1);
      fclose(pawel2);
      pthread_attr_destroy(&amp;attr);
      pthread_cond_destroy(&amp;cond);
      pthread_mutex_destroy(&amp;mutex);
  
      return 0;
  }
  </code></pre>
  <p>The app task is to made two different thread read from two files (/etc/passwd and /etc/profile) to a global buffer, and print results to proper file (PAWEL1 or PAWEL2) in main function , and it seems that i handle this behaviour with global flags but what i get is all lines from passwd and profile in one file - PAWEL2, and the file PAWEL1 is left empty.</p>