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);

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.

Yes it works, thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.