[HRPM] Sorting ps list - source code for server and client

chicks at chicks.net chicks at chicks.net
Sun Nov 26 12:26:44 CST 2000


On Tue, 21 Nov 2000, Matt Shivers wrote:
> Warning it's a lot of code...

That's what 'dd' is for.  :-)

I've attached my rewrite of Matt's script.  There's a longish comment at
the beginning which contains some minor nits, but the main point was to
demonstrate creating a custom sort routine and I hope you will find this a
decent example.

-- 
</chris>

"Pinky, you've left the lens cap of your mind on again." - The Brain
-------------- next part --------------
#!/usr/bin/perl -w

my $comments = q^

Minor nits:
-----------
Let's hope the formatting got dorked in transmission.  :-)
Consistant indentation is a good idea!

I changed
	my $num_procs = @usr_proc_list;
to
	my $num_procs = scalar(@usr_proc_list);
to be more explicit.  This (like the other nits) doesn't affect how
the program works, just how pretty it is and how easy it is to maintain.

I also changed
	my @temp = "\n$num_procs instance(s) of $process_name running on $host_name\n\n";
to 
	my @temp;
	push(@temp,"\n$num_procs instance(s) of $process_name running on $host_name\n\n");
but it could have also been written like
	my @temp = ("\n$num_procs instance(s) of $process_name running on $host_name\n\n");
and I would have been happy.  I hate assigning scalars to arrays and believing
Perl is going to coerce it for me.


I also changed
	# add contents of @usr_proc_list to @temp
	foreach $value (@usr_proc_list) {
		$temp[++$#temp] = $value;
	}
to
	push(@temp, sort pidorder @usr_proc_list);
which is much easier to understand as well as considerably faster.

^;

# test harness

$debug = 1; # 0 for production, 1 for debugging
$host_name = 'norfolk.pm.org';

if ($debug) {
	print get_proc_info('d');
}

#-----------------------------------------------------------------------------
# get_proc_info
#-----------------------------------------------------------------------------

sub get_proc_info {
	my $process_name = shift @_;

	my @ps_list = `ps -eo pid,uname,uid,time,%cpu,%mem,cmd`;

	# let's hope noone's user name conflicts with the process name
	# we're looking for
	my @usr_proc_list = grep {/$process_name/} @ps_list;

	# gets the number of instances of a proc.
	my $num_procs = scalar(@usr_proc_list);

	my @temp;
	push(@temp,"\n$num_procs instance(s) of $process_name running on $host_name\n\n");

	# append sorted processes to @temp
	push(@temp, sort pidorder @usr_proc_list);

	return @temp if $debug;

	# change newlines to question marks, so lines get transmitted
	# across the socket.
	foreach (@temp) {
		s/\n/\?/g; # /g ensures all occurences are substituted
	}

	# convert @temp into a scalar so it can be transmitted across
	# the socket.
	$lines = join("@", @temp)."\n"; # \n is necessary - terminates transfer

	return $lines;
}

sub pidorder {
	my $pid = 0; # index into array
	my $copya = $a;			my $copyb = $b;
	$copya =~ s/^\s+//;		$copyb =~ s/^\s+//;
	my @a = split(/\s+/,$copya,7);	my @b = split(/\s+/,$copyb,7);
	if (0) {
	 my $proca = $a[6];		my $procb = $b[6];
	 $proca =~ s/\s+/ /g;		$procb =~ s/\s+/ /g;
	 print "comparing '$a[$pid]' to '$b[$pid]' ($proca,$procb)\n";
	}
	return $a[$pid] <=> $b[$pid];
}

 


More information about the Norfolk-pm mailing list