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. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-21-2004, 02:17 PM
|
#1
|
Member
Registered: May 2004
Posts: 38
Rep:
|
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
|
|
|
09-21-2004, 02:44 PM
|
#2
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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.
|
|
|
09-21-2004, 02:57 PM
|
#3
|
Member
Registered: May 2004
Posts: 38
Original Poster
Rep:
|
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
|
|
|
09-21-2004, 03:21 PM
|
#4
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
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.
|
|
|
09-21-2004, 08:10 PM
|
#5
|
Member
Registered: May 2004
Posts: 38
Original Poster
Rep:
|
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?
|
|
|
09-21-2004, 08:55 PM
|
#6
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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;
}
|
|
|
10-01-2004, 08:08 PM
|
#7
|
Member
Registered: May 2004
Posts: 38
Original Poster
Rep:
|
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 05:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|