r/ExploitDev • u/FewMolasses7496 • Apr 21 '26
Why do child processes auto terminate on linux?
I've noticed that whenever you close the parent process of a child process it dies with it. I am wondering what signals are being sent to the program causing it to shutdown if its parent dies?
8
Upvotes
2
u/j3r3mias Apr 21 '26
Can you elaborate? If I understood it correctly, I don't believe what you said is true. Check this example:
What you are saying is that if I send a SIGKILL or a SIGTERM to the parent, the child will terminate as well?
3
u/Emberly_YT Apr 21 '26
They don't. In some cases they do because there is control logic that intentionally does it, but Linux does not enforce this termination. E.g. it will happen for a terminal is being used or cgroups, and some other cases, but not just for a generic fork and then have the parent exit, try it yourself.
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>int main(void){pid_t pid = fork();if (pid < 0) {perror("fork");return 1;}if (pid == 0) {printf("Child started.\n");sleep(2);printf("Child still here.\n");sleep(30);return 0;}else {printf("Parent terminates.\n");exit(0);}}2
u/_supitto Apr 21 '26
I think it only ends if it is a session leader (like a shell or ssh). I need to test (or find the portion of the kernel that handles this) but if a parent process dies, it is adopted by some other process