title: how to synchronize process using mutex with multithreading in c
body: <p>HI I am writing a program which write the string in file, process 1 will write a small letters in file and process 2 write a capital letters in same file. i implemented a program using threading, process 1 must run first, after that run the process 2 . program as follows.  </p>
  
  <pre><code>/******************
   *  Header Files
   ******************/
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  pthread_mutex_t lock;
  
  void* write_p1()
  {
      if((pthread_mutex_lock(&amp;lock)) != 0)
      {
          printf("pthread_mutex_lock() failed\n");
          exit(1);
      }
  
      int index=0;    /*used in for loop*/
      int flag=0; /*flag used in capital letter and space detecion*/
      FILE *fp=NULL;  /*file pointer used to store open file*/
      char str[100];  /*to store user string*/
  
      printf("*****Hi, I am Process 1*****\n");
      /*open the sample.txt file for write*/
      fp=fopen("sample.txt","w");
      if(fp==NULL)
      {
          printf("Not able to open file\n");
          exit(1);
      }
      printf("Enter small letters\n");
      fgets(str,100,stdin);
  
      /*capital letter and space detection*/
      if((strlen(str)&gt;0)&amp;&amp;(str[strlen(str)-1]=='\n'))
      {
          str[strlen(str)-1]='\0';
      }
      for(index=0;str[index]!='\0';index++)
      {
          if(islower(str[index]) || str[index] == ' ')
          {
              flag=0;
          }
          else
          {
              printf("Enter Small Letters\n");
              exit(1);
          }
      }
      if(flag==0)
      {
          fprintf(fp,"%s",str);
      }
      /*close the file*/
      fclose(fp);
      printf("Entered string: %s\n",str);
  
      if((pthread_mutex_unlock(&amp;lock)) != 0)
      {
          printf("pthread_mutex_unlock() failed\n");
          exit(1);
      }
      printf("\n\n");
  }
  
  void* write_p2()
  {
          if((pthread_mutex_lock(&amp;lock)) != 0)
          {
                  printf("pthread_mutex_lock() failed\n");
                  exit(1);
          }
  
          int index=0;    /*used in for loop*/
          int flag=0;     /*flag used in small letter and space detecion*/
          FILE *fp=NULL;  /*file pointer used to store open file*/
          char str[100];  /*to store user string*/
  
      printf("*****Hi, I am Process 2*****\n");
          /*open the sample.txt file for write*/
          fp=fopen("sample.txt","a");
      if(fp==NULL)
          {
                  printf("Not able to open file\n");
                  exit(1);
          }
          printf("Enter Capital letters\n");
          fgets(str,100,stdin);
  
      /*capital letter and space detection*/
          if((strlen(str)&gt;0)&amp;&amp;(str[strlen(str)-1]=='\n'))
          {
                  str[strlen(str)-1]='\0';
          }
          for(index=0;str[index]!='\0';index++)
          {
                  if(isupper(str[index]) || str[index] == ' ')
                  {
                          flag=0;
                  }
                  else
                  {
                          printf("Enter capital Letters\n");
                          exit(1);
                  }
          }
          if(flag==0)
      {
                  fprintf(fp,"%s",str);
          }
          /*close the file*/
          fclose(fp);
          printf("Entered string: %s\n",str);
          if((pthread_mutex_unlock(&amp;lock)) != 0)
          {
                  printf("pthread_mutex_unlock() failed\n");
                  exit(1);
          }
          printf("\n\n");
  }
  
  int main(void)
  {
      /*initialized semaphore*/
      if((pthread_mutex_init(&amp;lock,NULL)) != 0)
      {
          printf("pthread_mutex_init() failed\n");
          exit(1);
      }
      /*create a two thread*/
      pthread_t t1=0,t2=0;
      pthread_create(&amp;t1,NULL,write_p1,NULL);
      pthread_create(&amp;t2,NULL,write_p2,NULL);
      /*this function wait for thread to terminate*/
      pthread_join(t1,NULL);
      pthread_join(t2,NULL);
      /*destroy the semaphore*/
      if((pthread_mutex_destroy(&amp;lock)) != 0)
      {
          printf("pthread_mutex_destroy() failed\n");
          exit(1);
      }
      return 0;
  }
  </code></pre>
  
  <p>Now This program is working but sometime it will run first thread 2(process 2) or some time thread 1(process 1) , i need a solution for that, it must run thread 1(process 1) first after that thread 2(process 2), so what changes should i do?</p>