Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateProcess such that child process is killed when parent is killed?

Is there a way to call CreateProcess such that killing the parent process automatically kills the child process?

Perhaps using Create Process Flags?

Edit
The solution is to create a job object, place both parent and child in the job object. Whent he parent is killed the child is killed. I got the code from here: Kill child process when parent process is killed Take note of @wilx's comment about inherited handles.

like image 750
SFun28 Avatar asked Jun 06 '11 23:06

SFun28


People also ask

What happens to child process if parent is killed?

Orphan Processes When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes).

What happens to child process when parent is killed Windows?

In chrome on Windows, the child processes are in a job object and so the OS takes care of killing them when the parent process dies.

Does SIGTERM kill child process?

The SIGKILL signal can be sent with a kill system call. Note though, the SIGTERM handler registered in the following code sample can't catch the delivered SIGKILL , and it immediately kills the given child process.

How do you kill the parent process without killing your child?

1 Answer. Show activity on this post. If you are on the same terminal with the process, type ctrl-z to stop the parent, and the use ps -ef to find the PID of the php child. Use the kill lines above to effectively separate the child from the parent.


1 Answers

Using jobs as Neil says is IMHO the best way. You can make the child processes get killed when the job owning process dies by setting JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE on the job object using SetInformationJobObject(). The job object handle will be closed when your parent process exits/dies. For this to work it is essential that the job handle is not inherited by the child processes. If you want to track also the grand-child processes then you will have to create your child processes suspended, add them to your job object and only then let them run.

like image 99
wilx Avatar answered Oct 13 '22 17:10

wilx