How to Use Linux chrt Command?

The “chrt” command changes the process scheduling attributes at runtime. The scheduling attributes are divided into six types. In addition, the “chrt” command line tool is also beneficial for changing the running process priorities simultaneously within the specified range.

This post explains the use of the Linux “chrt” command. The outcomes of this guide are as follows:

Let’s start with the syntax of the “chrt” command line tool.

How to Use Linux chrt Command?

The functionality of the “chrt” command is based on its syntax, which is written and described below.

Syntax

$ chrt [options] -p [priority] pid

The “chrt” syntax contains the following components:

  • chrt: Represents the “chrt” Linux command.
  • options: Shows the options or arguments supported by the “chrt” command.
  • -p [priority]: Accepts an integer value within the defined range of maximum or minimum valid priorities.
  • pid: Stands for “Process id” of the running process.

Use the “chrt –help” command to check the options list that is supported by the “chrt” command with the description:

$ chrt --help

Scroll down the “help” page to get the complete list. The above option list is divided into three categories, i.e., “Policy options”, “Scheduling options”, and “Other options”.

Moreover, the “chrt” supports the six scheduling algorithms options that are defined below:

AlgorithmsDescription
SCHED_BATCHWorks on the “Batch processes” algorithm.
SCHED_DEADLINEBased on the “deadline” scheduling algorithm having negative priorities.
SCHED_FIFOBase on the “First In_First Out” algorithm. This policy is a real-time process and is generally used on the “Batch-Systems”. It provides only one queue that holds the process in the same order as they come.
SCHED_IDLEUtilizes the input/output background jobs having low priority.
SCHED_OTHERPolicy uses the Linux “time-sharing” default scheduling algorithm.
SCHED_RRRelies on the “Round Robin” scheduling algorithm.

Now, move on to the practical examples for understanding the Linux “chrt” command.

Example 1: Display the Current Scheduling Policy of a Process

First, run the “pidof” command to get the specific “process id” of the running process. The practical demonstration of this command is shown below:

$ pidof -s systemd

Run the “chrt” command with the process id of “systemd” running process:

$ chrt -p 2045

The output shows that the current “systemd” uses the “SCHED_OTHER” scheduling process. It also displays the scheduling priority, “0”, in this case.

Example 2: Check the Priorities of Scheduling Policies

The “chrt” command offers the “-m” option to get the maximum and minimum range list of priorities of all the scheduling algorithms:

$ chrt -m

Example 3: Set the Scdeuling Policy to “SCHED_IDLE”of a Process

The current running process “gedit” is working on the “SCHED_OTHER” scheduling algorithm having priority “0” as shown in the screenshot:

$ pidof gedit

To change the “gedit” scheduling policy from “SCHED_OTHER” to “SCHED_IDLE” with priority “0” use the “chrt” command in the following way:

$ chrt -i -p 0 3900

Example 4: Set the Scdeuling Policy to “SCHED_FIFO” of a Process

First, retrieve the current scheduling algorithm of the default browser “firefox” that is in running state having specific PID “7093” working on the “SCHED_OTHER”:

$ pidof -s firefox
$ chrt -p 7093

Type the “chrt” command with “-f” flag in the following way:

$ sudo chrt -f -p 20 7093

The “firefox” scheduling algorithm has been changed from “SCHED_OTHER” to “SCHED_FIFO” along with priority “0” to “20”.

Example 5: Set the Scdeuling Policy to “SCHED_RR” of a Process

The Linux chrt offers the “-r” flag for setting a specified running process’s “SCHED_RR” algorithm. For the practical implementation, use the pidof of “calculator”:

$ sudo chrt -r -p 12 4709

The above command successfully changed the “calculator” scheduling algorithm to  “SCHED_RR” with priority “12”.

That’s all about the Linux “chrt” command.

Conclusion

In Linux, the “chrt” command is used to dynamically change the scheduling “attributes” of a specified process. The user can also change the scheduling algorithm’s “priority” into the provided valid priorities range. This post has briefly illustrated the objective and usage of the “chrt” command with the help of practical examples.