#!/usr/bin/env perl
# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
#=======================================================================
# name - show_children.pl
# purpose - show children processes of a specified parent process
#
# !Revision History
# 04May2016  Stassi    Initial version
#=======================================================================
use strict;
use warnings;

# global variables
#-----------------
my ($PID, $PPID);


# main program
#-------------
{
    my (@ps_lines, @parts, $pid, $ppid, @children);

    init();
    @ps_lines = `ps -U $ENV{USER} -lf`;
    foreach (@ps_lines) {
        @parts = split;
        $pid = $parts[3];
        $ppid = $parts[4];

        next if $pid eq "PID";
        unless ($pid == $PID) { push @children, $pid if $ppid == $PPID }
    }
    foreach (@children) { print "$_\n" }
}

#=======================================================================
# name - init
# purpose -
#=======================================================================
sub init {
    usage() unless @ARGV;

    $PPID = shift @ARGV;
    usage() unless $PPID =~ m/^\d+$/;

    $PID = $$;
}

#=======================================================================
# name - usage
# purpose - print usage information to STDOUT
#=======================================================================
sub usage {
    use File::Basename;
    my $name = basename($0);
    print <<"EOF";
usage: $name ppid
description: 
This script prints the list of children process IDs
for ppid, where ppid is the parent process ID.

EOF
;
    exit();
}
