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