Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing processes

Status
Not open for further replies.

DZH

Programmer
Apr 12, 2002
26
ZA
Hi guys!

Given a process ID (PID), how do I get the child processes whose parent process is PID? The intent is to programmatically kill a process and all its child processes. If a child process is itself a parent of another process, then the "second generation" child processes should be killed as well. I would like to do this on Solaris using C and any of the scripting langs (SH, AWK, or PERL).

Thanks.
 
I'm not familiar with a standard library function that returns a list of child process IDs. However, since you know the PID you want to kill, you could call getppid() on each possibly valid PID and kill anything that has a parent PID that matches your target.

Perhaps someone else has a more efficient method. I'd like to hear it :)
 
getppid() returns the parent process ID of the calling process, therefore it would not useful in extracting all the required PIDs.
 
My apologies: the getppid() function takes no arguments, so it cannot be used to find the parent of another process.

I have some pstat functions on my HP/UX box that do what you want, but I believe they are proprietary. I'm not sure what the Solaris equivalent is.

Sorry for the confusion :)
 
I suspect this only compiles on HP/UX, but I'm posting it in case it will help you track down a Solaris solution.

Code:
#include <errno.h>
#include <limits.h>     /* For PID_MAX */
#include <sys/param.h>
#include <sys/pstat.h>
#include <pwd.h>
#include <stdio.h>

extern int errno;

int main( int argc, char **argv )
{
  int i, rVal, ppid;
  struct pst_status pst;
  struct passwd *pw;


  /* Validate command line */

  if ( argc < 2 )
  {
    puts( &quot;Usage: kidlist <pid>&quot; );
    return EINVAL;
  }

  ppid = atoi( argv[1] );


  /* Loop through the entire valid PID range and
   * list processes that have parent PIDs that
   * match our provided PID.
   */

  for ( i = 0; i < PID_MAX; i++ )
  {
    rVal = pstat_getproc( &pst, sizeof(pst), 0, i );

    if ( rVal == -1 )
    {
      if ( errno != ESRCH )  /* &quot;PID not found&quot; is okay to ignore */
      {
        perror( &quot;pstat_getproc()&quot; );
        return errno;
      }
    }

    else if ( pst.pst_ppid == ppid )  /* Found a child */
    {
      /* Obtain the name of the process owner */
      pw = getpwuid( pst.pst_uid );

      if ( pw == NULL )
        printf( &quot;(??????) %5d %s\n&quot;, pst.pst_pid, pst.pst_cmd );
      else
        printf( &quot;%-8s %5d %s\n&quot;, pw->pw_name, pst.pst_pid, pst.pst_cmd );
    }
  }

  return 0;
}
 
An alternative would be to parse the output of say 'ps -el' (flags may vary) which lists the PID and PPID of each process.
From that, you can build the ancestor relationships of all the processes.
 
I think this is a double post that has been solved.

thread205-571800
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top