jadelabe
jadelabe

Reputation: 33

How can I know which child has exited in C?

i have 3 child that end at different times in different order each time, but i dont know how to get which end first

i have something like this, where "winner", "second" and "third" are, random generated times, the first ending is "winner" but i dont know who it is

    wait(&winner);
wait(&second);
wait(&third);
printf("======================================\n");
printf("----------Classification---------------\n");
printf("======================================\n");
printf("-First-.... Team %d , time: %d\n",/*1*/, WEXITSTATUS(winner));
printf("-Second-.... Team %d , time: %d\n",/*2*/, WEXITSTATUS(second));
printf("-Third-.... Team %d , time: %d\n",/*3*/, WEXITSTATUS(third));

Upvotes: 2

Views: 606

Answers (1)

You should read the man page of wait(2). It returns the pid_t of the waited process.

You may also want to use waitpid (with W_NOHANG if you don't want to really wait in a blocking manner).

Also, read signal(7). You could handle SIGCHLD ....

And Advanced Linux Programming has several chapters related to the issue.

Upvotes: 7

Related Questions