Sling Academy
Home/DevOps/Process priority in Linux: Explained with examples

Process priority in Linux: Explained with examples

Last updated: January 28, 2024

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.

Next Article: Managing volumes in Ubuntu: The ultimate guide

Previous Article: How to manage jobs in Ubuntu: A practical guide

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide