Linux process – nice values

In Linux every process has a nice number, the higher the nice number, the less cpu time will be used by the process.  So a process is nicer (demand less cpu time) if it has an higher nice number.

If we are using ps -l  then we have column with the header NI, in this column we see the nice number for the processes.
For example:


[bob@localhost ~]$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S  1022  2407  2402  0  80   0 - 29130 wait   pts/0    00:00:00 bash
0 R  1022  5402  2407  0  80   0 - 30319 -      pts/0    00:00:00 ps

As we can see in this list we have 2 process bash and ps, and both of them as a nice number 0.

The nice number are ranged from -20 to 19.
By default when process start with a nice number of 0 (zero).
As we can see:


$ sleep 200 &
[1] 31213
oren@oren-computer:~/workspace
$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  7090  7083  0  80   0 -  6387 wait   pts/17   00:00:00 bash
0 S  1000 31213  7090  0  80   0 -  1824 hrtime pts/17   00:00:00 sleep
4 R  1000 31214  7090  0  80   0 -  7230 -      pts/17   00:00:00 ps

we start the sleep process, and it has a nice value of 0.

However we can use the nice command to start process with a different nice value, as a regular user, you can not start process with a negative nice value, but you can have value larger than 0.
The structure of the nice is the following:
nice -n <nice-value> <command to run> 
And it will run the command and give the process a nice value of <nice-value>.
For example, we can start the sleep command with a nice value of 10.


$ nice -n 10 sleep 200 &
[2] 31254
oren@oren-computer:~/workspace
$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 7090 7083 0 80 0 - 6387 wait pts/17 00:00:00 bash
0 S 1000 31213 7090 0 80 0 - 1824 hrtime pts/17 00:00:00 sleep
0 S 1000 31254 7090 0 90 10 - 1824 hrtime pts/17 00:00:00 sleep
4 R 1000 31255 7090 0 80 0 - 7230 - pts/17 00:00:00 ps

As we can the second sleep, that was started with the nice command, has a nice value of 10.

We can use renice to change nice value of running process, as regular user, we can only increase the nice value, but root user can decrease the nice value.
The structure of the renice command is the following:
renice -n <nice-value> <process-id>
For example, we will run sleep command with a nice value of 5 and then use renice to change the value to 10:


[bob@localhost ~]$ nice -n 5 sleep 300 &
[1] 2437
[bob@localhost ~]$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 1022 2399 2394 0 80 0 - 29130 wait pts/0 00:00:00 bash
0 S 1022 2437 2399 0 85 5 - 26973 hrtime pts/0 00:00:00 sleep
0 R 1022 2438 2399 0 80 0 - 30319 - pts/0 00:00:00 ps
[bob@localhost ~]$ renice -n 10 2437
2437 (process ID) old priority 5, new priority 10
[bob@localhost ~]$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 1022 2399 2394 0 80 0 - 29130 wait pts/0 00:00:00 bash
0 S 1022 2437 2399 0 90 10 - 26973 hrtime pts/0 00:00:00 sleep
0 R 1022 2441 2399 0 80 0 - 30319 - pts/0 00:00:00 ps

So we can see that in the sleep had nice value of 5, but after it had been changed with renice, it had nice value of 10

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s