Kill all child processes of a parent but leave the parent alive

linux kill child process when parent dies
kill child process c
perl kill all child processes
c++ kill all child processes
what happens to child process when parent is killed linux
java kill all child processes
can a parent process kill a child process
shell script kill child processes

What would be the best way to kill all the processes of a parent but not the parent? Let's say I have an undetermined number of child processes that I've forked and on a given alarm, in my signal handler, I'd like to kill all my child processes but keep myself running for various reasons.

As of now, I am using kill(-1*parentPid, SIGKILL) but this kills my parent along with its children.

One way to accomplish this is to deliver some signal that can be caught (not SIGKILL). Then, install a signal handler that detects if the current process is the parent process or not, and calls _exit() if it is not the parent process.

You could use SIGUSR1 or SIGUSR2, or perhaps SIGQUIT.

I've illustrated this technique here.

Optionally (as suggested by Lidong), the parent process can use SIG_IGN on the signal before issuing the kill() command.

signal(SIGQUIT, SIG_IGN);
kill(-parent_pid, SIGQUIT);

Killing a process and all of its descendants, Killing processes in a Unix-like system can be trickier than expected. Parent- child, process groups, sessions, and session leaders. It turns that killing of processes when I leave an SSH session is related to process groups,� Let's say I have an undetermined number of child processes that I've forked and on a given alarm, in my signal handler, I'd like to kill all my child processes but keep myself running for various reasons. As of now, I am using kill(-1*parentPid, SIGKILL) but this kills my parent along with its children.

Jxh 's answer is nice. Howevr , maybe you can just give a signal hander for every children process after fork and let it call exit function . and you give a signal hander for parent and let it ignore the signal (like SIGUSR1). this may add the code lines but you don't need detect the process is parent or child in signal hander function.

Kill all descendant processes, I have left the original answer intact because it gives some good This is much less likely to be changed by child processes and, in the case of was the original parent, so if this is the case, you can't expect to be able to kill it� Kill Child Process When Parent Exit C# How to send a HTTP Post Request in Delphi 2010 using WinInet Cannot apply indexing with [] to an expression of type System.Collections.Generic.IEnumerable

you can set the child process a new process group at the fork time, and while you want to kill the child process and its descendants, you can use killpg, example code as:

#include <unistd.h>
#include <signal.h>
#include <stdio.h>

void parent(pid_t pid) {
    killpg(pid, SIGKILL);
}

void child(void) {
    if (-1 == setsid())
        return;

    while(1) {
        sleep(1);
        printf("child\n");
    } 
}


int main() {
    pid_t pid;
    switch ((pid=fork())) {
    case 0: // child
        child();
        break;

    default: // parent
        getchar();
        getchar();
        parent(pid);
    }

    return 0;
}

Ways to kill parent and child processes in one command, Is to kill PID and all its child processes. 2. kill a group of processes with their PGID(Process Group ID). kill -- -$PGID Kill using the default signal (� Now, lets come back to the question (we left open in the last section) about the consequences when parent process gets killed while child is still alive. Well in this case, the child obviously becomes orphan but is adopted by the init process. So, init process becomes the new parent of those child processes whose parents are terminated. 4.

Zombie process, On Unix and Unix-like computer operating systems, a zombie process or defunct process is a When the parent dies, the orphaned child process is adopted by init (process ID 1). signal by default) or has the SA_NOCLDWAIT flag set, all child exit status information will be discarded and no zombie processes will be left. As soon as your process die, all the resources are being released (memory, files and threads) The correct way to do this: when you call beginthread, keep the returned handle in the parent thread, and call WaitForObject before you leave the program (we join the parent thread with the child thread).

Kill child process when parent process is killed, Kill all child processes of a parent but leave the parent alive, One way to accomplish this is to deliver some signal that can be caught (not SIGKILL ). Then, install� A trick: call os._exit to make parent process exit, in this way daemonic child processes will not be killed. But there are some other side affects, described in the doc: Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc. If you do not care about this, you can use it.

How to get child process from parent process, ps: How can i recursively get all child process for a given pid, To get all the processes Kill all child processes of a parent but leave the parent alive, One way to� Killing all Processes within a Process Group. You might think that killing the parent process automatically kills all its children. But not necessarily. In fact, the process group will continue to exist even when the process that started it has died even though the process group ID (pgid) and process ID (pid) are the same.

Comments
  • can you simply keep track of the children's PIDs in a simple array as you fork them?
  • I tried that but since the amount of processes to be forked is not known, I'd have to keep a counter of how many have been made in order to index them correctly and the counter variable's state is not well defined across multiple processes...of course, my implementation of that could be poor.
  • So you can sent a signal to both parent and children ,right ?
  • Lidong, that's part of the quandry, in my current implementation only the parent registers the signal handler...heck, I might as well post the code.
  • is that you scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/… :)
  • Hi Lidong, a small example would go a long way to illustrating what one should do =)