how to terminate a child process properly

  • Follow


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
0
Reply sam 4/19/2004 2:04:55 PM

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>]
0
Reply sam 4/19/2004 2:23:34 PM


-----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+MragVFX4UWr64RAhMvAKCS3CCSi82dokLtwM8ehuhYukgl3ACg1FJd
Ftb8s8HVZFCG8/M3SJvszBM=
=++RD
-----END PGP SIGNATURE-----
0
Reply Lew 4/19/2004 2:33:17 PM

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);
         }
}
0
Reply sam 4/19/2004 2:36:39 PM

In article <c60om6$265d$1@news.hgc.com.hk>,
 sam <samwun@hgcbroadband.com> wrote:

> 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>]

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 ***
0
Reply Barry 4/19/2004 9:43:58 PM

4 Replies
399 Views

(page loaded in 0.081 seconds)

Similiar Articles:













7/23/2012 5:55:18 PM


Reply: