I have an application which is initiated by the shell and terminated by the shell. Now in my application I had created a fork to reduce the load of my application so currently two processes running on same name with different PID's. Now I want to terminate my program, and if I kill with the PID of my process in shell it only kills the parent process leaving it as a zombie process and the child process remains. So how to kill both child and parent processes through the shell?
1
votes
4 Answers
2
votes
2
votes
do ps -el you will get the following
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
get pid and ppid of your process do pkill -P <parent_pid_name> meaning your parent process id which has spawned children and followed by kill <signal number> <pid> In this case too, in place of pid you have to give the parent process id who has spawned all the children .
1
votes
Your father process should trap SIGTERM and kill its child before exit.
I wrote this in C
int child;
void sighandler(int sig) {
if (sig == SIGTERM) {
if (child != -1) kill(child, SIGTERM);
}
}
int main() {
int i;
child = -1;
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, sighandler);
setsid();
setpgid(0,0);
i = fork();
switch(i) {
case -1:
break;
case 0: // child
sleep (100);
break;
default:
child = i;
sleep (100);
break;
}
return 0;
}
You can kill all processes with the same name with the "killall" command.