Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

How to Change Process Priority using Linux Nice and Renice

Every running process in Unix has a priority assigned to it.
You can change the process priority using nice and renice utility. Nice command will launch a process with an user defined scheduling priority. Renice command will modify the scheduling priority of a running process.
Linux Kernel schedules the process and allocates CPU time accordingly for each of them. But, when one of your process requires higher priority to get more CPU time, you can use nice and renice command as explained in this tutorial.

The process scheduling priority range is from -20 to 19. We call this as nice value.
A nice value of -20 represents highest priority, and a nice value of 19 represent least priority for a process.
By default when a process starts, it gets the default priority of 0.

1. Display Nice Value of a Process

The current priority of a process can be displayed using ps command.
The “NI” column in the ps command output indicates the current nice value (i.e priority) of a process.
We’ll launch a test program called test.pl which will be used to demonstrate nice and renice command. This test program will do certain tasks, and will be running for a while.
$ perl test.pl
If you execute ps command as shown below, you can notice that this test.pl program has the default nice value of 0 (look at the NI column in the following output).
$ ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 6884  6424 99  80   0 -  1556 -      13:45 pts/3    00:05:54 perl test.pl

2. Launch a Program with Less Priority

Instead of launching the program with the default priority, you can use nice command to launch the process with a specific priority.
In this example, test.pl is launched with a nice value of 10.
$ nice -10 perl test.pl
Note: Remember that -10 in the above command sets the priority of a process to 10. The – in nice command stands for the hypen, which we use to pass options to the command.
So, to pass nice value of 5, you’ll say -5. To pass nice value of 6, you’ll say -6.
As you see below, this program is now launched with a nice value of 10, which means this will run at a lower priority when compared to other programs that are launched by default.
$ ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 7044  6424 99  90  10 -  1556 -      13:58 pts/3    00:00:03 perl test.pl

3. Launch a Program with High Priority

You can also launch a program with a higher priority. Negative nice value will increase the priority a the process. So, the value has to be specified with a — (two hyphens) in front of the nice command as shown below.
# nice --10 perl test.pl
So, to pass nice value of -5, you’ll add two hyphens in front of 5. To pass nice value of -6, you’ll add two hyphens in front of 6.
As you see below, this program is now launched with a nice value of -10, which means this will run at a higher priority when compared to other programs that are launched by default.
# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -10 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl
Note: Regular users are not allowed to launch a program with a higher priority. Only root user is allowed to launch a program with high priority.
As a regular user, if you increase the priority, you’ll get the following error message from nice command.
$ nice --10 perl test.pl
nice: cannot set niceness: Permission denied
Note that after printing the above error message, the program would still continue to run with the default priority (i.e : 0).

4. Change the Priority with option -n

The process priority can be adjusted with the help of -n option.
Increase the priority:
# nice -n -5 perl test.pl
Decrease the priority:
# nice -n 5 perl test.pl

5. Change the Priority of a Running Process

The priority of an already running process can be changed using renice command.
In this example, the program test.pl is already running with a nice value of -10.
# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -10 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl
We can change the nice value of the above program to -19 as shown below. Pass the process id of the above program to -p option.
# renice -n -19 -p 3534
Verify that the nice value got changed to -19.
# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -19 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl

6. Change the Priority of All Processes that Belongs to a Group

Using -g option you can modify the priority of all processes that belongs to a group. The following command will change the nice value of all the process that belongs to nijam to 5.
# renice -n 5 -g nijam

7. Change the Priority of All Processes Owned by User

Renice allows to alter the priority of all the processes owned by a specific users as shown below.
# renice -n 5 -u bala
The above command will change the priority of all the processes owned by user bala. It will assign a nice value of 5 to all the processes that belongs to user bala.
# ps -fl -C "perl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 2720  2607 99  85   5 -  1556 -      14:34 pts/2    00:05:07 perl test.pl
0 R bala 2795  2661 99  85   5 -  1556 -      14:39 pts/3    00:00:09 perl 2.pl

Comments

Popular posts from this blog

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

7 Steps to configure BDR replication in postgresql

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

Ora2PG - Oracle/MySQL to Postgres DB migration Version 20.0

PostgreSQL Introduction