From: Peter Makholm on
"christoph.rabel(a)gmail.com" <christoph.rabel(a)gmail.com> writes:

> Essentially we spawn a new thread for each client. communicate "talks"
> a while with it and closes the connection afterwards. As it seems, the
> spawned processes end up as zombies. Can I avoid this behavior easily?

As the spawn() function you're using isn't a standard function it is
hard to say how you would reap dead processes correctly. But basically
you have to call the wait() or waitpid() function somewhere. A good
place might be in a SIGCHILD signal handler.

//Makholm
From: Ben Morrow on

Quoth "christoph.rabel(a)gmail.com" <christoph.rabel(a)gmail.com>:
> Hi!
> I have inherited a server application which was moved to Linux a
> couple of weeks ago. Now we see a lot of defunct processes in the
> process list.
>
> Essentially we spawn a new thread for each client. communicate "talks"
> a while with it and closes the connection afterwards. As it seems, the
> spawned processes end up as zombies. Can I avoid this behavior easily?
> Or is ignoring it the best approach?

When you say 'thread', do you mean an ithread (created with threads.pm)
or do you mean a Unix process?

> Here is some code to illustrate our approach:
> (I simplified it slightly, removed some error checking)
> while (($paddr = accept(CLIENT, SERVER)) || !$time_to_die) {
> if ($paddr) {
> my ($port, $iaddr) = sockaddr_in($paddr);
> my $name = gethostbyaddr($iaddr, AF_INET);
> my $ip = inet_ntoa($iaddr);
> logMsg(2, $ip . "(" . $name . "):" . $port . "
> connected");
> spawn (sub {
> communicate($ip, $port);
> });
> }
> }

You haven't shown us what 'spawn' does.

If these are true processes, then IIRC on Linux you can avoid zombies
with

$SIG{CHLD} = "IGNORE";

This is not portable to all Unices, however, so if that may matter in
the future write a proper SIGCHLD handler that calls waitpid. See
perlipc for how to do that.

If these are ithreads, then you probably want to ->detach them.

Ben