Managing Process In Linux Part 3 – How To Manage Process Priority

UNDERSTANDING THE SUBJECT MATTER

Having understood part 1 and part 2 of managing processes in Linux, it is high time we learned what process priority and niceness are and how to manage them.

What Is Linux Process Priority & Niceness?

Every process in Linux has a process ID as well as a process priority, i.e, the amount of system resources allocated to a process.

Every process, when they are started are assigned the same process priority. i.e, they run with almost the same amount of system resources except in a few cases.

However, the priority of a process can be changed, for example, if a process is such that it will take time to complete, you can nice such process. i.e, give it a higher priority than other processes by assigning a higher amount of system resources to the process.

More so, if a process is slow or taking time to complete, it could be a job as well, you can also re-nice the process. i.e, assign a higher amount of system resources to the process.

Nicing and re-nicing a process, in other words, setting and resetting/changing the priority of a process is simply giving more CPU time to a process than the other and it can either be positive or negative.

One can change the priority of a process by assigning a higher amount of system resources or processing power to the process or reducing the amount of system resources from the process, this will depend on the nice or renice value that will be used to change the process priority.

An example of how this is being done is seen in the “ACTION TIME” section.


How To Nice and Renice A Process

There are 140 priorities but we won’t delve into that for now not to go out the scope. Changing the priority of a process depends on the nice value you give to the process. nice value ranges from -20 (highest priority) to +19 (lowest priority)

If you use a negative nice value, more processing power will be assigned to the process. On the contrary, if you use a positive nice value, processing power will be reduced from the process.

Furthermore, to nice a process, the best practice is not to do it aggressively. For example, if you want to change a process priority from 20 to 9, it is best to change the nice value bit by bit and not do it all at once except if the difference in the number is not wide.

Having understood the concept of priority and niceness, changing the priority of a process in Linux or rather, the niceness of a process can be done with the

  • top command/interface
  • command line interface

To renice a process, i.e change the nice value of a process that is already running, using the top interface, use the command,

top

Please see examples in “ACTION TIME” section

To renice a process using the CLI, use the command,

 renice -n <nice-value> <PID> 

Please see examples in the “ACTION TIME” section.

To nice a process, i.e, set the nice value of a process that has not been running already, use the command,

nice -n <nice-value> <command>


What Is Nice Value Of A Process

It is very important to understand nice values. The nice value of a process by default is zero (0), and it can be changed between the range of -20 (highest priority) to +19 (lowest priority), which means that if a nice value of a process is lower, for example, -20, -19, -15, -7, -1, it will have a higher priority.


ACTION TIME

How To Check The Priority & Nice Value Of A Linux Process

using the ps command, do

ps -eo pid,ppid,pri,ni,comm

where “pid” is the proccess ID, “ppid” is the parent process ID, “pri” is the priority, “ni” is the nice value and “comm” is the command.

[root@lab02 ~]# ps -eo pid,ppid,pri,ni,comm

   PID   PPID PRI  NI COMMAND
     1      0  19   0 systemd
     2      0  19   0 kthreadd
     3      2  39 -20 rcu_gp
     4      2  39 -20 rcu_par_gp
     6      2  39 -20 kworker/0:0H-kblockd
     8      2  39 -20 mm_percpu_wq
     9      2  19   0 ksoftirqd/0
    10      2  19   0 rcu_sched
    11      2 139   - migration/0
    12      2 139   - watchdog/0
    13      2  19   0 cpuhp/0
    14      2  19   0 cpuhp/1
    15      2 139   - watchdog/1
    16      2 139   - migration/1
    17      2  19   0 ksoftirqd/1
    19      2  39 -20 kworker/1:0H-kblockd
    21      2  19   0 kdevtmpfs

For example, to check the priority & nice value of the PID, 15338, use the command,

[root@lab02 ~]# ps -eo pid,ppid,pri,ni,comm |grep 15338

 15338  14447  19   0 dd
[root@lab02 ~]#

Using the top command, do

[root@lab02 ~]# top

N:B: The top command will only show the top processes that are using more higher processing resources

process priority

How To Change The Priority (Renice A Linux Process With Examples)

Using The top Command/Interface

  • To renice and change the priority of the PID, 5237, do the following steps

enter the top command,

[root@lab02 ~]# top

enter the key, r which means renice, you will see the “PID to renice” information

Enter the PID, 5237 in my case

Enter the nice value

tap the enter key

you will see that the priority value, “PR” has changed to 13 and the CPU power for the process has increased to 46.1%.

Using The Command Line,

To change the priority of a process using the command line, use the command,

renice -n <nice-value> <PID>

To renice and change the priority of the PID, 77184, and give it a nice value of -7, do the following.

  • verify the current priority and CPU utilization
[root@lab02 ~]# ps -eo pid,ppid,pri,ni,comm |grep 77184

 77184  76680  19   0 dd
[root@lab02 ~]#
[root@lab02 ~]# ps -p 77184 -o %cpu,%mem
%CPU %MEM
18.6  0.0
[root@lab02 ~]#

  • change the priority and give the process a nice value of “-7”
[root@lab02 ~]# renice -n -7 77184

77184 (process ID) old priority 0, new priority -7
[root@lab02 ~]#

  • verify again.
[root@lab02 ~]# ps -eo pid,ppid,pri,ni,comm |grep 77184

 77184  76680  26  -7 dd
[root@lab02 ~]#
[root@lab02 ~]# ps -p 77184 -o %cpu,%mem

%CPU %MEM
42.4  0.0
[root@lab02 ~]#

How To renice A Process Using The Username,

Use the command,

[root@Linuxserver ~]# renice -n -7 -u nginx

984 (user ID) old priority -5, new priority -7
[root@Linuxserver ~]#

What Is The Difference Between Using the top interface and The Command Line (Don’t Get Confused)

You would notice that when we changed the priority of the process 5237 using the top command, with a nice value of -7, the priority reduced to 13 when we verified with the top interface.

However, if you had verified using the CLI, the priority will be added.

This can be confirmed when we changed the priority of the process 77184 using the cli, with a nice value of -7, the priority increased when we verified with the cli.

More so, if you verify with the top command/interface, you will see that the priority decreased as shown in the screen shot below.

Verifying the priority with either using top or CLI can get you confused if you are a newbie, but please don’t.

The most important thing is understanding the nice values as it was explained in details in the “UNDERSTANDING THE SUBJECT MATTER” section above.

How To Set The Priority (Nice A Linux Process With Examples)

To nice a process, i.e, set the nice value of a process that has not been running already on the system, for example, ” dd if=/dev/zero of=/dev/null”, use the command

[root@lab02 ~]# nice -n 11 dd if=/dev/zero of=/dev/null
[root@lab02 ~]# nice -n 11 dd if=/dev/zero of=/dev/null &

[13] 88996
[root@lab02 ~]#

NB: The shell job in the command above will run in background as explained in one of the articles on this site

RHCSA, Q&A On How To Manage Process Priority In Linux

Your feedback is welcomed. If you love others, you will share with others.

Be the first to comment

Leave a Reply

Your email address will not be published.


*