use Nice; # load the Nice.pm module
tie $his_speed, 'Nice', getppid();
tie $my_speed, 'Nice', $$;
*****
$speed = $his_speed;
*****
$speed = (tied $his_speed)->FETCH():
*****
$myobj = tie $my_speed, 'Nice', $$;
$speed = $my_speed; # through the implicit interface
$speed = $myobj->FETCH(); # same thing, explicitly
*****
package Nice;
use Carp; # Propagates error messages nicely.
use BSD::Resource; # Use these hooks into the OS.
use strict; # Enforce some discipline on ourselves,
use vars '$DEBUG'; # but exempt $DEBUG from discipline,
*****
sub TIESCALAR {
my $class = shift;
my $pid = shift;
$pid ||= $$; # arg of 0 defaults to my process
if ($pid =~ /\D/) {
carp "Nice::TIESCALAR got non-numeric pid $pid" if $^W;
return undef;
}
unless (kill 0, $pid) { # EPERM or ERSCH, no doubt
carp "Nice::TIESCALAR got bad pid $pid: $!" if $^W;
return undef;
}
return bless \$pid, $class;
}
*****
$pid = $pid || $$; # set if not set
*****
sub FETCH {
my $self = shift; # ref to scalar
confess "wrong type" unless ref $self;
croak "too many arguments" if @_;
my $nicety;
local $! = 0; # preserve errno
$nicety = getpriority(PRIO_PROCESS, $$self);
if ($!) { croak "getpriority failed: $!" }
return $nicety;
}
*****
sub STORE {
my $self = shift;
my $new_nicety = shift;
confess "wrong type" unless ref $self;
croak "too many arguments" if @_;
if ($new_nicety PRIO_MAX) {
carp sprintf
"WARNING: priority %d greater than maximum system priority %d",
$new_nicety, PRIO_MAX if $^W;
$new_nicety = PRIO_MAX;
}
unless (defined setpriority(PRIO_PROCESS, $$self, $new_nicety)) {
confess "setpriority failed: $!";
}
return $new_nicety;
}
*****
sub DESTROY {
my $self = shift;
confess "wrong type" unless ref $self;
carp "[ Nice::DESTROY pid $$self ]" if $DEBUG;
}