/*  
 * #######
 * ## ps_fork.c
 * ## ~~~~~~~
 * ## 04.02.1995: Creation TL
 * ## 04.02.2008:
 * ##
 * ## Ce programme se divise en 2 processus par FORK
 * ##
 * ##
 * ######
 */

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int  pidp, pidf, pidk, pidw;

  printf( " \n" );

  pidp = getpid();
  printf( " ps_fork   > Debut execution : pid = %d\n", pidp );

  /*
   * Creation du processus fils par FORK 
   * Les 2 processsus executent les memes instructions
   * Il sdifferent par leutr PID et la valeur retournee par le fork
   * et stokee dans la variable pidp
   */

  pidk = fork(); 

  if ( pidk == -1 ) {
    printf( " ps_fork   > Creation de processus impossible\n" );
    exit(1);
  }
    
  printf( " ps_fork   > Code execute par les 2 ! mais : getpid() = %d\n ", getpid());

  if ( pidk == 0 ) {

    /* code execute par le processus FILS */
    printf( " ps_fork(f)> Valeur retournee par le fork = %d\n", pidk );

    pidf=getpid();
    printf( " ps_fork(f)> PID( avant exec ) = %d\n", pidf );

    sleep(5);
    execl("ps_exec", "liste des arguments", 0 );

    printf( " ps_fork(f)> Erreur ps_exec\n" );
  } else {

    /* code execute par le processus PERE */
    printf( " PS_FORK(p)> Valeur retournee par le fork = %d\n", pidk );

    /* attente de fin d'execution du (d'un) fils */
    pidw = wait( 0 );
    printf( " PS_FORK(p)> PID du fils se terminant = %d\n", pidw );
  }
  
  printf( " ps_fork   > Fin Processus : %d\n", getpid() );
  exit( 0 );
}
/*--  fin  ps_fork.c  --*/
