PracticeDev/study_perl/test_m.pl

47 lines
1.0 KiB
Perl
Raw Normal View History

2022-12-20 17:31:11 +08:00
use Data::Dumper;
sub file_read_firstline {
my ($filename) = @_;
my $fh = IO::File->new ($filename, "r");
return undef if !$fh;
my $res = <$fh>;
chomp $res if $res;
$fh->close;
return $res;
}
sub read_proc_pid_stat {
my $pid = shift;
my $statstr = file_read_firstline("/proc/$pid/stat");
if ($statstr && $statstr =~ m/^$pid \(.*\) (\S) (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
print "$statstr\n";
print "$7\n";
print ref \$7, "\n";
return {
status => $1,
ppid => $2,
utime => $3,
stime => $4,
starttime => $7,
vsize => $8,
rss => $9 * 4096,
};
}
return undef;
}
sub test {
my %res = read_proc_pid_stat(1);
print "$res->{starttime}";
while (my ($k,$v)=each %res){print "$k $v\n"};
print "@{[$res]}\n";
foreach my $key ( keys %res ) {
print $key, " => ", $res->{$key},"\n";
}
}
test()