//fichier tst-fork.cpp , Patrick TRAU - IPST ULP #include <iostream.h> #include <unistd.h> //pour un petit tuto : http://pficheux.free.fr/articles/lmf/threads/ // ou http://www.estvideo.com/dew/index/page/programmation-systeme-linux int glob; main (int argc, char *argv[]) { pid_t pid; int duree; glob = 1; duree=1; if(argc==2)duree=atoi(argv[1]); pid = fork(); if (pid == 0) { /* Dans le fils */ cout<<"Je suis le fils, pid = "<< getpid() <<", glob = "<< glob <<"\n"; int i; for(i=0;i<3;i++) { cout<<"boucle fils "<<i<<", glob = "<< glob <<"\n"; sleep (duree*2); glob++; } //system("wc /usr/bin/*"); cout<<"Fin du fils, glob = "<< glob <<"\n"; exit (0); } else if (pid > 0) { /* Dans le pere */ cout<<"Je suis le pere, pid = "<< getpid() <<", glob = "<< glob <<"\n"; int i; for(i=0;i<5;i++) { cout<<"boucle pere "<<i<<", glob = "<< glob <<"\n"; sleep (duree); glob++; } cout<<"Fin du pere, glob = "<< glob <<"\n"; exit (0); } else { /* Erreur */ perror ("fork"); exit (1); } }
Patrick
TRAU, ULP - IPST
nov 05