title: Program to fork 4 child processes and each child will do different operation
body: <ol>
  <li>I am given the task of a parent process creating 4 child process and each child process will do addition, subtraction, multiplication and division respectively. </li>
  <li>I am not able to find the mistake in my code. </li>
  </ol>
  
  <p>Code: </p>
  
  <pre><code>int main ( int argc, char *argv[] )
  
  {
  int i, pid,pid1,pid2,pid3,pid4,num,count;
  int status;
  if(argc!=3)
      printf("incorrect number of arguments \n");
  
  int A,B,S,T,rem;
  num=0;
  count=0;
  S=0;
  T=atoi(argv[2]);
  A=atoi(argv[3]);
  B=atoi(argv[4]);
  printf("%d,\t %d,\t %d \n",T, A, B);
  int pid_t;
  //////////////////
  
  pid = fork();
  if(pid == 0) {
  printf("hi 1 \n");
  pid1 = fork();
  if (pid1 == 0)
  {   pid_t=getpid();
      printf("Forked child %d \n", pid_t);
      fflush(stdout);
      num=(rand() % T);
      sleep(num);
      S=A+B;
      printf("I am child number %d with pid_t %d, the sum is %d \n", count+1, pid_t, S);
      //pid1 code...
      exit(EXIT_SUCCESS);
  }
  else
      if (pid1 &lt; 0)
      {
          printf("Fork error %d.\n", errno);
      }
  
  pid2 = fork();
   if (pid2 == 0)
  {   pid_t=getpid();
      printf("Forked child %d \n", pid_t);        
  //  fflush(stdout);
      num=(rand() % T);
      sleep(num);
      S=A-B;
      printf("I am child number %d with pid_t %d, the difference is %d", count+1, pid_t, S);
          //pid2 code...
      exit(EXIT_SUCCESS);
  }
  else
      if (pid2 &lt; 0)
      {
          printf("Fork error %d.\n", errno);
      }
  
  pid3 = fork();
   if (pid3 == 0)
  {   pid_t=getpid();
      printf("Forked child %d \n", pid_t);
     // fflush(stdout);
      num=(rand() % T);
      sleep(num);
      S=A*B;
      printf("I am child number %d with pid_t %d, the product is %d", count+1, pid_t, S);
      //pid3 code...
      exit(EXIT_SUCCESS);
  }
  else
      if (pid3 &lt; 0)
      {
          printf("Fork error %d.\n", errno);
      }
  pid4 = fork();
   if (pid4 == 0)
  {   pid_t=getpid();
      printf("Forked child %d \n", pid_t);
    //  fflush(stdout);
      num=(rand() % T);
      sleep(num);
      S=A/B;
      rem=A%B;
      printf("I am child number %d with pid_t %d, the quotient is %d and the remainder is     %d \n", count+1, pid_t, S, rem);
      //pid4 code...
      exit(EXIT_SUCCESS);
  }
  else
      if (pid4 &lt; 0)
      {
          printf("Fork error %d.\n", errno);
      }
  }
  else 
  printf("\n no processes ceated \n");
  
  for (i = 0; i &lt; 3; ++i)
  {
  wait(&amp;status);
  }
  printf("i am done waiting \n");
  return 1;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>incorrect number of arguments 
  20,      156,      82 
  hi 1 
  Forked child 14917 
  incorrect number of arguments 
  20,      156,    82 
  hi 1 
  Forked child 14978 
  incorrect number of arguments 
  20,      156,    82 
  hi 1 
  Forked child 15283 
  </code></pre>
  
  <p>Inline Command arguments :</p>
  
  <pre><code>main 20 156 82
  </code></pre>