title: Max Value search in array using fork and pipe
body: <p>I have to write a c program (linux) to search max value in an array, useing 10 child in the same time. The array size is 1000. Each child searching the max from 100 numbers. The parent should get the results on pipelines. My code doesnt work perfectly. The main problem is the pipelines. The parent get only the first max. The second problem is that the childs arent running in the same times (not a huge problem but probably someone can told me what is wrong)
  I make some notes for my code, but my english is so bad sry.
  I hope i copy the source code in right form. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  
  
  int main()
  {
  int array[1000];       //main array
  int i;
  srand (time(NULL));     //for not the same numbers
  for(i=0;i&lt;1000;i++){    //array upload
  array[i]= ( rand() % (10000+1) );    //from 1 to 10000 random numbers
  }
  int searchminindex;   //search index, 0-100,100-200, etc
  int searchmaxindex;
  int threads = 10;    //number of threads
  int fd[2];
  pipe(fd);
  int arraymax[10];    //the 10 max numbers
  for(i=0;i&lt;threads;i++){             //for 10 threads
  if(fork() == 0){
      close(fd[0]);               //close reading
      searchminindex=i*100;           //number of thread * arraysize/threadsnumber
      searchmaxindex=(i+1)*100;
      int maxindex=searchminindex;            //it will store the max number index, start from the search min index
      int j;
      for(j=searchminindex;j&lt;searchmaxindex;j++){ //max search
          if(array[maxindex]&lt;array[j]){
              maxindex=j;
          }
      }
      write(fd[1], &amp;array[maxindex], sizeof(array[maxindex])); //write the max number into the pipe
      printf("max: %d\n", array[maxindex]);   //that for check the result, will be deleted from the final code
      close(fd[1]);               //close write
      wait(NULL);
      exit(0);
  }
  else{
  wait(NULL);
  close(fd[1]);               //close write
  read(fd[0], &amp;arraymax[i], sizeof(arraymax[i])); //read the max and store in arraymax[]
  close(fd[0]);                   //close read
  printf("the read max from the pipe: %d\n", arraymax[i]);  //that for check the result, will be deleted from the final code
  }
  }
  
  int arraymaxi=0;        //it is search the max in the main array for check the result, will be deleted
  int k;
  for(k=0;k&lt;1000;k++){
  if(array[arraymaxi]&lt;array[k]){
      arraymaxi=k;
  }
  }
  printf("maxi: %d\n", array[arraymaxi]);    //end of checking the correct result, will be deleted
  int l;                  //check the max numbers from the forks, will be deleted
  for(l=0;l&lt;10;l++){
  printf("from the pipe max: %d\n", arraymax[l]);
  }
  int h;                  //search the true max from the 10 numbers
  int truemaxindex=0;
  for(h=0;h&lt;10;h++){
  if(arraymax[truemaxindex]&lt;arraymax[h]){
      truemaxindex=h;
  }
  }
  printf("the final max: %d\n", arraymax[truemaxindex]);
  return 0;
  </code></pre>