I am currently trying to learn how to use Linux prctl(PR_SET_CHILD_SUBREAPER), and prctl(PR_GET_CHILD_SUBREAPER).
Unfortunately, I don't seem to understand what is going on when I use these functions. Can someone help me to spot a slip in my understanding?
I've set the main process as subreaper. Then I tried to use fork() to create a child process and did it again to obtain a grandchild process. Then I killed child process, to see what happened with the grandchild, but I am unable to check it.
int main(void) {
int p = fork();
prctl(PR_SET_CHILD_SUBREAPER, 1);
if(p < 0)
return EXIT_FAILURE;
if(p > 0){
//Main process
printf("I am the MainProcess, My PID: %d and PPID: %d\n", getpid(), getppid());
}
else{
printf("I am the Child, My PID: %d and PPID: %d\n", getpid(), getppid());
int p2 = fork();
if(p2 < 0)
return EXIT_FAILURE;
if(p2 > 0){
//still Child process
}
else{
int *reaper = NULL;
prctl(PR_GET_CHILD_SUBREAPER, reaper);
printf("I am the Grandchild, My PID: %d and PPID: %d\n", getpid(), getppid());
printf("Reaper ID: %d\n", *reaper);
kill(getppid(), SIGKILL);
printf("I am the Grandchild, My PID: %d and PPID: %d\n", getpid(), getppid());
prctl(PR_GET_CHILD_SUBREAPER, reaper);
printf("Reaper ID: %d\n", *reaper);
}
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
Output:
I am the MainProcess, My PID: 9088 and PPID: 23010
I am the Child, My PID: 9089 and PPID: 9088
I am the Grandchild, My PID: 9090 and PPID: 9089
To my surprise some printf() occurrences (in granchild part of code) were not called on runtime. What is the cause?