• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • proc_nice()

    (PHP 5, PHP 7)

    修改当前进程的优先级

    说明

    proc_nice(int $increment): bool

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

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

    参数

    $increment

    新的优先级值,具体的设定取决于所运行的平台。

    在 Unix 系统上,较小的值表示较高的优先级,例如:-20,而正数值表示更低的优先级。

    在 Windows 平台上,$increment参数的含义如下:

    优先级可能的值
    高优先级$increment<-9
    较高优先级$increment<-4
    正常优先级$increment< 5&$increment>-5
    较低优先级$increment> 5
    低优先级$increment> 9

    返回值

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

    范例

    使用proc_open()函数将进程设置为高优先级

    <?php
    // Highest priority
    proc_nice(-20);
    ?>
    

    更新日志

    版本说明
    7.2.0在 Windows 平台上可用。

    注释

    Note:可用性

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

    Note:Windows 平台

    proc_nice()函数会改变当前进程优先级,即使 PHP 是使用线程安全模式编译的。

    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 );
    ?>
    
    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"
    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.
    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));
    }
    ?>
    

    上篇:proc_get_status()

    下篇:proc_open()