We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Linux - Kill Child Processes

chsalvia
chsalvia asked
on
Medium Priority
5,084 Views
Last Modified: 2012-05-06
I'm trying to figure out the easiest way to have a parent process send a signal to all of its children in a C program written in a Linux environment.

Obviously, the easiest way to signal a particular child process is using the kill system call, and passing it the PID of the child.  But if you don't have the PID of the child, or you want to send a signal to ALL children, is there an easy way to do it?

I realize that you can pass PID 0 to kill(), which will send a signal to every process in the current process group.  Unfortunately, this includes the parent as well.  (I could have the parent block signals, and then have all children unblock, but this seems cumbersome.)

The man page for kill() also says the following:

"If pid is less than -1, then sig is sent to every process in the process group -pid."

I'm not sure what this means.  What does "in the progress group minus pid" mean?  Regardless, I tried passing PID -2 to kill, and it had no effect at all.

So, what is the best way to have the parent process send a signal to all of its children?
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
How did you create the child that you don't have the PID?
Can you set the process group when you create the child?
Top Expert 2005
Commented:
If You haven't recorded pids of Your children - You can only kill the process group (as You noticed, 0 means current, -X means group where process with pid=X belongs).
Refer also: man 2 killpg

But remember to create Your master process group leader. Refer setpgrp() setsid()

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Michael EagerConsultant

Commented:
You should keep track of PIDs for children.  One common way is to have the child process save it's PID somewhere, like /var/run/<program>.pid.  Another process (or the parent) can look in this directory to find the PID of the program.

Killing all of the processes in a process group will kill the parent as well as children.  That may not be what you wanted.  

Also take a look at "killall" or "pkill".  Both commands will kill a process based on the command name.  But I would never put those in a script -- you never know what might be running which has a similar name.  
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Can you detach a process to serve as the parent of all the processes you want to kill?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.