I have written a tcp server application in C. The code looks somewhat like this:
socket(...);
bind(...);
listen(...);
register_sigint_signal_handler(); // just calls exit(0) when ctrl-c is pressed
for (;;)
{
accept(...);
if (fork() == 0)
{
start_application();
exit(0);
}
waitpid(child);
}
The sigint handler just calls exit(0)
. Whenever a new client connection is established, a child process is created to handle data from that connection. I expect that, when the server (parent process) is killed, the child process should still run, but it does not. Interestingly, when running with gdb, the child continues to run.
What could be the reason that the child process gets killed? What could I do to ensure the child process will continue to run?