c++ parent process killing child process

I am stuck on a piece of code similar to the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
pid_t pid = fork();
if (pid == 0) 
{
    //in child process
    do_something();
}
else
{
    cout << "Press enter to end\n";
    cin.get();
    prctl(PR_SET_PDEATHSIG,SIGHUP);
}

return 0;


What I thought was supposed to happen here is that, after the fork, the child process goes off and does something, while the parent process waits on user input (i.e. press enter), and then terminates. I was under the assumption that the prctrl statement above would terminate all child processes. However, when I execute my program and monitor the processes with "ps -a", I notice that the parent process and (I assume) the child process is left stranded. How would I go about killing the child process when the parent process ends?
i think there is a kill function in the posfix library. let me see if i can find it. yep: http://linux.die.net/man/3/kill
also, its up to you, but based on what you said it sounds like you want std::thread more than fork()
SIGHUP doesn't tell a process to stop. Did you write a signal handler for SIGHUP that terminates the process?
Nice Linux-only API I didn't know about..

from man 2 prctl, PR_SET_PDEATHSIG section:

"This is the signal that the current process will get when its parent dies"

your current process is the parent, move that line into the child, before do_something (or switch to much more portable POSIX APIs)
Last edited on
Topic archived. No new replies allowed.