0

Hi all,

I'm making a client server program in Linux. I use fork() to create a child process of the server to handle a new client. When a client disconnects it should kill the child process. At the moment the program is able to kill the fist client(server) that connects, after that one they become zombie processes.

Heres how I've done it:

void handleSIGCHLD() {
   int stat;

   /*This should kill zombie processes*/
   while(waitpid(-1, &stat, WNOHANG) > 0);
}

and put this at the start of main()

signal(SIGCHLD, handleSIGCHLD);
2
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by Danny_501
1

My knowledge of signals is rusty, but here goes nothing...

I think that after a handler is executed (in this cause, 'handleSIGCHLD'), the handle is released, and the function is no longer associated with the signal. Therefore, from inside the handler, you need to call the 'signal' function at the end of your handler to re-associate the two.

For example, this code will not stop running when you send the Ctrl-C interrupt.

void handle(int foo)
{
  printf("Handled\n");
  signal(SIGINT,handle); /* Re-associate the handler. */
}

int main(void)
{
  signal(SIGINT,handle);
  while(1);
  return 0;
}

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.