(PHP 5, PHP 7, PHP 8)
pcntl_setpriority — Cambia la prioridad de un proceso
$priority, ? int $process_id = null , int $mode = PRIO_PROCESS ): bool
pcntl_setpriority() cambia la prioridad de
process_id.
priority
priority es generalmente un valor que va de
-20 a 20. La prioridad por omisión
es 0 mientras que un valor numérico más pequeño
favorece una mejor planificación. Como los niveles de prioridad
cambian entre los tipos de sistemas y las versiones de kernel, lea
la página de manual getpriority(2) de su sistema para detalles
específicos.
process_id
Si null , se utiliza el identificador del proceso actual.
mode
Una constante entre PRIO_PGRP , PRIO_USER ,
PRIO_PROCESS ,
PRIO_DARWIN_BG o PRIO_DARWIN_THREAD .
| Versión | Descripción |
|---|---|
| 8.0.0 |
process_id es ahora nullable.
|
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;
}
?>