G'day Karl,

My main issues with the way you're currently doing this is that you're reading the entire process table for every ancestry level you want and, for each of those levels, you're repeating almost identical code.

Here's a technique that only reads the process table once and allows access to the process data in a variety of ways (I've shown a few examples). I've used your data (and some extra for testing) in a dummy AoH reference ($table) rather than attempting to recreate a Proc::ProcessTable object: you'll need to make changes (e.g. '$_->{pid}' to '$_->pid') for a real solution. Also note that by changing the value of $ancestry, you can get whatever number of ancestry levels you want; although, you may want to rethink my constant names if you start using 'GREATGREAT...GREATGRANDPARENT'. :-)

#!/usr/bin/env perl use strict; use warnings; use constant { CHILD => 0, PARENT => 1, GRANDPARENT => 2, GREATGRANDPARENT => 3, }; my $table = [ { pid => 0, ppid => 0, cmd => 'root' }, { pid => 603, ppid => 0, cmd => 'cron_parent' }, { pid => 18421, ppid => 603, cmd => 'cron_child' }, { pid => 18423, ppid => 18421, cmd => 'sh_mon' }, { pid => 18425, ppid => 18423, cmd => 'sh_kit' }, { pid => 18444, ppid => 18425, cmd => 'java' }, { pid => 28421, ppid => 603, cmd => 'cron_child' }, { pid => 28423, ppid => 18421, cmd => 'sh_mon' }, { pid => 28425, ppid => 18423, cmd => 'sh_kit' }, { pid => 28444, ppid => 18425, cmd => 'java' }, { pid => 28445, ppid => 18425, cmd => 'java' }, { pid => 99999, ppid => 18421, cmd => 'java' }, { pid => 2, ppid => 1, cmd => 'unwanted' }, ]; my $ancestry = 3; my %tree; { my %all; for (@$table) { $all{$_->{pid}} = $_; $tree{$_->{pid}} = [ $_ ] if $_->{cmd} =~ /java/; } for my $child (keys %tree) { my $ppid = $all{$child}{ppid}; for (1 .. $ancestry) { push @{$tree{$child}}, {%{$all{$ppid}}}; $ppid = $all{$ppid}{ppid}; } } } use Data::Dump; print "*** All parents ***\n"; dd [ map { $tree{$_}[PARENT] } sort keys %tree ]; print "*** 18444 grandparent ***\n"; dd $tree{18444}[GRANDPARENT]; print "*** 18444 greatgrandparent ***\n"; dd $tree{18444}[GREATGRANDPARENT]; print "*** All ancestries ***\n"; dd \%tree;

Output:

*** All parents *** [ { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "cron_child", pid => 18421, ppid => 603 }, ] *** 18444 grandparent *** { cmd => "sh_mon", pid => 18423, ppid => 18421 } *** 18444 greatgrandparent *** { cmd => "cron_child", pid => 18421, ppid => 603 } *** All ancestries *** { 18444 => [ { cmd => "java", pid => 18444, ppid => 18425 }, { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "sh_mon", pid => 18423, ppid => 18421 }, { cmd => "cron_child", pid => 18421, ppid => 603 }, ], 28444 => [ { cmd => "java", pid => 28444, ppid => 18425 }, { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "sh_mon", pid => 18423, ppid => 18421 }, { cmd => "cron_child", pid => 18421, ppid => 603 }, ], 28445 => [ { cmd => "java", pid => 28445, ppid => 18425 }, { cmd => "sh_kit", pid => 18425, ppid => 18423 }, { cmd => "sh_mon", pid => 18423, ppid => 18421 }, { cmd => "cron_child", pid => 18421, ppid => 603 }, ], 99999 => [ { cmd => "java", pid => 99999, ppid => 18421 }, { cmd => "cron_child", pid => 18421, ppid => 603 }, { cmd => "cron_parent", pid => 603, ppid => 0 }, { cmd => "root", pid => 0, ppid => 0 }, ], }

[While I can see the Proc::ProcessTable documentation uses indirect object notation, please be aware this syntax is strongly discouraged (explained in perlobj: Invoking Class Methods).]

-- Ken


In reply to Re: Better way to search in the process table? by kcott
in thread Better way to search in the process table? by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.