20.5 Process Nice Values

   

The "nice value" or "nice number" is one of several factors that allow the system to compute the process priority number for every process. Every system process runs at a nice level between 0 and 39, inclusive. A nice value of 0 is the highest priority and 39 is the lowest . When the system starts, the init process runs with a nice value of 20. Every process that is initiated by another process inherits its nice value from its parent. Thus processes started by the init process have a nice value of 20. At the time a user logs in, a shell program is started. Every user command is a child process of the shell so it has the same nice value the shell has. However, the shell provides a mechanism to increase or decrease process nice values with the help of the nice command. Any user can lower the nice value by increasing the nice number. However, to increase priority, you need more privileges. The nice command is used to change the nice value of a new process, while the renice command is used to change the nice value of a running process.

The nice value scheme provides a facility where you can run lengthy jobs at a lower priority while processes that are required to be completed in a short time are run at a higher priority level.

Listing Nice Priorities

If you use the ps command with the -l option, it lists the nice number under the NI column. Most processes have a default nice value as shown next . I have used the cut command to get rid of unwanted information in the output.

 $  ps -eflcut -c 1-37,85-110  F S      UID   PID  PPID  C PRI NI COMD   3 S     root     0     0  0 128 20 swapper 141 S     root     1     0  0 168 20 init   3 S     root     2     0  0 128 20 vhand   3 S     root     3     0  0 128 20 statdaemon   3 S     root     4     0  0 128 20 unhashdaemon   3 S     root     7     0  0 -32 20 ttisr   3 S     root    22     0  0 100 20 supsched   3 S     root    23     0  0 100 20 smpsched   3 S     root    24     0  0 100 20 sblksched   3 S     root    25     0  0 100 20 sblksched   3 S     root    26     0  0 100 20 strmem   3 S     root    27     0  0 100 20 strweld   1 S     root 29582     1  0 156 20 /usr/sbin/getty console   1 S     root  2052     1  0 154 20 /opt/hpnp/bin/hpnpd   1 S     root  1849     1  0 154 20 /usr/sbin/cron  41 S     root  1527     1  0 120 20 /usr/sbin/xntpd   1 S       lp  1562     1  0 154 20 /usr/sbin/lpsched   1 S     root   407     1  0 154 20 /usr/sbin/syncer   1 S     root   461     1  0 154 20 /usr/sbin/swagentd   1 S     root   795     1  0 154 20 /usr/sbin/portmap   1 S     root   537     1  0 154 20 /usr/sbin/syslogd -D   1 S     root   546     1  0 155 20 /usr/sbin/ptydaemon   1 S     root   565     1  0 127 20 /usr/lbin/nktl_daemon 0   1 S     root  1295     1  0 154 20 /opt/dce/sbin/rpcd   1 S     root   982     1  0 154 20 /usr/sbin/snmpdm   1 S     root   827     1  0 154 20 /usr/sbin/biod 4   1 S     root   828     1  0 154 20 /usr/sbin/biod 4 <Some output truncated here> 

As you can see, all of the processes have a nice value equal to 20. If you want to run a process with a nice value other than the default, you use the nice command when starting the process. The nice command accepts a nice change value that shows the difference between the default nice value and the new nice value. To run a program myprog with a nice value of 25 (lower than the default value of 20), use the following command.

 nice -5 myprog 

To run a program at a higher nice level of 15, use the following command.

 nice --5 myprog 

Note that you need to provide two negative symbols to raise the priority of a program. If you are running a program in the background with the " & " symbol at the command line, it is run with a nice -4 value by default. It means that the program will run at a nice level of 24 instead of 20. The default nice value of the following program is 29.

 nice -5 myprog & 

If the nice change value is not specified with the command, a default value of 10 is used. For example, the following command runs the vi editor at a nice level of 30 instead of 20.

 nice vi 

You can create a subshell of the current shell that is running at a lower level as shown here.

 nice -5 sh 

Now the new shell is running at a nice level of 25. Any command that you create after that will have a default nice level of 25. Now the command, nice vi , will result in running the vi editor at the nice level of 35. To go back to the previous shell, use the exit command.

Changing the Nice Values of Running Processes

Nice values of running process can be changed using the renice command. This command accepts a change value, like the nice command does, which is relative to the current nice level of the process. The renice command takes the PID of the running process as its argument. Let's take the example of the process created when running a shell program named myprog in the background. Since it was running in the background, its nice value was 24. To change the nice value of myprog running in background from 24 to 25, use the following command, where 5116 is the process ID of the process created by myprog .

 renice -n 5 5116 

Note that the nice value is changed from 24 to 25 (not from 24 to 29). This is because the new nice value is determined with respect to the default nice value.

The renice command can be used to alter the nice values of a group of processes by using one of the following options.

-g Use the group ID instead of process ID with this option. It will change the nice values of all processes that fall into that group.
-u Use the user ID with this option. It will change the nice values of all processes owned by that user.

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net