List PIDs of child processes

Filed under: Perl tips, Technical — vincent @ 18:31

The next script can be usefull to list all PIDs of a given process and its childs (i.e.: threads also under Linux)

It’s quite brute-force, using /proc extensively. Most probably very Linux specific…

#!/usr/bin/perl -w

use strict;

my $pid=shift @ARGV;

# We'll first make a hash %h where:
# each key is the PID of a process
# each value is a list of the PIDs of the sub-process (direct descendant (ie: children) only, no grand child,...)

# we'll loop on all files under /proc, and take only those whose filename is a number
opendir(DIR,"/proc/") or die ("Can't list proc");
my $file;
my %h=(); # key=pid, value=list of children
while($file=readdir(DIR)) {
        next unless $file =~ m/^d+$/;  # only numbers
        open(STAT,"/proc/$file/stat") or next; # open the stat file for this process
        my $s=<STAT>;
        my @f=split(/s+/,$s);
        # pid: $f[0]   parent PID: $f[3]
        push @{ $h{$f[3]} },$f[0];
        close(STAT);
}
closedir(DIR);

my @pids=();

# recursive function to add a pid and the PIDs of children, grand-children,...
sub add($) {
        push @pids,$_[0];
        map { add($_) } @{ $h{$_[0]} } if (defined($h{$_[0]}));
}
# print join("n", map { $_ . ':' . join(',',sort @{ $h{$_}  } )} sort keys %h);

add($pid);

# output results
print join(' ',@pids)."n";

Encoding problems between Perl and MySql V4.0

Filed under: Perl tips — vincent @ 22:01

MySQL v4.0 doesn’t handle encoding very well. Basically, its considering everything you send it as simple binary data. At least, it doesn’t corrupt it, but no conversion is made. I had to interact, from Perl, with a database which was populated by PHP (v4 also), and which had ISO-8859-1 data in it.
(more…)


The power of the Perl ‘map’ function

Filed under: Perl tips — vincent @ 23:55

I’ve spent a few years writing Perl without ever looking carefully at the map function, but since I’ve discovered it, I use it often. I propose here some sample usage that I’ve found usefull. I’ll also show how to use the grep function, which is quite similar in usage.
(more…)


Post to WordPress in Perl (xmlrpc)

Filed under: Perl tips — vincent @ 23:29

Today, I had to enable a custom program to inject posts in a WordPress blog. Since WordPress implements not less than 3 differents APIs to this effect, through XMLRPC, the choice of the technology was easy: Perl and xmlrpc.

The task was complicated by the fact that the page was only accessible by passing through an HTTP Basic Authentication.

(more…)


Get the element from a hash

Filed under: Perl tips — vincent @ 15:02

I’ve tripped on this simple question:
Having a perl hash %h, and knowing that is has only one element, how to get to this element as easily / as efficiently as possible? Of course, we suppose here that you don’t know the key corresponding to this element…
(more…)


Usage of pbyacc

Filed under: Perl tips — vincent @ 00:00

Yacc, and its GNU replacement Bison is a parser generator, i.e. a program to generate some routines which can parse some text and data according to a user-specified grammar.The basic Yacc documentation is availlable on the net, or with man yacc. We detail here only some points.
(more…)