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 35 36 37 38
| #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
int
main ()
{
int i, n;
printf("entrer le nombre de voitures: ");
scanf("%d",&n);
i=0;
while(i<n)
{
pid_t pid_fils;
do {
pid_fils = fork ( ) ;
}
while ((pid_fils == -1) && (errno == EAGAIN));
if (pid_fils == -1)
{
printf ( "fork( ) impossible, errno=%d\n", errno);
return (1);
i++;
}
if (pid_fils == 0)
{
printf ("Fils : PID=%d, PPID=%d\n",getpid( ) , getppid( ));
return (0);
}
else {
printf ("Père : PID=%d, PPID=%d\n",getpid( ) , getppid( ));
wait (NULL);
return(0);
}
}
} |