#! perl -slw use strict; use Data::Dumper; use Win32::Process::Info; ## Get an array of hashes for every process in the system my @info = Win32::Process::Info->new ->GetProcInfo({no_user_info=>1}); ## The hashes contain all the information you will ever need! ## This displays the fields available for one of the processes print Dumper $info[ 0 ]; my @notepads; ## A place to hold the hashes that match the name for my $procRef ( @info ) { ## Scan them all ## Extract the Name, ProcessId KernelModeTime & UserModeTime values ## Using a hash slice my( $name, $pid, $kcpu, $ucpu ) = @{ $procRef }{ qw[Name ProcessId KernelModeTime UserModeTime ] }; if( $name =~ m/notepad/i ) { ## If it matches the name you are looking for ## Add an anon. array containing the PID and the combined KernelMode ## & UserMode time to the array push @notepads, [ $pid, $kcpu + $ucpu ]; } } die 'There are no notepads runnning' unless @notpads; ## Sort the @notepads Array of Arrays (AoA) by cpu utilisation, descending. @notepads = sort{ $b->[ 1 ] <=> $a->[ 1 ] } @notepads; ## The first element (PID) of the first @notepads element ## has the highest cpu utilisation, so kill it. ## On win32, any signal other than 0, 2, & 21 seems to be fatal. ## There may be other non-fatal signals, but it is boring testing for them. kill 1, $notepads[ 0 ][ 0 ];