PHP Conference Nagoya 2025

pcntl_setpriority

(PHP 5, PHP 7, PHP 8)

pcntl_setpriorityChange the priority of any process

Description

pcntl_setpriority(int $priority, ?int $process_id = null, int $mode = PRIO_PROCESS): bool

pcntl_setpriority() sets the priority of process_id.

Parameters

priority

priority is generally a value in the range -20 to 20. The default priority is 0 while a lower numerical value causes more favorable scheduling. Because priority levels can differ between system types and kernel versions, please see your system's setpriority(2) man page for specific details.

process_id

If null, the process id of the current process is used.

mode

One of PRIO_PGRP, PRIO_USER, PRIO_PROCESS, PRIO_DARWIN_BG or PRIO_DARWIN_THREAD.

Return Values

Returns true on success or false on failure.

Changelog

Version Description
8.0.0 process_id is nullable now.

See Also

add a note

User Contributed Notes 2 notes

up
2
t dot stobbe at blackdogdev dot com
18 years ago
As for the renice function by leandro dot pereira at gmail dot com, this isn't true. pcntl_setpriority() doesn't set the nice level of a process, but instead sets the base priority of it. At first glance this might seem like the same thing, but on a system level, they are actually quite different.

In fact, if you're looking to use pcntl_setpriority() to prioritize your process (a tool or a daemon or what-not), I wouldn't recomend using setpriority at all, but renice it instead. Let the system manage priorities and you'll end up with the results you were looking for.

This applies only to POSIX based systems only (as does the function presented by leandro dot pereira at gmail dot com as well).
up
-1
leandro dot pereira at gmail dot com
19 years ago
The following snippet may be used under older versions of PHP to provide similar functionality. Tested only under Linux.

<?php
function _pcntl_setpriority($priority, $pid = 0)
{
$priority = (int)$priority;
$pid = (int)$pid;

if (
$priority > 20 && $priority < -20) {
return
False;
}
if (
$pid == 0) {
$pid = getmypid();
}

return
system("renice $priority -p $pid") != false;
}

?>
To Top