-1

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?

kaspersky
  • 3,959
  • 4
  • 33
  • 50
  • It looks like a process Oedipus complex... Joking aside, possible duplicate [here](http://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits) – n0p Aug 12 '14 at 09:33
  • The pseudocode looks all wrong. What's the point of forking a child and then immediately waiting for it? – n. m. could be an AI Aug 12 '14 at 09:36
  • @n.m., indeed, for my current needs i serve 1 client at a time, for simplicity. Aside from that the code should be right. – kaspersky Aug 12 '14 at 09:39
  • @Coconop I want the child to _live_ if the parent receives sigint, sry for the confusion. – kaspersky Aug 12 '14 at 09:41
  • How do you kill the parent process? – n. m. could be an AI Aug 12 '14 at 09:42
  • @n.m, I press Ctrl-C. I installed a signal handler using `sigaction`. When parent catches sigint, `exit(0)` is called from the signal handler` – kaspersky Aug 12 '14 at 09:44
  • Ctrl-C delivers the signal to all processes attached to the terminal. If you intend to run a dsemon, have it detach from the tetminal first. – n. m. could be an AI Aug 12 '14 at 09:46
  • Is your signal handler actually called? The normal action when a parent exits before a child is that the child becomes a child of process 1, the 'in it' process. – user207421 Aug 12 '14 at 09:47
  • @n.m, I also used to kill the server with `kill -SIGINT parentpid` from another terminal, the same result, does that make any difference? – kaspersky Aug 12 '14 at 09:47
  • @EJP, yes, the signal handler is called. I wonder if the child inherits the signal hadnler... – kaspersky Aug 12 '14 at 09:50
  • @n.m., looks like you are right, the child inherits the signal handler, and when pressing CTRL-C in terminal, both parent and child receive sigint. When using `kill` command, the child continues to live. You could post that as an answer. – kaspersky Aug 12 '14 at 09:55

1 Answers1

2

When you press Ctrl-C in the terminal, all processes attached to the terminal get the signal. Since the parent handles SIGINT and exits, and the child inherits the handler of the parent, both exit when you press Ctrl-C in the terminal.

When running a server, it is normally recommended to call either setsid or daemon to detach the process from the controlling terminal, so that an accidental Ctrl-C doesn't kill the entire server.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243