How can I cause a child process to exit when the parent does?
There is no tie between a child process and its parent. They may know each others process ID, but there's no hard connection between them. What you're talking about a orphan process. And it's an OS level concern. Meaning any solution is probably platform dependent.
About the only thing I can think of is to have the child check its parents status periodically, exiting if the parent's shutdown. I don't think this would be all that reliable though.
How to make child process die after parent exits?
Child can ask kernel to deliver SIGHUP
(or other signal) when parent dies by specifying option PR_SET_PDEATHSIG
in prctl()
syscall like this:
prctl(PR_SET_PDEATHSIG, SIGHUP);
See man 2 prctl
for details.
Edit: This is Linux-only
exit(...): Will it cause parent process to terminate?
exit
does not cause the parent to also exit. It causes normal process termination for the current process.
How to kill all child processes after parent process termination?
Here's a probably more portable solution.
The fork(2)
system call will return the PID of your child processes, you can store the PIDs, and then you can use kill(2)
to send signal to the children and terminates them.
Notice that SIGKILL
and SIGTERM
signal may require some privileges of the parent process. If it doesn't have such privileges, you can send a SIGCONT
to the child process, and modify the SIGCONT
signal handler in your child process.
!!! Warning sign
From a signal handler using exit()
is not safe. I've just checked the manual man 7 signal
and found that it is not async safe. You can use _exit
, _Exit
or abort
Some pseudo code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void* handler(int sig){
_exit(0);
}
int main()
{
pid_t children[6];
for(int i=0;i<6;i++) // loop will run 6 times(there are 6 child processes.)
{
if((children[i] = fork()) == 0)
{
signal(SIGCONT,handler);
printf("Started [son] pid %d from [parent] pid %d\n",getpid(),getppid());
sleep(10); //child waits 10 seconds,then it exitted.
printf("Exitted [son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
//parent
sleep(5); //parent will wait 5 seconds than it will exit
for(int i=0;i<6;i++)
kill(children[I],SIGCONT);
printf("Parent terminated\n");
exit(0); //parent terminated.(how can I exit the the other 6 child processes too?)
}
Why child process not getting exited before parent process calls wait() function?
Upon exit, the child leaves an exit status that should be returned to the parent. So, when the child finishes it becomes a zombie.
Whenever the child exits or stops, the parent is sent a SIGCHLD
signal.
The parent can use the system call wait()
or waitpid()
along with the macros WIFEXITED
and WEXITSTATUS
with it to learn about the status of its stopped child.
If the parent exits, than you can see your children still as zombie processes (unwaited children ).
wait()
just tells you which child exited so you can get the exit code. If you have more children running, then of course, others could have terminated in the meantime as well.
If you don't care about the exit status, then wait()
is just fine, but you still have to wait on all children you started.
Related Topics
Conversion from 12 Hours Time to 24 Hours Time in Java
Using Spring Resttemplate in Generic Method with Generic Parameter
Convert Two Dimensional Array to List in Java
When Using == for a Primitive and a Boxed Value, Is Autoboxing Done, or Is Unboxing Done
What Does a Jvm Have to Do When Calling a Native Method
Eclipse Error ... Cannot Be Resolved to a Type
Managing Dll Dependencies with Maven
Configuration Using Annotation @Springbootapplication
Getting Xml Node Text Value with Java Dom
How to Create a Delay in Swing
Use Class Name as Root Key for JSON Jackson Serialization
Passing an Array or List to @Pathvariable - Spring/Java
How to Test Abstract Class in Java with Junit