Process priority in Linux: Explained with examples

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction to Process Priority in Linux

Linux operating systems are well-known for their multitasking capabilities, running multiple processes simultaneously. To manage these processes effectively, Linux uses a scheduling system that assigns priorities to each process. In this guide, we’ll dive into the concept of process priority and demonstrate how to manipulate it using examples.

Understanding Linux Process Priorities

Every process in Linux has an associated ‘nice’ value, which influences its priority in the process scheduling queue. The ‘nice’ value ranges from -20 (highest priority) to 19 (lowest priority). The kernel uses this nice value along with other factors to determine a process’s scheduling priority.

Viewing Process Priority

To view the nice values of running processes, you can use the top or ps command:

$ top
$ ps -el | head -1; ps -el | grep 'your-process-name'

Now, let’s explore how we can change the priority of a running process.

Changing Priority with the ‘nice’ Command

You can start a new process with a specific nice value using the nice command.

$ nice -n 10 command_to_run

Modifying Priority with ‘renice’

To change the nice value of an existing process:

$ sudo renice 5 -p PID

Replace ‘PID’ with the process ID of the target process.

Checking the Effect

After adjusting the priority, verify it using:

$ ps -l -p PID

Advanced Priority Management

In Linux, the scheduler also considers ‘real-time’ priorities. The chrt command allows control over real-time attributes.

Working with ‘chrt’

$ sudo chrt -r 99 command_to_run
$ sudo chrt -p PID

This will launch or modify a process with real-time attributes.

Batch Processes and ‘ionice’

Linux accommodates I/O-intensive tasks via the ionice command, specifying I/O scheduling class and priority.

Using ‘ionice’

$ ionice -c 3 -p PID
$ ionice -c 2 -n 7 command_to_run

The first command sets a process to idle priority and the second starts a new process with best-effort class and lower-priority level.

Analyzing Priority Impact

Assess the priority impact by monitoring system performance and process execution time against different nice values:

$ time command_to_run

Compare the real time for different nice values to determine impact.

Priority Inheritance

Discuss how child processes inherit the nice value of their parents and how to set a different nice value for child processes if needed:

$ nice -n 5 bash -c 'nice -n 10 child_process_command &'

In this example, the child process will have a nice value of 10, despite the parent’s value of 5.

Priority and Multicore Systems

How priority and nice values work in multicore systems, and the use of taskset for affining processes to specific cores:

$ taskset -c 2,3 command_to_run

This will run the process only on cores 2 and 3.

Conclusion

Understanding and manipulating process priority in Linux can optimize system performance and responsiveness. The tools explained provide granular control over process scheduling, allowing advanced users to tune their system according to the tasks’ importance and resource demands.