Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-21-2004, 01:17 PM   #1
Registered: May 2004
Posts: 38

Rep: Reputation: 15
Killing a child process from another child

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??
Old 09-21-2004, 01:44 PM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
Old 09-21-2004, 01:57 PM   #3
Registered: May 2004
Posts: 38

Original Poster
Rep: Reputation: 15
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
Old 09-21-2004, 02:21 PM   #4
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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.
Old 09-21-2004, 07:10 PM   #5
Registered: May 2004
Posts: 38

Original Poster
Rep: Reputation: 15
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?
Old 09-21-2004, 07:55 PM   #6
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Just catch SIGCHLD and do something like this:
void sigchld_handler()
  while(waitpid(-1, NULL, WNOHANG));

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

  return 0;
Old 10-01-2004, 07:08 PM   #7
Registered: May 2004
Posts: 38

Original Poster
Rep: Reputation: 15
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing child process coolfrog Programming 3 09-28-2004 02:58 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
How to debug a child process? koyi Programming 3 09-11-2003 05:23 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration