how to terminate a child process properly
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to terminate a child process properly




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how to terminate a child process properly  
sam


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 03:35 PM

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





[ Post a follow-up to this message ]



    Re: how to terminate a child process properly  
sam


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 03:35 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_se
rv09
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>]





[ Post a follow-up to this message ]



    Re: how to terminate a child process properly  
sam


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 03:35 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);
}
}





[ Post a follow-up to this message ]



    Re: how to terminate a child process properly  
Lew Pitcher


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 04:35 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 colle
cts
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-----





[ Post a follow-up to this message ]



    Re: how to terminate a child process properly  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 11: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_serv
09
> <defunct>]
> root      1170  0.0  0.0     0    0 pts/3    Z    22:58   0:00 [p_serv
09
> <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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:53 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register