Oct 21, 2023 4 min read

How to Use Linux chrt Command?

Use the linux chrt command with our step-by-step tutorial. It allows you to change the scheduling policy and priority of a running process.

Use Linux chrt Command
Table of Contents

Introduction

Before we begin talking about how to use Linux chrt command, let's briefly understand-What is chrt command ?

The chrt command in Linux allows you to change the scheduling policy and priority of a running process. This can be useful in scenarios where you need to control the execution behavior of a process.

This tutorial will explain how to use the chrt command effectively. We will also address a few FAQs on how to use Linux chrt command.

How to Use Linux chrt Command?

The syntax of the chrt command, which is written and explained below, determines how it functions.

Syntax

chrt [options] -p [priority] pid

The following components are found in the chrt syntax:

  • chrt: Indicates the chrt Linux command.
  • options: Displays the arguments or options that the chrt command supports.
  • -p [priority]: accepts integer values that fall within the specified maximum or lowest permissible priority range.
  • pid: The "Process Id" of the process that is now active.

To view the options list that is provided by the chrt command, use the "chrt -help" command.

chrt --help

To view the entire list, scroll down to the "help" page. Three categories—"Policy options," "Scheduling options," and "Other options"—are used to categorize the aforementioned option list.

Additionally, the chrt supports the following six scheduling algorithm options:

Algorithms Description
SCHED_BATCH Works with the "Batch processes" algorithm.
SCHED_DEADLINE Based on the scheduling algorithm's "deadline" having negative priorities.
SCHED_FIFO Based on the algorithm "First In_First Out" The "Batch-Systems" typically use this policy, which is a real-time procedure. There is only one queue available, and it holds the processes in the exact order that they arrive.
SCHED_IDLE Utilizes low priority background input/output jobs.
SCHED_OTHER The default scheduling algorithm for Linux "time-sharing" is used by policy.
SCHED_RR Utilizes the "Round Robin" scheduling algorithm.

Now, let's look at some practical examples to help you grasp Linux chrt command.

Example 1: Display a Process's Current Scheduling Policy

To obtain the exact "process id" of the active process, first use the "pidof" command. This command is demonstrated in practice below:

pidof -s systemd

Run "chrt" using the process id of the active "systemd" process:

chrt -p 2045

The output indicates that the "systemd" scheduling process currently in use is "SCHED_OTHER". Additionally, it shows the scheduling priority in this scenario, which is "0".

Example 2: Check the Priorities of Scheduling Policies

To obtain the maximum and minimum range list of priority for all scheduling algorithms, use the "-m" option of the chrt command:

chrt -m

Example 3: Change a process's scheduling policy to "SCHED_IDLE"

As seen in the screenshot, the process gedit is currently active and working on the scheduling method "SCHED_OTHER" with priority "0":

pidof gedit

Use the chrt command in the following manner to switch the gedit scheduling policy from "SCHED_OTHER" to "SCHED_IDLE" with priority "0":

chrt -i -p 0 3900

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

First, get the scheduling algorithm that is currently in use for the installed version of Firefox, which has the PID "7093" and is working on the "SCHED_OTHER":

pidof -s firefox
chrt -p 7093

Input the chrt command with the -f flag as follows:

sudo chrt -f -p 20 7093

Priority "0" has been replaced with "20" and the "firefox" scheduling algorithm has been modified from "SCHED_OTHER" to "SCHED_FIFO".

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

The -r flag of the Linux chrt command can be used to set the "SCHED_RR" algorithm for a given running process. Use the pidof for "calculator" for the practical implementation:

sudo chrt -r -p 12 4709

The above-mentioned command successfully switched the scheduling algorithm for the "calculator" to "SCHED_RR" with priority "12".

That's all there is to the chrt command on Linux.

FAQs to Use Linux chrt Command

How can I use chrt to change the scheduling policy and priority of a process? 

The basic syntax of the chrt command is chrt [options] priority command. For example, chrt --fifo 99 my_process will run my_process with a FIFO scheduling policy and a priority of 99.

What are the available scheduling policies in chrt? 

chrt supports different scheduling policies, including FIFO (first-in, first-out), RR (round-robin), and OTHER (default).

How can I specify the scheduling policy using chrt? 

You can use options like --fifo, --rr, or --other with the chrt command to specify the desired scheduling policy.

What are the valid priority values for chrt? 

The valid priority values typically range between 1 and 99, where a lower value represents a higher priority.

How can I check the scheduling policy and priority of a process? 

You can use the chrt -p pid command to display the current scheduling policy and priority of a process identified by its PID.

Can I change the scheduling policy and priority of an already running process? 

Yes, you can use the chrt -p priority -policy policy_name pid command to modify the scheduling policy and priority of a running process.

Are there any risks in changing the scheduling policy and priority of a process? 

Changing the scheduling policy and priority of a process can have unintended consequences and may affect system performance. It is crucial to understand the implications before making any modifications.

Conclusion

The "chrt" command can be used on Linux to dynamically modify the scheduling "attributes" of a given process. The user can additionally modify the scheduling algorithm's "priority" to fall within the range of acceptable priorities that is offered. The goal and usage of the "chrt" command have been briefly described in this tutorial with the aid of practical examples.

If you have any queries, please mention below in the comments section.....

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.