proc_nice

(PHP 5, PHP 7, PHP 8)

proc_nice β€” Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ процСсса

ОписаниС

function proc_nice(int $priority): bool

Ѐункция proc_nice() измСняСт ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ процСсса Π½Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅, ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠ΅ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ priority. ΠŸΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° priority Π±ΡƒΠ΄Π΅Ρ‚ ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Ρ‚ΡŒ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ процСсса, Π° ΠΎΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° priority Π±ΡƒΠ΄Π΅Ρ‚ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚.

Ѐункция proc_nice() Π½Π΅ связана с Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ proc_open() ΠΈ ΠΊ относящимся ΠΊ Π½Π΅ΠΉ функциям ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

priority

НовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚Π° процСсса. ΠœΠΎΠΆΠ΅Ρ‚ ΠΎΡ‚Π»ΠΈΡ‡Π°Ρ‚ΡŒΡΡ Π½Π° Ρ€Π°Π·Π½Ρ‹Ρ… ΠΏΠ»Π°Ρ‚Ρ„ΠΎΡ€ΠΌΠ°Ρ….

Π’ Unix, Ρ‡Π΅ΠΌ Π½ΠΈΠΆΠ΅ эта Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π°, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ -20, Ρ‚Π΅ΠΌ Π²Ρ‹ΡˆΠ΅ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ процСсса.

Π’ ОБ Windows ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ priority ΡƒΠΌΠ΅Π΅Ρ‚ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Ρ‚ΡŒ Ρ‚Π°ΠΊΠΈΠ΅ значСния:

Класс ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚Π° ДопустимыС значСния
Высокий ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ priority < -9
ΠŸΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Π²Ρ‹ΡˆΠ΅ Π½ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ priority < -4
ΠΠΎΡ€ΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ priority < 5 & priority > -5
ΠŸΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Π½ΠΈΠΆΠ΅ Π½ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ priority > 5
ΠŸΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ оТидания priority > 9

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка. Если происходит ошибка, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ ΠΏΡ€Π°Π² Π½Π° ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚Π°, Ρ‚Π°ΠΊΠΆΠ΅ гСнСрируСтся ошибка уровня E_WARNING.

Бписок измСнСний

ВСрсия ОписаниС
7.2.0 Π­Ρ‚Π° функция стала доступна Π² Windows.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 Установка высокого ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚Π° с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ proc_nice()

<?php
// ΠΠ°ΠΈΠ²Ρ‹ΡΡˆΠΈΠΉ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚
proc_nice(-20);
?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π”ΠΎΡΡ‚ΡƒΠΏΠ½ΠΎΡΡ‚ΡŒ

Ѐункция proc_nice() Π±ΡƒΠ΄Π΅Ρ‚ доступна Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π² Ρ‚ΠΎΠΌ случаС, Ссли ваша опСрационная систСма Π΅Ρ‘ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚. ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ 'nice' ΠΈΠΌΠ΅ΡŽΡ‚: SVr4, SVID EXT, AT&T, X/OPEN, BSD 4.3.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Волько Windows

Π’ Windows эта функция измСняСт ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ процСсса, Π΄Π°ΠΆΠ΅ Ссли PHP Π±Ρ‹Π» собран ΠΊΠ°ΠΊ потокобСзопасный.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • pcntl_setpriority() - Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚ процСсса
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
7
kevin AT REMOVETHIS mrkmg.com ΒΆ
13 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 ΒΆ
15 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 ΒΆ
15 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 ΒΆ
5 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 ΒΆ
17 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));
}

?>