r305_dm/TP3/Exo7/exo7.c

53 lines
956 B
C
Raw Normal View History

2023-09-28 22:32:32 +02:00
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
void fils(void)
{
int n;
srand(getpid());
exit(rand() % 128);
}
int main(int argc, char *argv[])
{
int n, max, status;
assert(argc == 2);
n = strtol(argv[1], NULL, 0);
for (int i = 0; i <= n; i++)
{
pid_t p = fork();
assert(p != -1);
if (p == 0)
{
fils();
}
}
wait(&status);
if (WIFEXITED(status))
max = WEXITSTATUS(status);
for (int i = 0; i <= n; i++)
{
wait(&status);
if (WIFEXITED(status))
{
if (WEXITSTATUS(status) > max)
max = WEXITSTATUS(status);
}
}
printf("Le maximum est %d\n", max);
exit(0);
}