PHP 8.3.4 Released!

proc_nice

(PHP 5, PHP 7, PHP 8)

proc_niceChange la priorité d'exécution du processus courant

Description

proc_nice(int $priority): bool

proc_nice() modifie la priorité du processus courant par le paramètre spécifié priority. Un paramètre priority positif atténuera la priorité du processus courant, tandis qu'une valeur négative priority augmentera la priorité.

proc_nice() n'est pas lié à proc_open() et ses fonctions associées d'aucune façon.

Liste de paramètres

priority

La nouvelle valeur de priorité, la valeur de ceci peut différer sur des plates-formes.

Sur Unix, une valeur faible, telle que -20 indique une priorité élevée alors qu'une valeur positive ont une priorité basse.

Pour Windows le paramètre priority a les significations suivantes :

Classe de priorité Valeurs possible
Priorité élevée priority < -9
Au-dessus de la priorité normale priority < -4
Priorité normale priority < 5 & priority > -5
Au-dessous de la priorité normale priority > 5
Priorité inactive priority > 9

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient. Si une erreur survient, par exemple, si l'utilisateur qui tente de changer la priorité d'un processus n'a pas suffisamment de droit pour le faire, une erreur de niveau E_WARNING est générée et false est retourné.

Historique

Version Description
7.2.0 Cette fonction est maintenant disponible sur Windows.

Exemples

Exemple #1 Utilisation de proc_nice() pour définir la priorité de processus à haute

<?php
// Priorité la plus élevée
proc_nice(-20);
?>

Notes

Note: Disponibilité

proc_nice() n'est disponible que sur les systèmes qui disposent de capacités NICE. NICE est compatible avec : SVr4, SVID EXT, AT&T, X/OPEN, BSD 4.3.

Note: Windows seulement

proc_nice() va changé la priorité du processus courent même si PHP a été compilé en utilisant la sécurité des threads.

Voir aussi

add a note

User Contributed Notes 5 notes

up
7
kevin AT REMOVETHIS mrkmg.com
10 years ago
On a Linux system, running apache2 as a non-privileged user you can not increase the niceness of the process after decreasing it. Also, you can not use the apache_child_ terminate either. I found the following does work though:

<?php

//decrease niceness
proc_nice(19);

//kill child process to "reset" niceness
posix_kill( getmypid(), 28 );

?>
up
3
php at richardneill dot org
13 years ago
If a process is reniced, then all its children inherit that niceness. So a PHP script can call proc_nice on itself, then invoke system(), and the command executed via system() will also be niced.

Also worth making a note of ionice. There's no PHP function for this, but it's important. A nice'd program will happily try to chew up all i/o bandwidth with very little CPU usage, it can therefore make the entire computer non-responsive despite the programmer's intention. Use "ionice -c3" or see "man ionice"
up
2
Marek
13 years ago
Regarding ionice - on linux the impact of the ionice -c3 class is similar to that of nice, because the CPU "niceness" is taken into account when calculating the io niceness.
up
0
phil_php at zieaon dot com
2 years ago
It is important to note that this is a relative change. I didn't read the description properly and couldn't figure out why setting proc_nice(0) didn't take the forked children back to 0!
For example if you run:
<?php
proc_nice
(-5);
proc_nice(0); // will have no effect
proc_nice(5); // will take the niceness back to 0

?>

In PHP CLI under Debian (and probably many other Linux flavours) you can read the 'niceness' from the proc filesystem. (There may be a PHP command that gives this info but there doesn't seem to be a link to it on this page.)
E.g
<?php
$Current_Niceness_Value
= intval(explode(" ",file_get_contents("/proc/".getmypid()."/stat"))[18]);

// Note: Older versions of Linux return an unsigned integer which has to be converted to a signed integer.
$Current_Niceness_Value = unpack("l",pack("L",intval(explode(" ",file_get_contents("/proc/".getmypid()."/stat"))[18])))[1];

?>
up
0
pandi at home dot pl
15 years ago
Simple function for check process nice, by default returns nice of current process:

<?php

public static function getProcessNice ($pid = null) {
if (!
$pid) {
$pid = getmypid ();
}

$res = `ps -p $pid -o "%p %n"`;

preg_match ('/^\s*\w+\s+\w+\s*(\d+)\s+(\d+)/m', $res, $matches);

return array (
'pid' => (isset ($matches[1]) ? $matches[1] : null), 'nice' => (isset ($matches[2]) ? $matches[2] : null));
}

?>
To Top