/**********************************************************************************************
* This is called when SIGCHLD occurs. We need to call the waitpid
function to
* remove the <defunct> process entries from the process table.
(They would
* also be removed if the parent died. But since the parent is
an immortal
* daemon, we should do this. Otherwise too many zombies will
haunt the
* system and crash it. (process table ful)).
* ORIGINAL WRITTEN BY: JIM LOGAJAN
**********************************************************************************************/
void bury_child(int signo)
{
pid_t chld_pid;
int status;
int i;
while((chld_pid = waitpid(-1, &status, WNOHANG))
> 0)
{
if(WIFEXITED(status))
{
fprintf(stdout, "Child exited with exit code(%d). ", WEXITSTATUS(status));
}
else if(WIFSIGNALED(status))
{
fprintf(stdout, "Child was terminated on signal %d. ", WTERMSIG(status));
if(status & 0200)
{
fprintf(stdout, "A core image was produced.\n");
}
}
}
fprintf(stdout, "\n");
if (chld_pid < 0 && errno != ECHILD)
perror("bury_child-->waitpid
returned < 0");
}
******************************************** William Roe Shopfloor Engineer Shopfloor Engineering, BCC, R6L Hewlett Packard, Roseville, CA (916) 785-0689 bill_roe@am.exch.hp.com broe@roester.rose.hp.com ********************************************
John Lorer wrote:
John Hawk wrote:
>
> The code below creates a fork to run grep.
> Code works correctly except for KILL resulting in an inability
> to reuse a grep file search. The question is why the PID goes Zombie vs
> being killed. Replace the PID with 0 and parent is killed as expected.
> Suggestions ?
>
[snip]
> void sigchld_handler (gint signal)
> {
> close (TASK_HANDLE);
> kill (TASK_PID,9);
> TASK_WORKING = FALSE;
> }
>
[snip]See 'man 2 wait'. If a child process exits before its parent's process
and is not waited, it becomes a zombie. To prevent the zombie, use
wait(NULL); or waitpid(TASK_PID, status, options); after the kill().
Read the man page and decide which waitpid() options you need, and
whether you care you about status.HTH
jdl
--
To unsubscribe: mail gtk-app-devel-list-request@redhat.com with
"unsubscribe" as the Subject.Mailing list concerns should be mailed to <listmaster@redhat.com>
--