I saw this similar question, which says that the child process won't be automatically killed when the parent process exits. So I wrote a simple program trying to validate that:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main() {
if (!fork()) {
while (1) {
printf("%d: child!\n", getpid());
sleep(5);
}
}
while (1) {
printf("%d: parent!\n", getpid());
sleep(1);
}
return 0;
}
When I ran the program, the output is like this:
8056: parent!
8057: child!
8056: parent!
8056: parent!
8056: parent!
8056: parent!
And I hit ctrl + C
, expecting that the parent process will be killed but child process still around. However, after I hit ctrl + C
:
- The program exited immediately, I was expecting that child process would still write to the terminal if it is not killed. Moreover:
- Neither 8056 or 8057 were shown in the process list.
Does it mean that the child process will be automatically killed when parent process exits? Or did I miss any process management details in a C program?