#!/usr/bin/perl

$cnt = ($ARGV[0] eq "-n" && $ARGV[1] =~ /^\d+$/) ? $ARGV[1] : 20;

opendir(PROC, "/proc");
while ($_ = readdir(PROC)) {
    next if (/curproc/);
    $id = (stat("/proc/$_"))[4];
    next unless defined $id;
    $procs{$id}++;
}
closedir(PROC);

$procs{$<}--;
@ids = reverse sort { $procs{$a} <=> $procs{$b} } keys(%procs);

format STDOUT =
@>>>  @<<<<<<<
$procs{$id}, (getpwuid($id))[0]
.

for $id (($cnt) ? @ids[0..($cnt-1)] : @ids) {
    write;
}
