#!/usr/bin/perl
use strict; use warnings;

open my $fh, q{grep 'PPid:' /proc/[0-9]*/status | sed 's,^/proc/,,;s,/status:PPid:,,' |} or die "can't popen";

my %cpid;

while(<$fh>) {
	chomp;
	my ($pid, $ppid) = split /\s+/, $_;
	if ($pid == $$ && !$ENV{include_self}) {
		next;
	}
#	$ppid{$pid} = $ppid;
	push @{$cpid{$ppid}}, $pid;
}

my @parents = @ARGV;
if ($ENV{include_parents}) { print "$_\n" for @parents; }
while (@parents) {
	my $ppid = shift @parents;
	my @children = @{$cpid{$ppid}||[]};
	print "$_\n" for @children;
	push @parents, @children;
}
