Skip to content

Instantly share code, notes, and snippets.

@plepe
Created August 31, 2012 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plepe/3550591 to your computer and use it in GitHub Desktop.
Save plepe/3550591 to your computer and use it in GitHub Desktop.
A function, returning an array of process ids of children (and their children, ...) of a process
<?php
function get_proc_children($pid, $tree=null) {
$tree=array();
$ret=array();
// build process tree in $tree
if($tree==null) {
$p=popen("ps eo pid,ppid", "r");
while($r=fgets($p)) {
if(preg_match("/^\s*([0-9]+)\s+([0-9]+)/", $r, $m)) {
if(!isset($tree[$m[2]]))
$tree[$m[2]]=array();
$tree[$m[2]][]=$m[1];
}
}
}
// no children? return
if(!isset($tree[$pid]))
return array();
// add grand children
foreach($tree[$pid] as $child) {
$ret=array_merge($ret, get_proc_children($child, &$tree));
}
return array_merge($ret, $tree[$pid]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment