Linux - Kill Child Processes

Posted on 2009-02-13
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?
Question by:chsalvia
    LVL 84

    Expert Comment

    How did you create the child that you don't have the PID?
    Can you set the process group when you create the child?
    LVL 43

    Accepted Solution

    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()
    LVL 8

    Expert Comment

    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.  
    LVL 84

    Expert Comment

    Can you detach a process to serve as the parent of all the processes you want to kill?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now