cours_progra/bac2/os/chap3/ex3.c

26 lines
424 B
C
Raw Normal View History

2023-11-08 09:42:46 +01:00
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#define printvi(X) printf("%s = %d\n", #X, X);
int main(void)
{
pid_t f = fork();
if(f == 0){
srand(time(NULL));
int delay = 1 + rand() % 10;
int value = rand() % 256;
printvi(delay);
printvi(value);
sleep(delay);
return value;
}
int child_ret = 0;
wait(&child_ret);
printvi(WEXITSTATUS(child_ret));
}