Q. How do I set the real time scheduling priority of a process under Linux operating system using a shell prompt?
A. You can use chrt command to set or retrieve the real-time scheduling attributes / scheduling priority of an existing PID. You can also run COMMAND with the given attributes.
Understanding Linux Scheduling Priorities
From the man page:
The scheduler is the kernel part that decides which runnable process will be executed by the CPU next. The Linux scheduler offers three different scheduling policies, one for normal processes and two for real-time applications.
- SCHED_OTHER – the default universal time-sharing scheduler policy used by most processes.
- SCHED_FIFO or SCHED_RR – intended for special time-critical applications that need precise control over the way in which runnable processes are selected for execution
- SCHED_BATCH – intended for “batch” style execution of processes
Scheduling Algorithm
- SCHED_FIFO uses First In-First Out scheduling algorithm
- SCHED_RR uses Round Robin scheduling algorithm
- SCHED_OTHER uses Default Linux time-sharing scheduling algorithm
- SCHED_BATCH use Scheduling batch processes algorithm
util-linux package
chrt command is part of util-linux package – low-level system utilities that are necessary for a Linux system to function. It is installed by default under Debian / Ubuntu / CentOS / RHEL / Fedora and almost all other Linux distributions.
How do I use chrt command to get real time attributes of a Linux process?
To get / retrieve the real-time attributes of an existing task / PID, enter:
# chrt -p pid
# chrt -p 112
# chrt -p 1
Output:
pid 1's current scheduling policy: SCHED_OTHER pid 1's current scheduling priority: 0
Any user can retrieve the scheduling information. No special privileges required.
How do I use chrt command to set real time attributes of a Linux process (already running processes)?
Use the syntax as follows to set new priority:
# chrt -p prio pid
# chrt -p 1025
# chrt -p 55 1025
# chrt -p 1025
Before setting new scheduling policy, you need to find out minimum and maximum valid priorities for each scheduling algorithm, enter:
# chrt -m
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
How do I set SCHED_BATCH scheduling policy?
To set scheduling policy to SCHED_BATCH, enter:
# chrt -b -p 0 {pid}
# chrt -b -p 0 1024
How do I set SCHED_FIFO scheduling policy?
To set scheduling policy to SCHED_FIFO, enter:
# chrt -f -p [1..99] {pid}
Set policy to SCHED_FIFO with 50 priority:
# chrt -f -p 50 1024
# chrt -p 1024
How do I set SCHED_OTHER scheduling policy?
To set policy scheduling policy to SCHED_OTHER, enter:
# chrt -o -p 0 {pid}
# chrt -o -p 0 1024
# chrt -p 1024
How do I set SCHED_RR scheduling policy?
To set scheduling policy to SCHED_RR, enter:
# chrt -r -p [1..99] {pid}
Set policy to SCHED_RR scheduling with 20 priority:
# chrt -r -p 20 1024
# chrt -p 1024
Further readings:
If you run CPU-intensive processes, you should familiarize yourself with other tools:
- nice command to run a program with modified scheduling priority
- renice command to alter priority of running processes
- taskset command to retrieve or set a processes’s CPU affinity
- man pages nice, renice, chrt, taskset, sched_setaffinity and sched_setscheduler
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Hi,
Thanks for the info it was very useful, but i had a problem where i had to set RT scheduling for processes without root privilages .
i overcame the problem by adding the following to /etc/security/limits.conf
@users – rtprio 99
if the 2.6.x kernel is built with the following option
Security options —>
[*] Enable different security models
this works with out any problem
thanks
PradeepR
I am having a problem with this setting a processes attributes, to real time. I get the message:
xxxxx@xxxxxx:/usr/hlds# chrt -f -p 98 hlds_amd
sched_setscheduler: Operation not permitted
failed to set pid 0’s policy
What would cause this?
This article is barely more useful than the man page. I was hoping for more. When would I want to use one of these over another? What are the differences betwen SCHED_RR and SCHED_FIFO? SCHED_IDLE vs SCHED_BATCH? How about an example of a task that would be a better candidate for SCHED_BATCH than SCHED_IDLE, etc? Can I make sure that a task NEVER consumes CPU resources while any other task wants CPU? How can I play highly compressed HD video smoothly while running 10 concurrent CPU-bound Matlab jobs?
This command can be used to specify which processor(s) a task must run on on a multi core system. Has anyone experimented with it in such a manner?
what is the default scheduling algorithm present in linux kernel?
SCHED_OTHER