LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Killing a child process from another child (http://www.linuxquestions.org/questions/programming-9/killing-a-child-process-from-another-child-233432/)

marri 09-21-2004 01:17 PM

Killing a child process from another child
 
hi
I was wondering how I could kill a child process from within another child process, since kill () doesn't work for me, and I think it is only possible to use kill() from the parent process or is that bullocks??
regards
marri

jim mcnamara 09-21-2004 01:44 PM

kill() works against any process. The process may be blocking the signal you are sending. No process can block
SIGKILL or SIGSTOP. I'm assuming these processes belong to you.

marri 09-21-2004 01:57 PM

yes, I create those processes, but when one child process tries to kill another one (not the parent) then the process to be killed goes to "defunct" state (viewed with "ps aux").
I use kill (child_pid, SIGTERM) and also have I tried SIGKILL but the process to be killed always goes to this "defunct" state.

In every child I create I include a signalhandler so I don't really see what the problem is

mirradric 09-21-2004 02:21 PM

If a process is in the defunct state, it is already dead.
When a process exits/dies but the parent process which spawned/fork it has not yet waited for it, it goes into a defunct state till the parent waits for it. These processes are also known as zombie processes though they seem to me to be more like unclaimed carcasses.

marri 09-21-2004 07:10 PM

My main process is intended to be running indefinetly, like a server, and I don't want those "zombie" processes piling up.
So do I have to call wait/waitpid() regularly in the main process to clean those "zombie" processes up?

itsme86 09-21-2004 07:55 PM

Just catch SIGCHLD and do something like this:
Code:

void sigchld_handler()
{
  while(waitpid(-1, NULL, WNOHANG));
}

int main(void)
{
  signal(SIGCHLD, sigchld_handler);

  return 0;
}


marri 10-01-2004 07:08 PM

I have a weird problem with forking in my program.

My program is to monitor other hosts by sending them a simple identification request. To do so I want to fork a child for each request so that each host-monitor is in a seperate child process. Then I have another child that is to monitor if some of the child processes are idled, and if so, kill them.

The problem is that after the program has forked child processes and killed them, forking doesn't work anymore. Calling fork retruns -1 all the time.

I can't figure out why it behaves like this.


All times are GMT -5. The time now is 04:42 PM.