title: parent-child process communication through pipes
body: <p>I am tryimg to write a C program that will search an array of integers for another given integer. However, to speedup the search, the search is done in parallel by two child processes. The parent process reads in the number of integers, and then the integers in an array. It also reads in the integer to be searched. It then creates two child processes. The first child process searches the first half of the array, and the second child process searches the second half. If the integer is found, its index in the array is sent to the parent through a pipe. If it is not found, a -1 is sent to the parent through a pipe. The parent waits for both child processes to finish and then prints an appropriate message.</p>
  
  <p>I have consulted some books and this is what i came up with. There is a small problem though...the two child processes are running one after the other rather than parallel. what changes should i make?</p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sys/ipc.h&gt;
  #include &lt;string.h&gt;
  void main ()
  {
  int i,status,num1,num2,num3,num4, fd1[2], fd2[2];
  int a[1000];
  char b[5],c[5],d[5],e[5];
  pid_t pid1,pid2;
  
  printf("\n\n\nEnter how many numbers - ");
  scanf("%d",&amp;num1);
  //printf("\n\nEnter the %d numbers below -\n",num1);
  for (i=0;i&lt;num1;i++)
  {
      printf("%d : ",i);
      scanf("%d",&amp;a[i]);
  }
  printf("\n\nEnter the number to search - ");
  scanf("%d",&amp;num2);
  pipe(fd1);
  pipe(fd2);
  pid1=fork();
  if (pid1==0)
  {
  
      printf("this is the child 1\n");        
      for (i=0;i&lt;(num1/2);i++)
      {
          if (a[i]==num2) 
          {
              printf("found by process 1\n");
              sprintf(b,"%d",i);
              sprintf(c,"%d",-1);
              write(fd1[1],&amp;b,4); 
              write(fd2[1],&amp;c,4);
              //kill(0,1);
              break;
          }
          printf("%d\n",a[i]);
  
      }
      _exit ( EXIT_FAILURE ) ;
  }
  else 
  if (pid1&gt;0)
  {
      pid2=fork();
      if (pid2==0)
      {
          printf("this is the child 2\n");        
          for (i=(num1/2);i&lt;num1;i++)
          {
              if (a[i]==num2) 
              {
                  printf("found by process 2\n");
                  sprintf(b,"%d",-1);
                  sprintf(c,"%d",i);
                  write(fd1[1],&amp;b,4);
                  write(fd2[1],&amp;c,4);                             
                  //kill(0,1);
                  break;
              }
  
              printf("%d\n",a[i]);
  
          }
          _exit(EXIT_FAILURE);
      }
  
  }
  
  if (waitpid (pid1, &amp;status, 0)&gt;0 &amp;&amp; waitpid (pid2, &amp;status, 0)&gt;0)
  {   
  
      read(fd1[0],d,4);
      read(fd2[0],e,4);
      num3=atoi(d);       
      num4=atoi(e);
      if (num3&gt;0) printf("value of i is %d\n",num3);  
      if (num4&gt;0) printf("value of i is %d\n",num4);
  }
  
  }
  </code></pre>