You can set up a signal handler, and send a signal to the child process from the parent when the parent reaches the end of its execution. The fork
function returns the process id of the child to the parent, so this can be supplied to the kill
function. You can pass it SIGINT
or SIGTERM
or the signal of your choosing.
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
int main( int argc, char* argv[] ) {
pid_t id = 0;
id = fork();
if( id == 0 ) {
/*child code*/
} else if( id == -1 ) {
printf( "Error forking." );
} else {
/*parent code*/
kill( id, SIGINT );
}
return 0;
}