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
| #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#define N 4
static void fils (unsigned, long);
int main (void)
{
for (unsigned i = 1; i <= N; i++)
{
long pid;
switch (pid = fork())
{
case -1 : perror ("erreur creation du fils"); break;
case 0 : fils (i, getpid()); return EXIT_SUCCESS;
default : printf("PERE creation FILS n°%u / pid : %ld\n",
i, pid); break;
}
}
puts ("Voila un dernier message du pere");
return EXIT_SUCCESS;
}
static void fils (unsigned i, long pid)
{
srand ((unsigned)time (NULL));
int attente = rand () * 10.0 / RAND_MAX;
printf ("FILS n°%u / pid : %ld / ppid : %ld\n",
i, pid, (long)getppid());
printf ("FILS n°%u va s'endormir pdt %d sec\n", i, attente);
sleep ((unsigned)attente);
printf ("FILS n°%u se reveille\n", i);
} |
Partager