(PHP 5, PHP 7, PHP 8)
pcntl_setpriority — Изменить приоритет процесса
$priority, ? int $process_id = null , int $mode = PRIO_PROCESS ): bool
pcntl_setpriority() задаёт приоритет процессу,
указанному в аргументе process_id.
priority
Как правило приоритет priority -
это значение в интервале от -20 до 20.
Приоритет по умолчанию равен 0, при том что более низкое
числовое значение означает более высокий приоритет процесса.
Поскольку уровни приоритета процессов отличаются в различных типах
операционных систем и версиях их ядер, пожалуйста, ознакомьтесь
с вашим системным руководством getpriority(2) для получения
детальной информации о специфике работы функции в вашей системе.
process_idЕсли не указано, то будет использован идентификатор текущего процесса.
mode
Может принимать значение одной из констант
PRIO_PGRP , PRIO_USER ,
PRIO_PROCESS ,
PRIO_DARWIN_BG или PRIO_DARWIN_THREAD .
Функция возвращает true , если выполнилась успешно, или false , если возникла ошибка.
| Версия | Описание |
|---|---|
| 8.0.0 |
process_id теперь допускает значение null.
|
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).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;
}
?>