1--TEST--
2proc_nice() basic behaviour
3--CREDITS--
4Italian PHP TestFest 2009 Cesena 19-20-21 june
5Fabio Fabbrucci (fabbrucci@grupporetina.com)
6Michele Orselli (mo@ideato.it)
7Simone Gentili (sensorario@gmail.com)
8--SKIPIF--
9<?php
10if(!function_exists('proc_nice')) die("skip. proc_nice not available ");
11if(substr(strtoupper(PHP_OS), 0, 3) == 'WIN') die('skip. not for Windows');
12exec('ps -p 1 -o "pid,nice"', $output, $exit_code);
13if ($exit_code !== 0) {
14    die("skip ps -p is not available");
15}
16?>
17--FILE--
18<?php
19    function getNice($id)
20    {
21        $res = shell_exec('ps -p ' . $id .' -o "pid,nice"');
22        preg_match('/^\s*\w+\s+\w+\s*(\d+)\s+(-?\d+)/m', $res, $matches);
23        if (count($matches) > 2)
24            return $matches[2];
25        else
26            return -1;
27    }
28    $delta = 5;
29    $pid = getmypid();
30    $niceBefore = getNice($pid);
31    proc_nice($delta);
32    $niceAfter = getNice($pid);
33    // The maximum niceness level is 19, if the process is already running at a high niceness, it cannot be increased.
34    // Decreasing is only possible for superusers.
35    var_dump(min($niceBefore + $delta, 19) == $niceAfter);
36?>
37--EXPECT--
38bool(true)
39