Alejandro Garcia
Alejandro Garcia

Reputation: 1221

Killing a subprogram created by other program when its killed

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 ?

Upvotes: 0

Views: 126

Answers (1)

Filip Roséen
Filip Roséen

Reputation: 63807

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 system to fire up your launched process.


You'll also have to know how to kill the spawned child (using it's pid).

Upvotes: 4

Related Questions