I’m writting a C program that launches another program using the system() function. I’d want to know if there is a possible way to kill the program that is launched, if the main program is killed. I’m programming it for a Linux machine.
Example:
/* foo.c */
int main()
{
system("./blah");
return 0;
}
blah does whatever has to do. If I kill foo, blah is still running.
Is there any way to make foo to kill blah when it dies ?
You’ll need to work with signal handling to know when someone/something is trying to kill your application, read the below documentation for further information.
Besides that you’ll need to know the process id of your spawned child process. For this I’d recommend to use something more sophisticated than
systemto fire up your launched process.You’ll also have to know how to kill the spawned child (using it’s
pid).kill