0

I am using fork & have a problem where if 1 branch exits, the others are left stray.
By "stray", I mean I am back in bash & can type in system commands, but the server is constantly outputting on top of my cursor. The code used to work by killing both branches if I ^c. Since adding sockets into the mix, a client disconnecting caused the server to go stray.

Anyone have ideas as to why?

In my main() :

printf("\n      Server Running   Press ^c to quit\n\n");

if (!fork()) { // Debug output
    while (1) {
        // Do output stuff until ^c signal
        fflush(stdout);
    }
} else { // Internets
    while (1) {
        clfd=accept(listenfd, (struct sockaddr*)&claddr, &claddrlen);
        if (!fork()) { // Listener
            close(clfd);
        } else { // Communication branch
            num=rand()%16;
            //i=recv(clfd, &num, sizeof(num), 0);
            send(clfd, &num, 1, 0);
            close(clfd);
            exit(0);
        }
    }
    exit(0);
}

return 0;

This is from ps:

  PID TTY          TIME CMD
11159 pts/8    00:00:00 sv_run
11899 pts/8    00:00:00 sv_run
11987 pts/8    00:00:00 ps
21687 pts/8    00:00:01 bash

This is is part of the pstree:

|-sshd---sshd---csh
|-sshd---bash---top
|-2*[sv_run]
|-8*[trashapplet]
|-udevd---udevd
Nyaarium
  • 1,540
  • 5
  • 18
  • 34
  • Can you show the output of the command `pstree`? – cyphar Dec 06 '13 at 06:55
  • I don't know what the server is no longer in focus or the clients are causing the server to go stray mean but the code doesn't make a great deal of sense. It's hard to tell if you left too much out in cutting down the code or if you are suffering from some misapprehensions. – Duck Dec 06 '13 at 06:58
  • Wasnt quite sure what was relevant to put in. – Nyaarium Dec 06 '13 at 22:46
  • By "go stray", I mean I am back in bash & can type in system commands, but the server is constantly outputting on top of my cursor. – Nyaarium Dec 06 '13 at 22:50
  • In that case Vlad answered your question. You need to kill the children. – Duck Dec 07 '13 at 00:22

1 Answers1

1

When you press Control+C, you kill the process that have created multiple child processes. Killing the parent does not, by default, kill child processes, and therefore they continue to run while you continue working in the terminal. And because all of the processes share the same output device, the output is interleaved.

I am not sure what do you actually expect your code to do. Neither I know what platform you are using. But if it so happens that you want kill the process and all of its children on SIGINT and you are using Linux, then you can ask the kernel to deliver SIGHUP upon death of the parent in your child processes. For more details on how to do this, see How to make child process die after parent exits?

Alternative, you have to make your server kill of its child processes on exit, for example, by using a custom SIGINT signal handler.

Hope it helps. Good Luck!

Community
  • 1
  • 1
  • Curse myself. Not exactly the solution, but this made me realize I was using fork wrong. I thought fork()==0 was the parent. Turns out 0 – Nyaarium Dec 07 '13 at 23:24