1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| int main()
{
pid_t pid;
cerr<<"------avant le fork------"<<endl;
pid = fork();
cerr<<"------apres le fork------"<<endl;
/* Si pid > 0 on est dans le processus pere
pid = 0 processus fils
pid < 0 le fork n'a pas fonctionne */
if (pid > 0) {
for (int i=0;i <11 ; i++)
cout << i << endl;
cout<<"\tProcessus pere (valeur pid="<<pid<<"), getpid="<<getpid()<<" , getppid="<<getppid()<<endl;
sleep(5);
}
else if (pid == 0){
for (int i=20;i <31 ; i++)
cout << i << endl;
cout<<"\tProcessus fils valeur pid="<<pid<<"), getpid="<<getpid()<<" , getppid="<<getppid()<<endl;
sleep(5);
}
else
cerr<<"Erreur a la creation du processus"<<endl;
cerr<<"------avant le return du main()------"<<endl;
return(0);
} |
Partager