Created
August 31, 2012 09:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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