37 lines
687 B
C
37 lines
687 B
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/wait.h>
|
||
|
#include <assert.h>
|
||
|
|
||
|
void fils(){
|
||
|
srand(getpid());
|
||
|
exit(rand()%128);
|
||
|
}
|
||
|
|
||
|
int main(int argc, char** argv){
|
||
|
int i, n, max, status;
|
||
|
pid_t p;
|
||
|
if (argc != 2){
|
||
|
printf("Usage : %s <entier n>", argv[0]);
|
||
|
exit(1);
|
||
|
}
|
||
|
n = (int) strtod(argv[1], NULL);
|
||
|
for (i = 1; i <= n; i++){
|
||
|
p = fork();
|
||
|
assert(p != 1);
|
||
|
if (p == 0)
|
||
|
fils();
|
||
|
}
|
||
|
wait(&status);
|
||
|
max = WEXITSTATUS(status);
|
||
|
for (i = 2; i <= n; i++){
|
||
|
wait(&status);
|
||
|
if (WEXITSTATUS(status) > max)
|
||
|
max = WEXITSTATUS(status);
|
||
|
}
|
||
|
printf("Le plus grand nombre choisi par les processus est %d\n", max);
|
||
|
exit(0);
|
||
|
}
|