Killing a child process brought down the parent

I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child process, I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

Thanks,
-Beth

Beth <id@net.com> wrote:

I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child process, I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Before calling spawnnvp() to spawn the child process, there is a call to
signal( SIGCHLD, sighandler) to set up the signal handler for SIGCHLD. The
sighandler{} does nothing. When the child process was killed, the parent
process did receive SIGCHLD and the sighandler() routine is called. Do I
need to add code in sighandler() routine to prevent the parent process exit
when the child process died ?

Appreciate your help.

-Beth

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:bgpdii$2jn$15@nntp.qnx.com

Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Beth <id@net.com> wrote:

Before calling spawnnvp() to spawn the child process, there is a call to
signal( SIGCHLD, sighandler) to set up the signal handler for SIGCHLD. The
sighandler{} does nothing. When the child process was killed, the parent
process did receive SIGCHLD and the sighandler() routine is called. Do I
need to add code in sighandler() routine to prevent the parent process exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

The child process is doing the pppd connection. The kill() worked most time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only. However my
pidwait() will not catch the SIGCHLD signal, and what else I would not be
able to do?

Thanks,
-Beth

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:bgra6m$hki$1@nntp.qnx.com

Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a call to
signal( SIGCHLD, sighandler) to set up the signal handler for SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the parent
process did receive SIGCHLD and the sighandler() routine is called. Do I
need to add code in sighandler() routine to prevent the parent process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Make your parents ignore SIGHUP, see if that help you.

-xtang

Beth <id@net.com> wrote in message news:bgrn9u$2en$1@inn.qnx.com

The child process is doing the pppd connection. The kill() worked most
time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only. However
my
pidwait() will not catch the SIGCHLD signal, and what else I would not be
able to do?

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a call
to
signal( SIGCHLD, sighandler) to set up the signal handler for SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the
parent
process did receive SIGCHLD and the sighandler() routine is called. Do
I
need to add code in sighandler() routine to prevent the parent process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the
parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Ignoring SIGHUP didn’t seem to help.

-Beth

“Xiaodan Tang” <xtang@qnx.com> wrote in message
news:bgrr8j$89p$1@nntp.qnx.com

Make your parents ignore SIGHUP, see if that help you.

-xtang

Beth <> id@net.com> > wrote in message news:bgrn9u$2en$> 1@inn.qnx.com> …
The child process is doing the pppd connection. The kill() worked most
time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only.
However
my
pidwait() will not catch the SIGCHLD signal, and what else I would not
be
able to do?

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a
call
to
signal( SIGCHLD, sighandler) to set up the signal handler for
SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the
parent
process did receive SIGCHLD and the sighandler() routine is called.
Do
I
need to add code in sighandler() routine to prevent the parent
process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are
passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the
parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
\

Beth <id@net.com> wrote:

The child process is doing the pppd connection. The kill() worked most time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

Hm… I’m not sure I asked this before, but what OS version are you
running?

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only. However my
pidwait() will not catch the SIGCHLD signal, and what else I would not be
able to do?

By pidwait() do you mean waitpid()? (Or is pidwait() your own routine?)

Hm…what happens if you also catch SIGTERM?
You said: ‘The program exited and prompted with “Terminate”’

Was it actually “Terminated”? If so, that is the shell noticing how
you died and saying it was from a SIGTERM.

Though, ignoring SIGCHLD shouldn’t affect SIGTERM behaviour, and if
you died due to a SIGCHLD, I wouldn’t expect the shell to print out
“Terminated” – it would probably pick something else. I don’t know
what, though. Hm… in fact, I can’t SIGCHLD to actually kill anything.

Can you verify that you are actually getting that SIGTERM?

-David

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a call to
signal( SIGCHLD, sighandler) to set up the signal handler for SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the parent
process did receive SIGCHLD and the sighandler() routine is called. Do I
need to add code in sighandler() routine to prevent the parent process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

I am running QNX 6.2.0.
Yes, I do see the SIGTERM comes into the parent process. If I catch the
SIGTERM, the child process wouldn’t die.
It’s waitpid() that is used to wait the child process to complete and also
wait on kill() to finish.
The question is why the parent is getting the SIGTERM when killing the child
process?

Thank you very much for your help.

-Beth

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:bgs490$hn6$1@nntp.qnx.com

Beth <> id@net.com> > wrote:
The child process is doing the pppd connection. The kill() worked most
time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

Hm… I’m not sure I asked this before, but what OS version are you
running?

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only.
However my
pidwait() will not catch the SIGCHLD signal, and what else I would not
be
able to do?

By pidwait() do you mean waitpid()? (Or is pidwait() your own routine?)

Hm…what happens if you also catch SIGTERM?
You said: ‘The program exited and prompted with “Terminate”’

Was it actually “Terminated”? If so, that is the shell noticing how
you died and saying it was from a SIGTERM.

Though, ignoring SIGCHLD shouldn’t affect SIGTERM behaviour, and if
you died due to a SIGCHLD, I wouldn’t expect the shell to print out
“Terminated” – it would probably pick something else. I don’t know
what, though. Hm… in fact, I can’t SIGCHLD to actually kill anything.

Can you verify that you are actually getting that SIGTERM?

-David

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a call
to
signal( SIGCHLD, sighandler) to set up the signal handler for
SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the
parent
process did receive SIGCHLD and the sighandler() routine is called.
Do I
need to add code in sighandler() routine to prevent the parent
process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the
parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Beth <id@net.com> wrote:

I am running QNX 6.2.0.
Yes, I do see the SIGTERM comes into the parent process. If I catch the
SIGTERM, the child process wouldn’t die.
It’s waitpid() that is used to wait the child process to complete and also
wait on kill() to finish.
The question is why the parent is getting the SIGTERM when killing the child
process?

It shouldn’t.

The most likely case is a bug in your code as to what you pass to
kill().

I don’t know of any OS issues with misdelivery of signals in that
way, and I don’t know of anything that “automatically” generates
a SIGTERM – SIGHUP, SIGCHLD, and others, yes – but not SIGTERM.

So, I don’t have an answer.

-David

Thank you very much for your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgs490$hn6$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
The child process is doing the pppd connection. The kill() worked most
time,
the only time it didn’t work was when I wanted to kill the child process
while it’s dialing modem. I don’t know if that would affect the kill()
process. I did check the pid before calling kill(), the pid is the child
process id, not value of zero.

Hm… I’m not sure I asked this before, but what OS version are you
running?

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn the
child process, it would work as expected, killing the child only.
However my
pidwait() will not catch the SIGCHLD signal, and what else I would not
be
able to do?

By pidwait() do you mean waitpid()? (Or is pidwait() your own routine?)

Hm…what happens if you also catch SIGTERM?
You said: ‘The program exited and prompted with “Terminate”’

Was it actually “Terminated”? If so, that is the shell noticing how
you died and saying it was from a SIGTERM.

Though, ignoring SIGCHLD shouldn’t affect SIGTERM behaviour, and if
you died due to a SIGCHLD, I wouldn’t expect the shell to print out
“Terminated” – it would probably pick something else. I don’t know
what, though. Hm… in fact, I can’t SIGCHLD to actually kill anything.

Can you verify that you are actually getting that SIGTERM?

-David

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a call
to
signal( SIGCHLD, sighandler) to set up the signal handler for
SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the
parent
process did receive SIGCHLD and the sighandler() routine is called.
Do I
need to add code in sighandler() routine to prevent the parent
process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the
parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

I put together a test code to demonstrate the problem that I am having. Hope
I could get some help.

The program starts a child process to do the modem dialing. The
monitor_thread is used to kill the child process when it’s started. ( In
real environment, there is a timer popup, only kill the modem dialing
process when the modem does not get response in certain amount of time. And
we retry this dialing process for a number of times).
The problem is the call to kill() brings down both child and parent process.
The parent process exited with “Terminate” prompt.

Appreciate any help.

-Beth


#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <malloc.h>

pid_t pppdPid=-1;
int num=50;
void* monitor_thread(void* arg);
void modemdial(char * argv[]);

int main(int argc, char * argv[])
{

pthread_create( NULL, NULL, &monitor_thread, NULL );
sleep(5);
while (num >0)
{
if (pppdPid==-1)
{
modemdial(argv);
}
printf(“sleep 10 sec, num=%d\n”, num);
sleep(10);
num–;
}

return 1;
}

void sighandler_child()
{
}

void modemdial( char * argv[])
{
char * arg[4];

signal(SIGCHLD, sighandler_child);

printf(" Start dialing process\n ");

//arg[0]=(char *)malloc(sizeof(argv[1]));
arg[0]=(char *)malloc(sizeof("/usr/sbin/pppd"));
//strcpy(arg[0], argv[1]);
strcpy(arg[0], “/usr/sbin/pppd”);
arg[1]=NULL;

if( (pppdPid=spawnvp ( P_NOWAIT, arg[0], arg)) == -1 )
{
printf(“error\n”);
}
printf(“pppdPid=%d\n”, pppdPid);
}

void* monitor_thread(void* arg)
{
int rc;
int status;
pid_t pid;

printf(“monitor thread start\n”);
sleep(5);
while (1)
{
if(pppdPid >0)
{
printf(“Kill child process %d\n”, pppdPid);
rc=kill(pppdPid, SIGTERM);
if(rc <0 )
{
printf(“Kill error rc=%d\n”, rc);
return NULL;
}
wait:
printf(“waitpid…\n”);
pid=waitpid(pppdPid, &status, WNOHANG);
printf(“pid=%d\n”, pid);
if((pid >0) && (pid==pppdPid))
{
printf(“Process %d is killed\n”, pid);
pppdPid=-1;
}
else
goto wait;
}
sleep(5);
}
return NULL;
}




“David Gibbs” <dagibbs@qnx.com> wrote in message
news:bh0gq8$qd3$1@nntp.qnx.com

Beth <> id@net.com> > wrote:
I am running QNX 6.2.0.
Yes, I do see the SIGTERM comes into the parent process. If I catch the
SIGTERM, the child process wouldn’t die.
It’s waitpid() that is used to wait the child process to complete and
also
wait on kill() to finish.
The question is why the parent is getting the SIGTERM when killing the
child
process?

It shouldn’t.

The most likely case is a bug in your code as to what you pass to
kill().

I don’t know of any OS issues with misdelivery of signals in that
way, and I don’t know of anything that “automatically” generates
a SIGTERM – SIGHUP, SIGCHLD, and others, yes – but not SIGTERM.

So, I don’t have an answer.

-David

Thank you very much for your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgs490$hn6$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
The child process is doing the pppd connection. The kill() worked
most
time,
the only time it didn’t work was when I wanted to kill the child
process
while it’s dialing modem. I don’t know if that would affect the
kill()
process. I did check the pid before calling kill(), the pid is the
child
process id, not value of zero.

Hm… I’m not sure I asked this before, but what OS version are you
running?

If I call signal(SIGCHLD, SIG_IGN) to ignore the signal before spawn
the
child process, it would work as expected, killing the child only.
However my
pidwait() will not catch the SIGCHLD signal, and what else I would
not
be
able to do?

By pidwait() do you mean waitpid()? (Or is pidwait() your own
routine?)

Hm…what happens if you also catch SIGTERM?
You said: ‘The program exited and prompted with “Terminate”’

Was it actually “Terminated”? If so, that is the shell noticing how
you died and saying it was from a SIGTERM.

Though, ignoring SIGCHLD shouldn’t affect SIGTERM behaviour, and if
you died due to a SIGCHLD, I wouldn’t expect the shell to print out
“Terminated” – it would probably pick something else. I don’t know
what, though. Hm… in fact, I can’t SIGCHLD to actually kill
anything.

Can you verify that you are actually getting that SIGTERM?

-David

Thanks,
-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgra6m$hki$> 1@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
Before calling spawnnvp() to spawn the child process, there is a
call
to
signal( SIGCHLD, sighandler) to set up the signal handler for
SIGCHLD.
The
sighandler{} does nothing. When the child process was killed, the
parent
process did receive SIGCHLD and the sighandler() routine is
called.
Do I
need to add code in sighandler() routine to prevent the parent
process
exit
when the child process died ?

No, having a signal handler will prevent you from dieing.

The only other likely case is if, somehow, the variable you are
passing
to kill() picks up a value of zero somehow… which would send the
SIGTERM to you, instead of your child.

-David

Appreciate your help.

-Beth

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bgpdii$2jn$> 15@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
I have a program that spawns a child process by calling
pid=spawnvp(P_NOWAIT… ). When I need to terminate that child
process,
I
used kill(pid, SIGTERM). Sometime, kill() would bring down the
parent
process. The program exited and prompted with “Terminate”.

Any idea what could cause this to happen?

What does the parent do when it gets a SIGCHLD (notification of
child death) signal? When the child dies, the parent will get
this
signal as notification.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


\

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Beth <id@net.com> wrote:

I put together a test code to demonstrate the problem that I am having. Hope
I could get some help.

I tested this under 6.2.1B (which is what I currently have installed)
and it has run to completion (50 iterations) without failing.

I do find the use of the same global variable in different threads without
mutexes a bit… risky, but that is in the race condition type thing,
might only occur rarely (which should be coded around for a final
result, but not likely an issue for demo code.)

I don’t have a 6.2.0 system readily available to test on – if someone
does, maybe they could try it.

-David


The program starts a child process to do the modem dialing. The
monitor_thread is used to kill the child process when it’s started. ( In
real environment, there is a timer popup, only kill the modem dialing
process when the modem does not get response in certain amount of time. And
we retry this dialing process for a number of times).
The problem is the call to kill() brings down both child and parent process.
The parent process exited with “Terminate” prompt.

Appreciate any help.

-Beth



#include <unistd.h
#include <errno.h
#include <string.h
#include <stdio.h
#include <signal.h
#include <pthread.h
#include <sys/types.h
#include <sys/wait.h
#include <malloc.h

pid_t pppdPid=-1;
int num=50;
void* monitor_thread(void* arg);
void modemdial(char * argv[]);

int main(int argc, char * argv[])
{

pthread_create( NULL, NULL, &monitor_thread, NULL );
sleep(5);
while (num >0)
{
if (pppdPid==-1)
{
modemdial(argv);
}
printf(“sleep 10 sec, num=%d\n”, num);
sleep(10);
num–;
}

return 1;
}

void sighandler_child()
{
}

void modemdial( char * argv[])
{
char * arg[4];

signal(SIGCHLD, sighandler_child);

printf(" Start dialing process\n ");

//arg[0]=(char *)malloc(sizeof(argv[1]));
arg[0]=(char *)malloc(sizeof("/usr/sbin/pppd"));
//strcpy(arg[0], argv[1]);
strcpy(arg[0], “/usr/sbin/pppd”);
arg[1]=NULL;

if( (pppdPid=spawnvp ( P_NOWAIT, arg[0], arg)) == -1 )
{
printf(“error\n”);
}
printf(“pppdPid=%d\n”, pppdPid);
}

void* monitor_thread(void* arg)
{
int rc;
int status;
pid_t pid;

printf(“monitor thread start\n”);
sleep(5);
while (1)
{
if(pppdPid >0)
{
printf(“Kill child process %d\n”, pppdPid);
rc=kill(pppdPid, SIGTERM);
if(rc <0 )
{
printf(“Kill error rc=%d\n”, rc);
return NULL;
}
wait:
printf(“waitpid…\n”);
pid=waitpid(pppdPid, &status, WNOHANG);
printf(“pid=%d\n”, pid);
if((pid >0) && (pid==pppdPid))
{
printf(“Process %d is killed\n”, pid);
pppdPid=-1;
}
else
goto wait;
}
sleep(5);
}
return NULL;
}


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

I ran it on 6.2.0 and it ran to completion (50 iterations).

Murf

David Gibbs wrote:

Beth <> id@net.com> > wrote:
I put together a test code to demonstrate the problem that I am having. Hope
I could get some help.

I tested this under 6.2.1B (which is what I currently have installed)
and it has run to completion (50 iterations) without failing.

I do find the use of the same global variable in different threads without
mutexes a bit… risky, but that is in the race condition type thing,
might only occur rarely (which should be coded around for a final
result, but not likely an issue for demo code.)

I don’t have a 6.2.0 system readily available to test on – if someone
does, maybe they could try it.

-David

The program starts a child process to do the modem dialing. The
monitor_thread is used to kill the child process when it’s started. ( In
real environment, there is a timer popup, only kill the modem dialing
process when the modem does not get response in certain amount of time. And
we retry this dialing process for a number of times).
The problem is the call to kill() brings down both child and parent process.
The parent process exited with “Terminate” prompt.

Appreciate any help.

-Beth

#include <unistd.h
#include <errno.h
#include <string.h
#include <stdio.h
#include <signal.h
#include <pthread.h
#include <sys/types.h
#include <sys/wait.h
#include <malloc.h

pid_t pppdPid=-1;
int num=50;
void* monitor_thread(void* arg);
void modemdial(char * argv[]);

int main(int argc, char * argv[])
{

pthread_create( NULL, NULL, &monitor_thread, NULL );
sleep(5);
while (num >0)
{
if (pppdPid==-1)
{
modemdial(argv);
}
printf(“sleep 10 sec, num=%d\n”, num);
sleep(10);
num–;
}

return 1;
}

void sighandler_child()
{
}

void modemdial( char * argv[])
{
char * arg[4];

signal(SIGCHLD, sighandler_child);

printf(" Start dialing process\n ");

//arg[0]=(char *)malloc(sizeof(argv[1]));
arg[0]=(char *)malloc(sizeof("/usr/sbin/pppd"));
//strcpy(arg[0], argv[1]);
strcpy(arg[0], “/usr/sbin/pppd”);
arg[1]=NULL;

if( (pppdPid=spawnvp ( P_NOWAIT, arg[0], arg)) == -1 )
{
printf(“error\n”);
}
printf(“pppdPid=%d\n”, pppdPid);
}

void* monitor_thread(void* arg)
{
int rc;
int status;
pid_t pid;

printf(“monitor thread start\n”);
sleep(5);
while (1)
{
if(pppdPid >0)
{
printf(“Kill child process %d\n”, pppdPid);
rc=kill(pppdPid, SIGTERM);
if(rc <0 )
{
printf(“Kill error rc=%d\n”, rc);
return NULL;
}
wait:
printf(“waitpid…\n”);
pid=waitpid(pppdPid, &status, WNOHANG);
printf(“pid=%d\n”, pid);
if((pid >0) && (pid==pppdPid))
{
printf(“Process %d is killed\n”, pid);
pppdPid=-1;
}
else
goto wait;
}
sleep(5);
}
return NULL;
}


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

John and David, thank you very much for trying the test code. I found out
the reason that you ran the test code successfully was probably because the
settings of “options” for pppd.
I added line “nodetach” the “options” file, this was to make sure the pppd
was not detached from the child process after it’s up and that when child
process was killed, so did the pppd process.
Also, to recreate the problem, I turned the server modem off.

Thanks,
-Beth

“John A. Murphy” <murf@perftech.com> wrote in message
news:3F3D304A.D868B131@perftech.com

I ran it on 6.2.0 and it ran to completion (50 iterations).

Murf

David Gibbs wrote:

Beth <> id@net.com> > wrote:
I put together a test code to demonstrate the problem that I am
having. Hope
I could get some help.

I tested this under 6.2.1B (which is what I currently have installed)
and it has run to completion (50 iterations) without failing.

I do find the use of the same global variable in different threads
without
mutexes a bit… risky, but that is in the race condition type thing,
might only occur rarely (which should be coded around for a final
result, but not likely an issue for demo code.)

I don’t have a 6.2.0 system readily available to test on – if someone
does, maybe they could try it.

-David

The program starts a child process to do the modem dialing. The
monitor_thread is used to kill the child process when it’s started.
( In
real environment, there is a timer popup, only kill the modem dialing
process when the modem does not get response in certain amount of
time. And
we retry this dialing process for a number of times).
The problem is the call to kill() brings down both child and parent
process.
The parent process exited with “Terminate” prompt.

Appreciate any help.

-Beth

#include <unistd.h
#include <errno.h
#include <string.h
#include <stdio.h
#include <signal.h
#include <pthread.h
#include <sys/types.h
#include <sys/wait.h
#include <malloc.h

pid_t pppdPid=-1;
int num=50;
void* monitor_thread(void* arg);
void modemdial(char * argv[]);

int main(int argc, char * argv[])
{

pthread_create( NULL, NULL, &monitor_thread, NULL );
sleep(5);
while (num >0)
{
if (pppdPid==-1)
{
modemdial(argv);
}
printf(“sleep 10 sec, num=%d\n”, num);
sleep(10);
num–;
}

return 1;
}

void sighandler_child()
{
}

void modemdial( char * argv[])
{
char * arg[4];

signal(SIGCHLD, sighandler_child);

printf(" Start dialing process\n ");

//arg[0]=(char *)malloc(sizeof(argv[1]));
arg[0]=(char *)malloc(sizeof("/usr/sbin/pppd"));
//strcpy(arg[0], argv[1]);
strcpy(arg[0], “/usr/sbin/pppd”);
arg[1]=NULL;

if( (pppdPid=spawnvp ( P_NOWAIT, arg[0], arg)) == -1 )
{
printf(“error\n”);
}
printf(“pppdPid=%d\n”, pppdPid);
}

void* monitor_thread(void* arg)
{
int rc;
int status;
pid_t pid;

printf(“monitor thread start\n”);
sleep(5);
while (1)
{
if(pppdPid >0)
{
printf(“Kill child process %d\n”, pppdPid);
rc=kill(pppdPid, SIGTERM);
if(rc <0 )
{
printf(“Kill error rc=%d\n”, rc);
return NULL;
}
wait:
printf(“waitpid…\n”);
pid=waitpid(pppdPid, &status, WNOHANG);
printf(“pid=%d\n”, pid);
if((pid >0) && (pid==pppdPid))
{
printf(“Process %d is killed\n”, pid);
pppdPid=-1;
}
else
goto wait;
}
sleep(5);
}
return NULL;
}


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Beth <id@net.com> wrote:

John and David, thank you very much for trying the test code. I found out
the reason that you ran the test code successfully was probably because the
settings of “options” for pppd.
I added line “nodetach” the “options” file, this was to make sure the pppd
was not detached from the child process after it’s up and that when child
process was killed, so did the pppd process.
Also, to recreate the problem, I turned the server modem off.

Tried that – no failure. But, well, I don’t even have a modem on my
outgoing dial machine, just a serial port… so that may change the timing or
something.

Hm… maybe run with the instrumented kernel, log the events, and look at
what happened that way?

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Thank you all for helping me on this problem. It appeared to be an
undocumented behavior of pppd, that when it’s killed, it would send a
SIGTERM to the parent process.

-Beth


“David Gibbs” <dagibbs@qnx.com> wrote in message
news:bhrhj5$2j4$2@nntp.qnx.com

Beth <> id@net.com> > wrote:
John and David, thank you very much for trying the test code. I found
out
the reason that you ran the test code successfully was probably because
the
settings of “options” for pppd.
I added line “nodetach” the “options” file, this was to make sure the
pppd
was not detached from the child process after it’s up and that when
child
process was killed, so did the pppd process.
Also, to recreate the problem, I turned the server modem off.

Tried that – no failure. But, well, I don’t even have a modem on my
outgoing dial machine, just a serial port… so that may change the timing
or
something.

Hm… maybe run with the instrumented kernel, log the events, and look at
what happened that way?

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

For whoever is interesting:

When pppd in the middle of running a script (connect ), and
got hit by a signal, it will then try to re-send this signal to everybody
in same process group, try to kill all the scripts it started, and
everything
started by the script.

If the pppd is started with “nodetach”, the parent are in this same
group, thus, it got a SIGTERM from pppd and died :frowning:

This is inconvenient to applications, we are looking into improve this
behavior.

-xtang

Beth <id@net.com> wrote in message news:bhtskt$ah1$1@inn.qnx.com

Thank you all for helping me on this problem. It appeared to be an
undocumented behavior of pppd, that when it’s killed, it would send a
SIGTERM to the parent process.

-Beth


“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:bhrhj5$2j4$> 2@nntp.qnx.com> …
Beth <> id@net.com> > wrote:
John and David, thank you very much for trying the test code. I found
out
the reason that you ran the test code successfully was probably
because
the
settings of “options” for pppd.
I added line “nodetach” the “options” file, this was to make sure the
pppd
was not detached from the child process after it’s up and that when
child
process was killed, so did the pppd process.
Also, to recreate the problem, I turned the server modem off.

Tried that – no failure. But, well, I don’t even have a modem on my
outgoing dial machine, just a serial port… so that may change the
timing
or
something.

Hm… maybe run with the instrumented kernel, log the events, and look
at
what happened that way?

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.