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