jeff-j of osxroot
2/13/2015 - 12:11 AM

returns heirarchical pid tree

returns heirarchical pid tree

#!/usr/bin/perl

#------------------------------------------------------------------------------
# You shouldn't have to change anything below here
#------------------------------------------------------------------------------

use Getopt::Std;

$ENV{PATH}= '/bin:/usr/bin:/usr/ucb';
($prog= $0)=~ s#^.*/##;

#------------------------------------------------------------------------------
# Run PS with the flags appropriate for our OS.
#------------------------------------------------------------------------------

open PS, "/bin/ps -awwx -ouser,pid,ppid,command |";
while(<PS>) {
	chomp;
	($user, $pid, $ppid, $command) = split /\s+/, $_, 4;

	$parent{$pid} = $ppid;
	$info{$pid} = [$user, $command];
	push @{$children{$ppid}}, $pid;
}
close PS;

sub show {
	my($pid, $indent) = @_;

	$aref = $info{$pid};
	print sprintf("%s%5d (%s)   %s\n", $indent, $pid, @$aref);

	foreach $cpid (@{$children{$pid}}) {
		&show($cpid, $indent . '    ');
	}
}

show 0, '';