fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. int main(void)
  6. {
  7. printf("Bonjour\n");
  8. int fils1=fork();
  9. int fils2,r;
  10. if(fils1== 0){
  11. sleep(2);
  12. printf("fils 1 PID : %d\n",getpid());
  13. int i=0;
  14. for(i=0;i<1;i++)printf(" %d ",i);
  15. printf("\n");
  16. }
  17. else{
  18. fils2=fork();
  19. if(fils2== 0){
  20. waitpid(fils1,&r,NULL);
  21. printf("pid fils 2: %d\n",getpid());
  22. }
  23. }
  24.  
  25. wait(NULL);
  26. printf("pid pere : %d\n",getpid());
  27. return 0;
  28. }
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
Bonjour
pid fils 2: 29628
pid pere : 29628
Bonjour
pid pere : 29608