LINUX! In shell scripting if possible and show commands (please do not copy codes form others) Thank you!
Write a simple test program to find out PIDs of ALL ancestor (i.e., parent, grandparent ) processes of the current process or a given process specified by a PID. A parent process's PID of a given process pid can be found in /proc/$pid/status
pid is given as an argument, second is the pid of its parent, grandparent and so on.
use strict;
use warnings;
use Getoption::Long;
use Pod::Usage;
sub _usage_and_exit {
my %option = @_;
pod2usage(
-noperldoc => 1,
-verbose => 99,
-exitval => defined $option{help} && $option{help} ? 0 :
1,
-output => defined $option{help} && $option{help} ?
\*STDOUT : \*STDERR,
);
}
my %option;
my $sw = Getoptionions( \%option, 'help|h', );
my $pid = shift;
_usage_and_exit( help => $option{help} )
if not $sw
or defined $option{help}
or not defined $pid
or $pid !~ /^\d+$/
or $pid < 1
or @ARGV;
while ( $pid > 0 ) {
open my $stat_fh, "<", "/proc/$pid/stat"
or die "no such pid: $pid\n";
my $ppid = ( split /\s+/, <$stat_fh> )[3];
close $stat_fh;
print $pid, "\n";
$pid = $ppid;
}
This scripts non-zero value if it unable to find pid given or the process is in chain.
LINUX! In shell scripting if possible and show commands (please do not copy codes form others)...
For Linux Operating System: Write a simple test program in C to find out PIDs of ALL ancestor (i.e., parent, grandparent ) processes of the current process or a given process specified by a PID. A parent process's PID of a given process pid can be found in /proc/$pid/status