|
Home > Archive > Unix Programming > April 2004 > how to terminate a child process properly
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 |
how to terminate a child process properly
|
|
|
| Hi,
I have a program need to fork a child which in turn fork other 2
processes (grand children).
I used the following method to kill grand child processes:
kill (grand_child_pid, SIGTERM);
but the child process died as well. Here is the illustration:
Main Proc -> Child Proc -> Grand child Proc
the child process has a for(;;) look to keep the child process alive,
but it still get terminated when the grand child is dead..
any idea how to keep the child process alive when killing grand child?
thanks
sam
| |
|
| sam wrote:
> Hi,
>
> I have a program need to fork a child which in turn fork other 2
> processes (grand children).
> I used the following method to kill grand child processes:
>
> kill (grand_child_pid, SIGTERM);
>
> but the child process died as well. Here is the illustration:
>
> Main Proc -> Child Proc -> Grand child Proc
>
> the child process has a for(;;) look to keep the child process alive,
> but it still get terminated when the grand child is dead..
>
> any idea how to keep the child process alive when killing grand child?
>
>
> thanks
> sam
After have two Grand child Proc killed, the process table in the system
listed two defunc processes:
root@redhat [10:58pm] [...unpv12e/log_server]# ps -auxww | grep p_serv09
root 1159 0.0 0.1 1480 412 pts/3 S 22:57 0:00
../p_serv09 8000
root 1161 0.0 0.1 1488 484 pts/3 S 22:58 0:00
../p_serv09 8000
root 1169 0.0 0.0 0 0 pts/3 Z 22:58 0:00 [p_serv09
<defunct>]
root 1170 0.0 0.0 0 0 pts/3 Z 22:58 0:00 [p_serv09
<defunct>]
| |
|
| sam wrote:
> Hi,
>
> I have a program need to fork a child which in turn fork other 2
> processes (grand children).
> I used the following method to kill grand child processes:
>
> kill (grand_child_pid, SIGTERM);
>
> but the child process died as well. Here is the illustration:
>
> Main Proc -> Child Proc -> Grand child Proc
>
> the child process has a for(;;) look to keep the child process alive,
> but it still get terminated when the grand child is dead..
>
> any idea how to keep the child process alive when killing grand child?
>
>
> thanks
> sam
Here is a test program:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main ()
{
int child_pid;
int grand_child_pid;
int pid;
void sig_chld(int);
signal(SIGCHLD, sig_chld) ;
if ((child_pid = fork()) == 0) {
for (;;) {
printf ("in child..\n");
if ((grand_child_pid = fork()) == 0) {
printf ("grand_child: %ld died\n", getpid());
exit (0);
}
sleep(2);
}
}
return (1);
}
void sig_chld(int signo)
{
int pid, stat;
void pr_cpu_time(void);
printf ("caught sigchld: %ld, parent is: %ld\n", getpid(),
getppid());
pr_cpu_time();
while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) {
printf("child %d terminated\n", pid);
}
}
| |
| Lew Pitcher 2004-04-19, 11:35 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
sam wrote:
| sam wrote:
|
|> Hi,
|>
|> I have a program need to fork a child which in turn fork other 2
|> processes (grand children).
[snip]
|
| After have two Grand child Proc killed, the process table in the system
| listed two defunc processes:
[snip]
Yes? And your problem is?
Remember, a process will remain in the process table until it's parent collects
it's termination status.
- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFAg+MragVFX4UWr64RAhMvAKCS3CCSi82d
okLtwM8ehuhYukgl3ACg1FJd
Ftb8s8HVZFCG8/M3SJvszBM=
=++RD
-----END PGP SIGNATURE-----
| |
| Barry Margolin 2004-04-19, 6:34 pm |
| In article <c60om6$265d$1@news.hgc.com.hk>,
sam <samwun@hgcbroadband.com> wrote:
> sam wrote:
>
> After have two Grand child Proc killed, the process table in the system
> listed two defunc processes:
>
> root@redhat [10:58pm] [...unpv12e/log_server]# ps -auxww | grep p_serv09
> root 1159 0.0 0.1 1480 412 pts/3 S 22:57 0:00
> ./p_serv09 8000
> root 1161 0.0 0.1 1488 484 pts/3 S 22:58 0:00
> ./p_serv09 8000
> root 1169 0.0 0.0 0 0 pts/3 Z 22:58 0:00 [p_serv09
> <defunct>]
> root 1170 0.0 0.0 0 0 pts/3 Z 22:58 0:00 [p_serv09
> <defunct>]
I thought you said that the child died -- isn't 1161 the child (it would
help if you used a ps option that shows the PPID column).
I think you need to read the FAQ entry on "zombie" processes.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|