title: Segfaults in c using pthread and multithreaded programming
body: <p>Hi this is the multithread program I'm trying to write but it gives me segfault<br>
  the original code works fine without multi-thread but this gives me segfaults<br>
  I used mutex in every global var reading but even one thread gives me segfault<br>
  this is my multithreaded code that segfaults<br></p>
  <pre><code># include &lt;stdio.h&gt; 
  # include &lt;string.h&gt; 
  # include &lt;pthread.h&gt;
  # include &lt;stdlib.h&gt;
  # include &lt;math.h&gt;
  # include &lt;limits.h&gt;
  
  # define NUMBER_OF_COLUMNS 20
  # define NUMBER_OF_CLASSES 4
  # define NUMBER_OF_THREADS 4
  # define NUMBER_OF_COLUMNS_PLUS_ONE 21 //STUPID C WONT LET ME MAKE GLOBAL VECTORS
  double max[NUMBER_OF_COLUMNS], min[NUMBER_OF_COLUMNS];
  double maxMinusMin[NUMBER_OF_COLUMNS];
  int number_of_datas = 0, number_of_wrong_predictions = 0;
  pthread_t threads[NUMBER_OF_THREADS];
  pthread_mutex_t mutex_minmax;
  
  
  void * solve(void *id){
      FILE *traincsv, *weightscsv;
      long tid;
      tid = (long) id;
      char buffer[1024];
      char* token;
      int EOFchecker = 1;
      double temp;
      double sum[NUMBER_OF_CLASSES];
      double vectors[NUMBER_OF_CLASSES][NUMBER_OF_COLUMNS_PLUS_ONE];
      int winner = 0;
      int number_of_datas_local = 0, number_of_wrong_predictions_local = 0;
      char filename[16];
      sprintf(filename, &quot;trainq%ld.csv&quot;, (tid + 1));
      printf(&quot;Hello World! Thread ID, %ld\n&quot;, tid);
      printf(&quot;%s\n&quot;, filename);
      traincsv = fopen(filename,&quot;r&quot;);
      weightscsv = fopen(&quot;weights.csv&quot;,&quot;r&quot;);
  
  
      //load vectors to array last one is bias
      pthread_mutex_lock(&amp;mutex_minmax);
      fscanf(weightscsv,&quot;%s&quot;,buffer);//first junk line
      for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
      {
          fscanf(weightscsv,&quot;%s&quot;,buffer);
          token = strtok(buffer,&quot;,&quot;);
          for (size_t j = 0; j &lt; NUMBER_OF_COLUMNS + 1; j++)
          {
              temp = atof(token);
              vectors[i][j] = temp;
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
      }
      pthread_mutex_unlock(&amp;mutex_minmax);
      pthread_mutex_lock(&amp;mutex_minmax);
      fscanf(traincsv,&quot;%s&quot;,buffer);//first line is just names
      fscanf(traincsv,&quot;%s&quot;,buffer);
      pthread_mutex_unlock(&amp;mutex_minmax);
      while (EOFchecker != EOF)
      {
          token = strtok(buffer,&quot;,&quot;);
          //reset sum of wieghts to bias;
          printf(&quot;thread id %ld token %s\n&quot;,tid,token);
          for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
          {
              sum[i] = vectors[i][NUMBER_OF_COLUMNS];
          }
          for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
          {
              temp = atof(token);
  
              //Normalize
              pthread_mutex_lock(&amp;mutex_minmax);
              temp = (temp-min[i])/(maxMinusMin[i]);
              pthread_mutex_unlock(&amp;mutex_minmax);
  
              for (size_t j = 0; j &lt; NUMBER_OF_CLASSES; j++)
              {
                  sum[j] += vectors[j][i] * temp;
              }
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
          //check which vector has most points
          for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
          {
              if (sum[i] &gt; sum[winner])
              {
                  winner = i;
              }
              
          }
          if(winner != atof(token)){
              number_of_wrong_predictions_local ++;
          }
          number_of_datas_local ++;
          pthread_mutex_lock(&amp;mutex_minmax);
          EOFchecker = fscanf(traincsv,&quot;%s&quot;,buffer);
          pthread_mutex_unlock(&amp;mutex_minmax);
      }
      pthread_mutex_lock(&amp;mutex_minmax);
      number_of_datas+=number_of_datas_local;
      number_of_wrong_predictions+=number_of_wrong_predictions_local;
      pthread_mutex_unlock(&amp;mutex_minmax);
      pthread_exit(NULL);
  }
  int main(){
      void * status;
      FILE *traincsv, *traincsvnorm, *weightscsv;
      char buffer[1024];
      char* token;
      double temp;
      int EOFchecker = 1;
      int return_code;
      
      
      if (pthread_mutex_init(&amp;mutex_minmax, NULL) != 0) { 
          printf(&quot;\n mutex init has failed\n&quot;); 
          return 1; 
      } 
      
      for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
      {
          min[i] = INFINITY;
      }
      
      
      traincsv = fopen(&quot;train.csv&quot;,&quot;r&quot;);
      traincsvnorm = fopen(&quot;trainnorm.csv&quot;,&quot;w+&quot;);
  
      // find min and max
      fscanf(traincsv,&quot;%s&quot;,buffer);//first line is just names
      fscanf(traincsv,&quot;%s&quot;,buffer);
      while (EOFchecker != EOF)
      {
          token = strtok(buffer,&quot;,&quot;);
          for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
          {
              temp = atof(token);
              if(max[i]&lt;temp) max[i] = temp;
              if(min[i]&gt;temp) min[i] = temp;
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
          EOFchecker = fscanf(traincsv,&quot;%s&quot;,buffer);
      }
      for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
      {
          maxMinusMin[i] = max[i] - min[i];
      }
      for(long tid = 0; tid &lt; NUMBER_OF_THREADS; tid++)
      {
          return_code = pthread_create(&amp;threads[tid],
                  NULL, solve, (void *) tid);
  
          if (return_code)
          {
              printf(&quot;ERROR; return code from pthread_create() is %d\n&quot;,
                      return_code);
              exit(-1);
          }
      }
  
      // pthread_mutex_destroy(&amp;mutex_minmax);
      printf(&quot;wrong predictions : %d \nall predictions : %d \n&quot;,number_of_wrong_predictions,number_of_datas);
      for(long i = 0; i &lt; NUMBER_OF_THREADS; i++)
          pthread_join(threads[i], &amp;status);
      fclose(traincsv);
      fclose(traincsvnorm);
      pthread_exit(NULL);
  }
  </code></pre>
  <p>this is my original code that works fine</p>
  <pre><code># include &lt;stdio.h&gt; 
  # include &lt;string.h&gt; 
  # include &lt;pthread.h&gt;
  # include &lt;stdlib.h&gt;
  # include &lt;math.h&gt;
  
  # define NUMBER_OF_COLUMNS 20
  # define NUMBER_OF_CLASSES 4
  // void * solve(void *id){
  //     int tid;
  //     tid = (int) id;
  
  // }
  int main(){
      FILE *traincsv, *traincsvnorm, *weightscsv;
      char buffer[1024];
      char* token;
      double temp, max[NUMBER_OF_COLUMNS], min[NUMBER_OF_COLUMNS];
      int EOFchecker = 1;
      int number_of_datas = 0, number_of_wrong_predictions = 0;
      double vectors[NUMBER_OF_CLASSES][NUMBER_OF_COLUMNS + 1];
      double sum[NUMBER_OF_CLASSES];
      int winner = 0;
  
      for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
      {
          min[i] = INFINITY;
      }
      
      
      traincsv = fopen(&quot;train.csv&quot;,&quot;r&quot;);
      traincsvnorm = fopen(&quot;trainnorm.csv&quot;,&quot;w+&quot;);
      weightscsv = fopen(&quot;weights.csv&quot;,&quot;r&quot;);
      // fprintf(traincsvnorm,&quot;%s\n&quot;,buffer);
  
      //load vectors to array last one is bias
      fscanf(weightscsv,&quot;%s&quot;,buffer);//first junk line
      for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
      {
          fscanf(weightscsv,&quot;%s&quot;,buffer);
          token = strtok(buffer,&quot;,&quot;);
          for (size_t j = 0; j &lt; NUMBER_OF_COLUMNS + 1; j++)
          {
              temp = atof(token);
              vectors[i][j] = temp;
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
      }
  
      //find min and max
      fscanf(traincsv,&quot;%s&quot;,buffer);//first line is just names
      fscanf(traincsv,&quot;%s&quot;,buffer);
      while (EOFchecker != EOF)
      {
          token = strtok(buffer,&quot;,&quot;);
          for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
          {
              temp = atof(token);
              if(max[i]&lt;temp) max[i] = temp;
              if(min[i]&gt;temp) min[i] = temp;
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
          EOFchecker = fscanf(traincsv,&quot;%s&quot;,buffer);
      }
      // Normilize
      double maxMinusMin[NUMBER_OF_COLUMNS];
      for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
      {
          maxMinusMin[i] = max[i] - min[i];
      }
      EOFchecker = 1;
      fseek(traincsv,0,SEEK_SET);
      fscanf(traincsv,&quot;%s&quot;,buffer);//first line is just names
      fscanf(traincsv,&quot;%s&quot;,buffer);
      while (EOFchecker != EOF)
      {
          token = strtok(buffer,&quot;,&quot;);
          //reset sum of wieghts to bias;
          for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
          {
              sum[i] = vectors[i][NUMBER_OF_COLUMNS];
          }
          for (size_t i = 0; i &lt; NUMBER_OF_COLUMNS; i++)
          {
              temp = atof(token);
              temp = (temp-min[i])/(maxMinusMin[i]);
              for (size_t j = 0; j &lt; NUMBER_OF_CLASSES; j++)
              {
                  sum[j] += vectors[j][i] * temp;
              }
              token = strtok(NULL,&quot;,&quot;);//next token of last string
          }
          //check which vector has most points
          for (size_t i = 0; i &lt; NUMBER_OF_CLASSES; i++)
          {
              if (sum[i] &gt; sum[winner])
              {
                  winner = i;
              }
              
          }
          if(winner != atof(token)){
              number_of_wrong_predictions ++;
          }
          number_of_datas ++;
          EOFchecker = fscanf(traincsv,&quot;%s&quot;,buffer);
      }
  
      printf(&quot;wrong predictions : %d \nall predictions : %d \n&quot;,number_of_wrong_predictions,number_of_datas);
      fclose(traincsv);
      fclose(traincsvnorm);
      fclose(weightscsv);
  }
  </code></pre>
  <p>these are inputs <a href="https://pastebin.com/0TLD1ke9" rel="nofollow noreferrer">train.csv</a> <a href="https://pastebin.com/AMq79DVf" rel="nofollow noreferrer">weights.csv</a><br>
  it somtimes gives me free(): invalid pointer error and also here is gdb bt</p>