文字

proc_nice

(PHP 5, PHP 7)

proc_nice修改当前进程的优先级

说明

bool proc_nice ( int $increment )

proc_nice() 修改当前进程的优先级, 修改量由 increment 参数指定。 increment 为正数会降低当前进程优先级, 反之,为负数会提高优先级。

proc_nice() proc_open() 函数以及和 proc_open() 相关的函数并无什么关系。

参数

increment

优先级变化值。

返回值

成功时返回 TRUE , 或者在失败时返回 FALSE 。。 如果发生错误,例如用户无权修改当前进程的优先级, 会生成 E_WARNING 级别的错误。

注释

Note: Availability

仅在具有 'nice' 能力的系统上才可以使用 proc_nice() 函数。 下列系统含有 'nice':SVr4, SVID EXT, AT&T, X/OPEN, BSD 4.3。 也就是说,此函数在 Windows 平台上不可用。

用户评论:

[#1] kevin AT REMOVETHIS mrkmg.com [2013-04-19 18:17:38]

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_killgetmypid(), 28 );

?>

[#2] Marek [2011-01-19 08:40:43]

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.

[#3] php at richardneill dot org [2010-06-24 00:14:34]

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"

[#4] pandi at home dot pl [2008-11-25 05:22:11]

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));
}

?>

[#5] griph at dd dot chalmer dot se [2003-11-10 14:34:52]

If you don't have PHP5 and needs to nice your process this works good.

<?php

function proc_nice($priority) {
  
exec("renice +$priority ".getmypid());
}

//You also need a shutdown function if you don't want to leave your http deamons with a modified priority
function exit_func(){
  
// Restore priority
  
proc_nice(0);
}

register_shutdown_function('exit_func');
?>

上一篇: 下一篇: