Linux chrt Command Tutorial for Beginners (5 Examples)

Processes form the core of a running Linux system. When a program converts into a process (which is when it's executed), a lot of attributes are set. Sometimes, you may want to change these attributes at runtime. There exists a command line tool chrt that lets you do this.

In this tutorial, we will discuss this utility with some easy to understand examples. But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.

Linux chrt command

As already mentioned in the introduction section above, the chrt command lets you manipulate the real-time attributes of a process. Following is its syntax:

chrt [options] -p [prio] pid

And here's how the tool's man page explains it:

       chrt sets or retrieves the real-time scheduling attributes of an exist?
       ing pid, or runs command with the given attributes.  Both  policy  (one
       of  SCHED_OTHER,  SCHED_FIFO, SCHED_RR, SCHED_BATCH, or SCHED_IDLE) and
       priority can be set and retrieved.

       The SCHED_BATCH policy is supported since Linux 2.6.16.  The SCHED_IDLE
       policy is supported since Linux 2.6.23.

       The  SCHED_RESET_ON_FORK  flag  for policies SCHED_RR and SCHED_FIFO is
       supported since Linux 2.6.31.

Following are some Q&A-styled examples that should give you a good idea on how the chrt command works.

Q1. How to use the chrt command?

Suppose you are interested in manipulating the scheduling policy or priority of the gedit process. So the first thing you need to do is to get the process id for this process, something which you can do using the pidof command.

pidof gedit

On my system, for example, the above command gave the pid '6063' as output. Now, to retrieve the current scheduling policy and priority for the gedit process, use chrt in the following way:

chrt -p 6063

And here's the output this command produced on my system:

pid 6063's current scheduling policy: SCHED_OTHER
pid 6063's current scheduling priority: 0

Q2. How to make chrt show minimum/maximum valid priorities?

This can be done using the -m command line option.

chrt -m

For example, on our system the above command produced the following output:

SCHED_OTHER min/max priority    : 0/0
SCHED_FIFO min/max priority    : 1/99
SCHED_RR min/max priority    : 1/99
SCHED_BATCH min/max priority    : 0/0
SCHED_IDLE min/max priority    : 0/0

You will have to refer to these values for priority setting while setting scheduling policies.

Q3. How to set a scheduling policy to SCHED_BATCH?

This can be done using the -b command line option.

For example:

chrt -b -p 0 6063

Now if you check the scheduling policy for gedit again using the following command:

chrt -p 6063

You should get an output similar to the following, signifying the change you did has been applied:

pid 6063's current scheduling policy: SCHED_BATCH
pid 6063's current scheduling priority: 0

Q4. How to set a scheduling policy to SCHED_FIFO?

This can be done using the -f command line option.

chrt -f -p 20 6063

As done in the previous Q&A, you can cross verify the changes using the chrt -p command. Please note that you may require root privileges for using the -f command line option.

Q5. How to set a scheduling policy to SCHED_OTHER?

This can be done using the -o command line option.

chrt -o -p 0 6063

To verify, use the chrt -p command.

chrt -p 6063

Following is the output in my case:

pid 6063's current scheduling policy: SCHED_OTHER
pid 6063's current scheduling priority: 0

Conclusion

Agreed, the chrt command isn't the kind of tool that you may use every day. Heck, you may not even need it until you are into system management or working on some kernel scheduling related work. However, it's worth knowing at-least that such a tool exists. For more information on chrt, head to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment