title: Factorial calculation using fork() in C for Linux
body: <p>I need to create a program that calculates the factorial number for one previously declared through <em>main</em> argument using fork()</p>
  
  <p>Sorry for the printfs in portuguese, just ignore them, the point is to divide the number that was set on the arguments and make the calculation using two childrens, after that print the result using the father process. But I'm unable to proceed with the result i've got on the childrens, since they are different processes, can anyone help me solve this?</p>
  
  <p>follows the code i've got so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  
  void filho_01();
  void filho_02();
  
  int num=0, total1=1,total2=1, cont=0,num2=0,valor,resultado=0;
  
  int main(int agrc, char** argv) { 
      pid_t var1, var2;
      printf("Calculo de Fatorial com Fork \n\n");
      printf("Processo-Pai: Iniciando Execucao.\n\n");
      printf("Valor Obtido atraves do Parametro\n");
  
      num = atoi(argv[1]); 
      num2=num/2;
      valor=num;
  
      var1=fork();
      if ( var1 == 0 ) filho_01();
      var2=fork();
      if ( var2 == 0 ) filho_02();
      waitpid(var1,&amp;total1,0);
      waitpid(var2,&amp;total2,0);
  
      printf("%d",total1);
      resultado=total1*total2;
  
  
      printf("\nProcesso-Pai: Encerrando Execucao.\n");  
      printf(" \n   Fatorial de %d = %d \n",valor, resultado); 
  
  }
  
  void filho_01() {
  printf("\n\n Filho 1 iniciando:\n");
  printf("Calculando...\n");
  sleep(3);
  
  
      for (cont = num; num &gt;num2; num--) {    
          printf("  filho01: valor = %d \n", num);
          total1=total1*num;
      }
  
           exit(0);
  }
  
  
  void filho_02() {
  printf("\n\n Filho 2 iniciando:\n");
  printf("Calculando...\n");
  sleep(5);
  
  
      for (cont = num2; num2 &gt;=1; num2--) {    
          printf("  filho02: valor = %d \n", num2);
          total2=total2*num2;
      }
         exit(0);
  }
  </code></pre>