title: Write in file using 20 threads
body: <p>The Idea here is to create a file to be written to. I'm trying to create ten threads and have them print to the file 10 times each. Using a semaphore to stop multiple threads from writing to the file at once. But I have errors. The code is below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  
  #define FNAME   "fisier.txt"
  #define MAX_STRING_LEN  80
  #define NUMBER_OF_THREADS 10
  
  FILE *fp;
  sem_t mutex;
  int counter;
  
  FILE *makeTextFile(char *fname, char mode){
     FILE *localFP;
     localFP = fopen(fname, &amp;mode);
   return (localFP);
  }
  
  void *print_message(void *tid){
      int i;
      for (i = 0; i &lt; 10; i++){
          sem_wait(&amp;mutex);
          fp = fopen(FNAME, "a");
          fprintf(fp, "Thread %d is running.\n", tid);
          fclose(fp);
      sem_post(&amp;mutex);
      printf ( "Thread %d has finished.\n", tid);
      }
  }
  
  int threads(){
      const char *fName = "fisier.txt";
      int status;
      pthread_t threads[NUMBER_OF_THREADS];
      fp = makeTextFile(FNAME, 'w');
      fprintf(fp, "Process ID: %ld\n", (long)getpid());
      fclose(fp);
  
      int i;
      for (i =0; i &lt; NUMBER_OF_THREADS; i++){
          status = pthread_create(&amp;threads[i], NULL, &amp;print_message, (void *)i);
          if (status != 0){
              printf("pthread_create returned error code %d\n", status);
              exit(-1);
          }
       }
      }
  
  int main() {
      threads();  
  
      return 0;
  }
  </code></pre>
  
  <p>Warnings:</p>
  
  <pre><code>probl2.c: In function ‘print_message’:
  probl2.c:27:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘void *’ [-Wformat=]
           fprintf(fp, "Thread %d is running.\n", tid);
                       ^
  probl2.c:30:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
       printf ( "Thread %d has finished.\n", tid);
                ^
  probl2.c: In function ‘threads’:
  probl2.c:44:68: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     status = pthread_create(&amp;threads[i], NULL, &amp;print_message, (void *)i);
  </code></pre>
  
  <p>Writing in a file just it: Process ID: 10568
  I want to write 
  How to solve it ? </p>