How does re-parenting of stopped process heppens? Why does stopped process just terminates after re-parenting?
More precisely, suppose I have a code like this
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <stdio.h>
int main(void) {
pid_t child;
child = fork();
if (child == 0) {
int t = 0;
while (true) {
printf("%d. I'm %d, my parent is %d\n", t++, getpid(), getppid());
sleep(1);
}
} else {
printf("I'm the parent. My pid is %d\n", getpid());
printf("Starting to wait for 30 seconds\n");
sleep(30);
printf("Done waiting, aborting\n");
}
}
When I run this code the child process works and parent process just sleeps. After 30 seconds passed the parent process terminates and the child process now becomes a child of init
and just continues running. Everything is normal.
But if I run this code and in first 30 seconds of it's execution I also run
kill -SIGSTOP <child_pid>
Then the child process stops (T
state in ps xaf
) and the parent process sleeps. After 30 second passed the parent process returns from sleep and just terminates (as it reached the end of main
) but the child process instead of being re-parented to init
in stopped state just terminates. I don't see it in ps xaf
and if run lastcomm
I see this output:
a.out F X equi pts/5 0.00 secs Wed Mar 16 17:44
Why is this happening that stopped process dies after re-parenting? Is it possible in linux to re-parrent stopped process?