Process scheduling priorities

During a process lifetime, it may need CPU and other resources to keep executing normally. We know that multiple processes are running simultaneously in a system and they may need a CPU to complete an operation. To share the available CPUs and resources, process scheduling is done so that each process gets a chance to make use of the CPU. When a process gets created, an initial priority value is set. Depending upon the priority value, the process gets the CPU time.

The process scheduling priority range is from -20 to 19. This value is also called a nice value. The lower the nice value, the higher is the scheduling priority of a process. So, the process with -20 will have the highest scheduling priority and the process with the nice value 19 will have the lowest scheduling priority.

To see the nice value of a process, the ps or top command can be used. The corresponding nice value of a process is available in the NI column:

$ ps -l
Process scheduling priorities

In the ps output, we can see in the NI column that the nice value of bash and the ps processes is 0.

Changing scheduling priorities

Every process in a system has some priority assigned that depends upon its nice value. Based on priority, the process gets CPU time and other resources to use. Sometimes, it may happen that a process needs to be executed quickly, but it is waiting for CPU resources to be freed for long time because of a lower scheduling priority. In such cases, we may want to increase its scheduling priority in order to finish a task sooner. We can change the scheduling priority of a process by using the nice and renice commands.

Using nice

The nice command launches a process with a user-defined scheduling priority. By default, processes created by a user get the nice value 0. To verify this, run the nice command without any option:

$ nice
0

Let's create a new firefox process that actually consumes CPU and resources:

$ killall firefox  # Terminate any firefox if already running
$ firefox &    # Firefox launched in background
$ top
Using nice

We can see that the nice value of firefox is 0 and the CPU usage is 8.7%.

Now, we will kill the current firefox and launch another firefox with the nice value 10. This means, firefox will have a lower priority than other user-created processes.

To create a process with a different nice value, the -n option is used with nice:

$ killall firefox
$ nice -n 10 firefox &

OR

$ nice -10 firefox &

To see what nice value firefox has now, check the top output:

$ top
Using nice

We can see that the firefox process has the 10 nice value. To provide more scheduling priority—that is, setting a negative nice value to a process—root privilege is required.

The following example sets the firefox process as a higher scheduling priority:

$  nice -n -10 firefox

OR

$ sudo  nice --10 firefox

Using renice

The nice command can only modify a nice value during the launch of a process. However, if we want to change a running process scheduling priority, then the renice command should be used. The renice command alters the scheduling priority of one or more running processes.

The syntax of using renice is as follows:

renice [-n] priority [-g|-p|-u] identifier

Here, the -g option considers succeeding an argument—that is, identifier as GIDs.

The -p option considers succeeding an argument—that is, identifier as PIDs.

The -u option considers succeeding an argument—that is, identifier as usernames or UIDs.

If none of the options—-g, -p, or -u—are provided, identifiers are considered as PIDs.

For example, we will change the priority of all the processes belonging to a user. Firstly, see the current priority of processes owned by the user:

$  top -u skumari    # User is skumari
Using renice

Now, we will modify the priority of all processes using renice with the –u option:

$ sudo renice -n -5 -u skumari

Let's view a new nice value of processes owned by the user skumari:

$ top -u skumari
Using renice

To modify the scheduling priority of a few processes, modify using the process's PIDs. The following example modifies the process plasmashell and Firefox having the PIDs 1505 and 5969 respectively:

$ sudo renice -n 2 -p 1505 5969
$ top -u skumari
Using renice

Now, we can see that the nice values of the process plasmashell and Firefox are 2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.243.131