103.6 Modify Process Execution Priorities

LPIC-1 Exam Objective 103.6: Modify Process Execution Priorities #

Weight: 2 #

Description #

Candidates should be able to manage process execution priorities.

Key Knowledge Areas: #

  • Know the default priority of a job that is created.
  • Run a program with higher or lower priority than the default.
  • Change the priority of a running process.

Files, Terms, and Utilities: #

  • nice
  • ps
  • renice
  • top

Introduction #

In Linux, process execution priorities determine how much CPU time is allocated to each process. Understanding how to manage these priorities is crucial for optimizing system performance and ensuring that critical tasks receive the necessary resources. This tutorial will cover how to modify process execution priorities using tools such as nice, renice, ps, and top in both Ubuntu/Debian and Enterprise Linux distributions like Red Hat Enterprise Linux (RHEL) and CentOS.

Default Priority of a Job #

When a process is created, it is assigned a default priority, which is typically 0. The priority range in Linux goes from -20 (highest priority) to 19 (lowest priority).

Checking Default Priority #

You can check the priority of running processes using the ps command:

ps -eo pid,ni,cmd | grep <process_name>

Here, ni represents the nice value of the process.

Running a Program with Higher or Lower Priority #

The nice command is used to start a process with a specified priority.

Syntax #

nice -n <priority> <command>

Example #

Starting a process with a lower priority:

nice -n 10 sleep 1000 &

This command starts the sleep process with a nice value of 10.

Changing the Priority of a Running Process #

The renice command is used to change the priority of an already running process.

Syntax #

renice <priority> -p <pid>

Example #

Changing the priority of a process with PID 1234 to -5:

sudo renice -5 -p 1234

Using top to Monitor and Change Priorities #

The top command is a powerful utility for monitoring system processes and their resource usage. It also allows you to change the priority of running processes.

Steps: #

  1. Run top by simply typing:
    top
    
  2. Find the process you want to change.
  3. Press r and enter the PID of the process.
  4. Enter the new priority value.

Practical Examples #

Ubuntu/Debian Example #

  1. Checking Current Priority:

    ps -eo pid,ni,cmd | grep apache2
    
  2. Running a Program with Lower Priority:

    nice -n 10 /usr/bin/myapp &
    
  3. Changing the Priority of a Running Process:

    sudo renice -5 -p 1234
    
  4. Using top to Change Priority:

    • Run top
    • Find the process (e.g., myapp)
    • Press r and enter the PID
    • Set the new priority (e.g., -10)

Enterprise Linux (RHEL/CentOS) Example #

  1. Checking Current Priority:

    ps -eo pid,ni,cmd | grep httpd
    
  2. Running a Program with Lower Priority:

    nice -n 15 /usr/bin/myapp &
    
  3. Changing the Priority of a Running Process:

    sudo renice -10 -p 5678
    
  4. Using top to Change Priority:

    • Run top
    • Find the process (e.g., myapp)
    • Press r and enter the PID
    • Set the new priority (e.g., 5)

Conclusion #

Managing process execution priorities is a crucial skill for Linux system administrators. By using commands like nice, renice, ps, and top, you can ensure that critical processes receive the necessary resources and maintain optimal system performance. This knowledge is essential for the LPIC-1 certification and practical system administration tasks.

Further Reading #