November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Process priority with nice

Process priority with nice

Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time. Typically you’ll have one person using a desktop system running any number of applications or many users using many applications on a server.

The amount of time devoted to tasks largely depends on how intensive the task is. Some tasks require higher priority than others; for instance, if you were compiling a large software package you didn’t need immediately, that priority should probably be lower than your Web browser or e-mail client.

Each process has a niceness value associated with it, which is what the kernel uses to determine which processes require more processor time than others. The higher the nice value, the lower the priority of the process. In other words, the “nicer” the program, the less CPU it will try to take from other processes; programs that are less nice tend to demand more CPU time than other programs that are nicer.

The priority is noted by a range of -20 (the highest) to 20 (the lowest). Using ps, you can see the current nice value of all programs:

$ ps axl
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     1     0  16   0   2648   112 -      S    ?          0:01 init [3]
1     0     2     1  34  19      0     0 ksofti SN   ?          0:02 [ksoftirqd/0]
5     0     3     1  10  -5      0     0 worker S<   ?          0:00 [events/0]
...

You can see that init has a nice value of 0, while other kernel tasks associated with PID 2 and 3 have a nice value of 19 and -5 respectively.

Typically, a program inherits its nice value from its parent; this prevents low priority processes from spawning high priority children. Having said that, you can use the nice command (as root or via sudo) with the command you wish to execute in order to alter its nice value. Here is a short illustration:

# ps axl | grep axl | grep -v grep
4     0 30819 30623  15   0   4660   772 -      R+   pts/0      0:00 ps axl
# nice -10 ps axl | grep axl | grep -v grep
4     0 30822 30623  30  10   4660   772 -      RN+  pts/0      0:00 ps axl

You can see there that the nice value, represented by column six, has been altered. You can also use the renice command to alter running processes. In the following example, vim was started to edit the file foo and began with a default nice value of 0. Using renice, we can change its priority:

# ps axl | grep vim | grep -v grep
0     0 30832 30623  16   0  15840  3140 -      S+   pts/0      0:00 vim foo
# renice -5 30832
30832: old priority 0, new priority -5
# ps axl | grep vim | grep -v grep
0     0 30832 30623  15  -5  15840  3140 -      S<+  pts/0      0:00 vim foo

Here, we have adjusted the priority of vim, giving it a slightly higher priority. Renice operates on the process ID, so using grep, we determined that vim is process ID 30832 and saw that the nice value was 0. After executing renice, the nice value is now -5.

Standard caveats apply: Only root can alter the nice priority of programs. So if you find that your compilation is taking too much CPU from other activities, consider renicing the parent process via root. Subsequent children should have a better nice value, or you can even start the compilation (or any other activity) with nice, specifying an appropriate nice value. You can also use renice to renice all programs belonging to a process group or user name/ID.

 

 

If you wish to run a command which typically uses a lot of CPU (for example, running tar on a large file), then you probably don’t want to bog down your whole system with it. Linux systems provide the nice command to control your process priority at runtime, or renice to change the priority of an already running process. The full manpage has help, but the command if very easy to use:

1
$ nice -n prioritylevel /command/to/run

The priority level runs from -20 (top priority) to 19 (lowest). For example, to run tar and gzip at a the lowest priority level:

1
$ nice -n 19 tar -czvf file.tar.gz bigfiletocompress

similarly, if you have a process running, use ps to find the process ID, and then use renice to change it’s priority level:

1
$ renice -n 19 -p 987 32

1 comment to Process priority with nice

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>