Skip to content

process priorities

(Written by Paul Cobbaut, https://github.com/paulcobbaut/)

priority and nice values

introduction

All processes have a priority and a nice value. Higher priority processes will get more cpu time than lower priority processes. You can influence this with the nice and renice commands.

pipes (mkfifo)

Processes can communicate with each other via pipes. These pipes can be created with the mkfifo command.

The screenshots shows the creation of four distinct pipes (in a new directory).

student@linux:~$ mkdir procs
student@linux:~$ cd procs/
student@linux:~/procs$ mkfifo pipe33a pipe33b pipe42a pipe42b
student@linux:~/procs$ ls -l
total 0
prw-r--r-- 1 paul paul 0 2010-04-12 13:21 pipe33a
prw-r--r-- 1 paul paul 0 2010-04-12 13:21 pipe33b
prw-r--r-- 1 paul paul 0 2010-04-12 13:21 pipe42a
prw-r--r-- 1 paul paul 0 2010-04-12 13:21 pipe42b
student@linux:~/procs$

some fun with cat

To demonstrate the use of the top and renice commands we will make the cat command use the previously created pipes to generate a full load on the cpu.

The cat is copied with a distinct name to the current directory. (This enables us to easily recognize the processes within top. You could do the same exercise without copying the cat command, but using different users. Or you could just look at the pid of each process.)

student@linux:~/procs$ cp /bin/cat proj33
student@linux:~/procs$ cp /bin/cat proj42
student@linux:~/procs$ echo -n x | ./proj33 - pipe33a > pipe33b &
[1] 1670
student@linux:~/procs$ ./proj33 <pipe33b >pipe33a &
[2] 1671
student@linux:~/procs$ echo -n z | ./proj42 - pipe42a > pipe42b &
[3] 1673
student@linux:~/procs$ ./proj42 <pipe42b >pipe42a &
[4] 1674

The commands you see above will create two proj33 processes that use cat to bounce the x character between pipe33a and pipe33b. And ditto for the z character and proj42.

top

Just running top without options or arguments will display all processes and an overview of innformation. The top of the top screen might look something like this.

top - 13:59:29 up 48 min,  4 users,  load average: 1.06, 0.25, 0.14
Tasks: 139 total,   3 running, 136 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us, 99.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem:    509352k total,   460040k used,    49312k free,    66752k buffers
Swap:   746980k total,        0k used,   746980k free,   247324k cached

Notice the cpu idle time (0.0%id) is zero. This is because our cat processes are consuming the whole cpu. Results can vary on systems with four or more cpu cores.

top -p

The top -p 1670,1671,1673,1674 screenshot below shows four processes, all of then using approximately 25 percent of the cpu.

student@linux:~$ top -p 1670,1671,1673,1674

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1674 paul      20   0  2972  616  524 S 26.6  0.1   0:11.92 proj42
 1670 paul      20   0  2972  616  524 R 25.0  0.1   0:23.16 proj33
 1671 paul      20   0  2972  616  524 S 24.6  0.1   0:23.07 proj33
 1673 paul      20   0  2972  620  524 R 23.0  0.1   0:11.48 proj42

All four processes have an equal priority (PR), and are battling for cpu time. On some systems the Linux kernel might attribute slightly varying priority values, but the result will still be four processes fighting for cpu time.

renice

Since the processes are already running, we need to use the renice command to change their nice value (NI).

The screenshot shows how to use renice on both the proj33 processes.

student@linux:~$ renice +8 1670
1670: old priority 0, new priority 8
student@linux:~$ renice +8 1671
1671: old priority 0, new priority 8

Normal users can attribute a nice value from zero to 20 to processes they own. Only the root user can use negative nice values. Be very careful with negative nice values, since they can make it impossible to use the keyboard or ssh to a system.

impact of nice values

The impact of a nice value on running processes can vary. The screenshot below shows the result of our renice +8 command. Look at the %CPU values.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1674 paul      20   0  2972  616  524 S 46.6  0.1   0:22.37 proj42
 1673 paul      20   0  2972  620  524 R 42.6  0.1   0:21.65 proj42
 1671 paul      28   8  2972  616  524 S  5.7  0.1   0:29.65 proj33
 1670 paul      28   8  2972  616  524 R  4.7  0.1   0:29.82 proj33

Important to remember is to always make less important processes nice to more important processes. Using negative nice values can have a severe impact on a system\'s usability.

nice

The nice works identical to the renice but it is used when starting a command.

The screenshot shows how to start a script with a nice value of five.

student@linux:~$ nice -5 ./backup.sh

practice : process priorities

1. Create a new directory and create six pipes in that directory.

2. Bounce a character between two pipes.

3. Use top and ps to display information (pid, ppid, priority, nice value, ...) about these two cat processes.

4. Bounce another character between two other pipes, but this time start the commands nice. Verify that all cat processes are battling for the cpu. (Feel free to fire up two more cats with the remaining pipes).

5. Use ps to verify that the two new cat processes have a nice value. Use the -o and -C options of ps for this.

6. Use renice te increase the nice value from 10 to 15. Notice the difference with the usual commands.

solution : process priorities

1. Create a new directory and create six pipes in that directory.

[student@linux ~]$ mkdir pipes ; cd pipes
[student@linux pipes]$ mkfifo p1 p2 p3 p4 p5 p6
[student@linux pipes]$ ls -l
total 0
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p1
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p2
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p3
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p4
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p5
prw-rw-r-- 1 paul paul 0 Apr 12 22:15 p6

2. Bounce a character between two pipes.

[student@linux pipes]$ echo -n x | cat - p1 > p2 &
[1] 4013
[student@linux pipes]$ cat <p2 >p1 &
[2] 4016

3. Use top and ps to display information (pid, ppid, priority, nice value, ...) about these two cat processes.

top (probably the top two lines)

[student@linux pipes]$ ps -C cat
  PID TTY          TIME CMD
 4013 pts/0    00:03:38 cat
 4016 pts/0    00:01:07 cat

[student@linux pipes]$ ps fax | grep cat
 4013 pts/0    R      4:00  |           \_ cat - p1
 4016 pts/0    S      1:13  |           \_ cat
 4044 pts/0    S+     0:00  |           \_ grep cat

4. Bounce another character between two other pipes, but this time start the commands nice. Verify that all cat processes are battling for the cpu. (Feel free to fire up two more cats with the remaining pipes).

echo -n y | nice cat - p3 > p4 &
nice cat <p4 >p3 &

5. Use ps to verify that the two new cat processes have a nice value. Use the -o and -C options of ps for this.

[student@linux pipes]$ ps -C cat -o pid,ppid,pri,ni,comm
  PID  PPID PRI  NI COMMAND
 4013  3947  14   0 cat
 4016  3947  21   0 cat
 4025  3947  13  10 cat
 4026  3947  13  10 cat

6. Use renice te increase the nice value from 10 to 15. Notice the difference with the usual commands.

[student@linux pipes]$ renice +15 4025
4025: old priority 10, new priority 15
[student@linux pipes]$ renice +15 4026
4026: old priority 10, new priority 15

[student@linux pipes]$ ps -C cat -o pid,ppid,pri,ni,comm
  PID  PPID PRI  NI COMMAND
 4013  3947  14   0 cat
 4016  3947  21   0 cat
 4025  3947   9  15 cat
 4026  3947   8  15 cat