Re: Threads

Richard B. Johnson (root@chaos.analogic.com)
Mon, 4 Oct 1999 15:58:03 -0400 (EDT)


On Mon, 4 Oct 1999, Stanislav Krasilovskiy wrote:

> Hi,
>
> I have a short question, but I lack the expertise to answer it.
>
> I am creating a thread in a custom system call, through a call to
> kernel_thread(). When this thread finishes, it becomes a zombie and
> persists until the user program which called the system call exits. Is
> there any way to all to make the kernel thread die cleanly when it
> finishes? (Without doing something like waitpid()).
>
> Thank you very much.

Make a signal handler for the children (a generic one for all of them).

#include<signal.h>
#include<sys/wait.h>

void reaper(int unused)
{
while(wait3(&unused, WNOHANG, NULL) > 0)
;
}

code()
{

struct sigaction sa;
sigaction(SIGCHLD, NULL, &sa);
sa.sa_handler = reaper;
sa.sa_flags |= SA_RESTART;
sigaction(SIGCHLD, &sa, NULL);
......
......

With this, any child that expires, that has not set up it's own handler,
will be caught and have its status read and thrown away so it can finish
its exit. This way, you will have no zombies.

Cheers,
Dick Johnson
**** FILE SYSTEM WAS MODIFIED ****
Penguin : Linux version 2.3.13 on an i686 machine (400.59 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/