Operating System - HP-UX
1754021 Members
7684 Online
108811 Solutions
New Discussion юеВ

Re: Nice values for background processes

 
David Snider
Frequent Advisor

Nice values for background processes

Ok, Here's the deal, I understand that executing processes in the background (with &) by default get a nice value of 24 instead of the standard 20..

But why is it that if I launch a process in the background within a shell script, this rule does not hold true?

For example:

$sleep 999 &
[1] 21398
$ps -elf|grep sleep|grep -v grep
1 S oracle 21398 20571 0 168 24 50db4540 16 49a9a100 14:49:56 pts/3 0:00 sleep 999
$


But, if I run the following script..

$cat sleeptest
sleep 9999 &

$./sleeptest
$ps -elf|grep sleep|grep -v grep
1 S oracle 21489 1 0 168 20 4fddbcc0 16 4dd28140 14:51:44 pts/3 0:00 sleep 9999
$


The nice value is now 20? Is this right? It must be, but can someone explain to me why?
2 REPLIES 2
Brant Evans
Occasional Advisor

Re: Nice values for background processes

An HP White Paper titled "HP-UX 10.0 Process Management White Paper" states the following about process priority and nice:

All processes have a priority, set when the process is invoked and based on factors such as who is running the process (user, system) and whether the process is created in a time-share or real-time environment.

The nice command can be used to set a process to run at a lower priority than would be set by default. nice does not lower the priority of an already running process. nice is useful for running programs whose execution time is not critical.

For example, suppose you have a program, named numcrunch, that manipulates large arrays of data, but the data is not critical to your work at the moment. How long it takes for the program to manipulate the data is unimportant; more critical programs should have greater access to CPU resources. To run numcrunch as a low priority background process, type:

$ nice numcrunch &

Note that both Korn and C shells handle nice slightly differently: ksh automatically lowers priority of background processes by four; this behavior can be modified using the bgnice argument. If you specify nice from ksh, it executes /usr/bin/nice and lowers priority by ten.

If you specify nice from csh, it executes its built-in command and lowers priority by four; however, if you specify /usr/bin/nice, csh lowers priority by ten.

For details, see nice(1), ksh(1), csh(1), renice(1M), and nice(2) in the HP-UX Reference.


Hope this helps.
Thomas Schler_1
Trusted Contributor

Re: Nice values for background processes

David,

your first example shows that the sleep command is executed along with the & sign directly from your shell. This means the PID of the shell is the parent process (PPID) of the sleep command. The shell, therefore, lowers the nice value, because the shell launches the sleep command. See Brant's explanation.

In your 2nd example, the sleep command is not launched by the shell but by the script 'sleeptest'. The shell launches the script 'sleeptest' using the nice value for normal interactive processes (because there is no & sign appended to './sleeptest'). 'sleeptest' itself is the parent process for the sleep command, but hasn't changed the nice value of the sleep command, despite of the & sign. So, the sleep command gets the nice value of its parent process ('slepptest'). As you can see, the PPID of the sleep command is no longer that of 'sleeptest'. That's because 'sleeptest' immediatly exited after launching the sleep command. Therefore, the PPID changed to 1 (init).

I guess, if you'd executed './sleeptest &', then also the sleep command would run with the nice value 24.

Only the shell renices jobs that are put into the background with the & sign. In the other cases (e.g. using scripts), you should explicitly use the nice commend for renicing.
no users -- no problems