Starting Processes with nice

The nice command starts another program with an altered process priority. The nice command's syntax is simple but potentially a bit confusing:

nice [option] [command [argument]]

Note Some shells, such as csh, include their own built-in implementations olnice. You can either use the shell's own nice or the standalone nice command with such shells. To use the standalone nice command, though, you must type its complete path, usually /bin/nice or/usr/bin/nice.

The program accepts just three options: -help, to display basic usage information; -version, to display the program's version number; and -n or-adjustment, to set the new process's priority. Instead of using -n or-adjustment, you can enter the new priority by preceding the number by a single dash (-), as in -12 to set a priority of 12. The command and argument are the command you want to run and any arguments it takes. If you pass no arguments to nice, it returns the priority of the shell from which you ran it.

The confusing part of nice is that the priority value seems backward. You can set a process's priority to anywhere from -20 to 19, but contrary to intuition, numerically higher priority values correspond to lower scheduling priority. For instance, if you run two processes, one with a nice value of 10 and the other with a value of-10, the process run at -10 will consume more CPU time. The default value when you run a program without nice is 0. (Figure 14.2 shows the nice value in the Nl column of top's output, revealing that most processes are run at a 0 nice value.) If you don't pass any adjustment value to nice, it assumes a value of 10. Therefore, you can achieve a good reduction in CPU demand for an individual program by launching it with nice and no other options, as in nice crunch.

Adding to the confusion of specifying priorities with nice is the possibility of specifying a value without using -n or-adjustment. For instance, typing nice -n 7 crunch and typing nice -7 crunch accomplish the same thing—both reduce the priority of the crunch program. The second command looks like a -7 priority, but in fact the dash isn't a minus sign in this context.

In all cases, the value you pass to nice is an adjustment—a change to the priority of the shell you're using. For instance, if you're using a normal shell in an xterm with a 0 nice value, and if you type nice xterm, the new xterm and shell will run with a nice value of 10. If you then type nice xterm in the new shell, the resulting shell will run with a nice value of 19. (It can't be 20 because the scale doesn't go that high.)

Ordinary users can use nice to reduce the priority of their processes (that is, apply positive nice values), but only root can increase the priority of jobs (that is, apply negative nice values). This rule applies relative to the priority of whatever process runs nice; for instance, if a user reduces the priority of a shell, the user cannot then restore the priority of programs launched from that shell to the default value of 0.

As a general rule, nice is best used to minimize the CPU-time demands of low-priority but CPU-intensive programs. Depending on your uses for the computer, you might run scientific or engineering simulations, ray tracers, audio manipulation tools, and so on with nice in order to minimize the impact of these programs on foreground processes, such as word processors, web browsers, and shells. Most interactive programs don't use much CPU time, but their responsiveness is important. Suppose you run a CPU-intensive program, such as a ray tracer, along with a less CPU-intensive program, such as a word processor. If you don't use nice, the ray tracer and word processor will compete for CPU cycles, and the word processor's performance will suffer, despite the fact that it uses far less CPU time than the ray tracer uses. If you run the ray tracer with a large nice value, though, the kernel will give priority to the word processor, so the word processor's performance won't be degraded much or at all by the ray tracer's CPU needs. Because the word processor needs relatively little CPU time overall, the ray tracer will still get the CPU time it needs, assuming no other CPU-intensive processes are running on the computer. Using nice to reduce the CPU time given to a CPU-intensive program will impact that program's performance if the system is running other CPU-intensive tasks, though. If your CPU load average, as revealed by top or uptime, is much above 0, you can expect use of nice to increase the run time of a CPU-intensive process.

On rare occasion, you may want to use nice to increase a process's priority (that is, give it a negative nice value). This practice is most likely to be desirable when you're launching a process that can't afford to miss the CPU time it requires. For instance, Linux audio recording tools work by sampling the output of a sound card at regular intervals. If one of these samples is missed because Linux couldn't give the recording tool CPU time at the critical moment, the resulting audio file will have audible artifacts. Such problems are most likely to occur on slower or heavily loaded systems. You may be able to improve matters by running the audio tool with a negative nice value, giving it priority over other processes. Another option is to shut down potentially competing processes.

0 0

Post a comment