I propose to try the following program. It uses two Linux processes to run two procedures, created by a parent process. As soon as one child finishes, the parent gets notified and terminates the other. Studying the documentation for the system calls appearing in the include section is of course necessary to understand the code.
// fork
#include <unistd.h>
// wait, kill
#include <sys/types.h>
// wait
#include <sys/wait.h>
// kill
#include <signal.h>
// exit
#include <stdlib.h>
//printf
#include <stdio.h>
void proc( int id)
{
int seed= id;
int count= rand_r( &seed) % 10;
printf( "\nprocess[%d]: taking %d steps.", id, count);
int i;
for ( i= 1; i <= count; ++i) {
printf( "\nprocess[%d]: step #%d", id, i);
sleep( 1);
}
printf( "\nprocess[%d]:\tfinished.", id);
exit( 1);
}
int main( int argc, char* argv[])
{
// create 1st child process
pid_t p1= fork();
if ( 0 == p1)
proc( 1); // child does not return
// create 2nd child process
pid_t p2= fork();
if ( 0 == p2)
proc( 2); // child does not return
// this is the parent process
// wait for a child to terminate
pid_t p_terminated= wait( NULL);
// declare result
// terminate the other child
if ( p1 == p_terminated) {
puts( "\nparent: \tprocess[1] finished first.");
kill( p2, SIGKILL);
}
if ( p2 == p_terminated) {
puts( "\nparent: \tprocess[2] finished first.");
kill( p1, SIGKILL);
}
}
On my machine, the program produces the following output:
process[1]: taking 3 steps.
process[2]: taking 7 steps.
process[1]: step #1
process[2]: step #1
process[1]: step #2
process[2]: step #2
process[1]: step #3
process[2]: step #3
process[1]: finished.
parent: process[1] finished first.
It is possible for the two processes to finish at the same time so that they both print their "finished
" message, but even in this case the parent declares one of them to be the first, and terminates the other.