Getting and setting the priority value for the parent process and child process
I have a perl script that will create a child process. I need to get the priority (nice) value for these two processes (parent and child)
I can get the pid of both parent and child processes like below:
$parentPID = $$;
$childPID = fork();
How do I get the priority values for this process in a perl script?
source to share
Note getpriority()
where the first parameter for PID is PRIO_PROCESS
(you can use BSD::Resource
to import this constant or just use zero instead)
Reading the current PID priority and setting a new one,
nice -7 perl -E'say getpriority(0,$$); setpriority(0,$$,9); say getpriority(0,$$)'
Output
7 9
source to share
use the Forks :: Super CPAN module. Example:
$pid = fork { os_priority => 10 }; # like nice(1) on Un*x
if you don't want to use the CPAN module, the setpriority function sets the current priority for a process, process group, or user.
source to share