Unix Programming - Defunct Processes

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > Defunct Processes





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Defunct Processes
Rookie

2004-10-18, 8:48 pm

Hi,

I am trying to pass a kill(childpid,signal) from a parent process to a child
process when the parent receives a SIGINT signal. I am able to catch the
signal properly at the parent.

But unfortunately when I press ctrl-C only the parent's signal handler is
executing (which is printing a line and sending kill's to the children).I
also put a sleep in the parent's signal handler to see what was happening. I
then did a ps and saw that all the children had been labeled as <defunct>
even before the parent's exit() was called in its signal handler, then when
the parent exited all the children were automatically killed without any
signal handler being called.

Firstly I would appreciate it if someone could tell me what I am doing
wrong. And also whats this <defunct> process thing?



Walter Roberson

2004-10-19, 2:48 am

In article <cl1q3f$dc6$1@gist.usc.edu>,
Rookie <dominicjoseph@rediffmail.com> wrote:
:I am trying to pass a kill(childpid,signal) from a parent process to a child
:process when the parent receives a SIGINT signal. I am able to catch the
:signal properly at the parent.

:But unfortunately when I press ctrl-C only the parent's signal handler is
:executing (which is printing a line and sending kill's to the children).

None of the output routines are marked as safe to use within a signal
handler in POSIX, so you should instead be setting a volatile variable
and returning.

http://docsun.cites.uiuc.edu/sun_do....html#GEN-95948

:I
:also put a sleep in the parent's signal handler to see what was happening. I
:then did a ps and saw that all the children had been labeled as <defunct>
:even before the parent's exit() was called in its signal handler, then when
:the parent exited all the children were automatically killed without any
:signal handler being called.

:Firstly I would appreciate it if someone could tell me what I am doing
:wrong. And also whats this <defunct> process thing?

That means that the child process has exitted but the parent has
no issued a wait() or wait3() to collect the status of the child.
Some state for the child process must be maintained because the parent
might ask for the state information later. Unix deals with that
by marking the process as dead and then releasing the process resources
except for the process control block.
--
Whose posting was this .signature Google'd from?
Rich Teer

2004-10-19, 2:48 am

On Mon, 18 Oct 2004, Rookie wrote:

> I am trying to pass a kill(childpid,signal) from a parent process to a child
> process when the parent receives a SIGINT signal. I am able to catch the
> signal properly at the parent.
>
> But unfortunately when I press ctrl-C only the parent's signal handler is
> executing (which is printing a line and sending kill's to the children).I


Source code, please. Also, printing a line (in fact, calling any of the
standard IO library functions) is not a good idea from a signal handler
(although in a small program you might get away with it).

> Firstly I would appreciate it if someone could tell me what I am doing


Hard to see without seeing your code.

> wrong. And also whats this <defunct> process thing?


A defunct process is a zombie--and a zombie is a process that
has died and whose status hasn't been reaped by its parent.

If the zombie's parent terminates before reaping the child's
status, the child is inherited by init, and IT reaps the
child's status.

I explain all this in greater detail in my book, Solaris
Systems Programming. Have a look around the book's web site
(www.rite-group.com/rich/ssp), and perhaps order a copy from
Amazon.com using the link supplied.

HTH,

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming",
published in August 2004.

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
Rookie

2004-10-19, 2:48 am

> Hard to see without seeing your code.

Here is the code

Parent:



void signalHandler(int n)
{
int i;
for(i=0;i<noOfChildren;i++)
kill(childpids[i],SIGINT);
}

void main()
{
(void) signal(SIGINT,signalHandler);
..
..
..
..
}


Child:

void childSignalHandler(int n)
{
flag=1;
}

void main()
{
(void) signal(SIGINT,childSignalHandler);
..
..
..
..
}


Rich Teer

2004-10-19, 2:48 am

On Mon, 18 Oct 2004, Rookie wrote:

>
> Here is the code
>
> Parent:
>
>
>
> void signalHandler(int n)
> {
> int i;
> for(i=0;i<noOfChildren;i++)
> kill(childpids[i],SIGINT);
> }
>
> void main()


main() is NOT void!

> {
> (void) signal(SIGINT,signalHandler);


You're not checking for errors, either.

Also, you need to show ALL of your code. A few isolated lines
out of context doesn't show much...

(And on a stylistic note, some white space and not using
MiXeD case identifiers would make your code more readable.)

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming",
published in August 2004.

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
seemanta dutta

2004-10-19, 2:48 am

<snip>


>
> :Firstly I would appreciate it if someone could tell me what I am doing
> :wrong. And also whats this <defunct> process thing?



You are not doing anything illegal here. You just forgot to call
wait().
Defunct processes are called zombie processes and get created when the
parent does not call wait(). After a call to wait the parent can
collect the return status of the child along with a lot of other
details which are packed in an integer value.

In case you don't want your children to become zombies and yet also
don't want to wait for them to die, you can use the signal call to
ignore SIGCHLD signal which is emitted when the child exits.

#include<signal.h>

signal(SIGCHLD,SIG_IGN);

That should do the trick.
For more on these things you may want to refer to the unix programming
book by Stevens ( Advanced Unix programming).

regards,
Seemanta
Jim Cochrane

2004-10-19, 5:51 pm

In article <Pine.SOL.4.58.0410182117550.17681@zaphod.rite-group.com>, Rich Teer wrote:
> On Mon, 18 Oct 2004, Rookie wrote:
>
>
> ...
>
> (And on a stylistic note, some white space and not using
> MiXeD case identifiers would make your code more readable.)


He's probably been writing Java for too long and forgot where the
underscore character is on his keyboard.

--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com