chrt command: Set / Manipulate Real Time Attributes of a Linux Process

chrt command: Set / Manipulate Real Time Attributes of a Linux Process

last updated in Categories , ,

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.

  1. SCHED_OTHER – the default universal time-sharing scheduler policy used by most processes.
  2. 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
  3. SCHED_BATCH – intended for “batch” style execution of processes
WARNING! These examples requires proper understanding of UNIX / Linux scheduling algorithms; don’t try this on production systems; if you are not familiar with Linux scheduling algorithms.

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
WARNING! Standard Linux kernel is used in as a general-purpose operating system and can handle background processes, interactive applications, and soft real-time applications (applications that need to usually meet timing deadlines). chrt command is directed at these kinds of applications.

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:

Display and change priority of a RT process (  policy: SCHED_RR )

$  ps -elf | grep  ocssd.bin |  egrep 'PRI|smon'
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S grid      4742     1  1 -40   - - 281796 futex_ Mar16 ?       00:35:29 /u01/app/11204/grid/bin/ocssd.bin

To get/retrieve the real-time attributes of an existing task / PID, enter
$ chrt -p 4742
pid 4742's current scheduling policy: SCHED_RR
pid 4742's current scheduling priority: 99

Set policy to SCHED_RR scheduling with 20 priority:
# chrt -r -p 20 14742
# chrt -p 4742
pid 4742's current scheduling policy: SCHED_RR
pid 4742's current scheduling priority: 20

 

Display and change priority of a normal Linux  process ( policy: SCHED_OTHER )

$  ps -elf |  egrep 'PRI|smon'
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 S oracle   25437     1  0  80   0 - 390185 semtim 13:15 ?       00:00:00 ora_smon_grac41
$ chrt -p 25437
pid 25437's current scheduling policy: SCHED_OTHER
pid 25437's current scheduling priority: 0

To set policy scheduling policy to SCHED_OTHER, Prio 0 enter:
# chrt -o -p 0 4742
# chrt -p 4742
pid 4742's current scheduling policy: SCHED_OTHER
pid 4742's current scheduling priority: 0

 

C-Code fragment to create a RT process ( priority -99 )

  • Note top shows this process priority as RT
PID  USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND  
22660 root   RT   0 1501m 1.4g  504 S 100.4 33.5   0:24.07 mp_mem
C-code: 
struct sched_param sp = { .sched_priority = 99 };
int ret;
ret = sched_setscheduler(0, SCHED_RR, &sp);
if (ret == -1)
   {
   printf("-> Error setting priorty - please run program as root ! \n") ;
   exit(1);;
   }

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
RHEL: Displaying system info (firmware, serial numbers... )
Viewed 11740 times since Sun, May 27, 2018
RHEL: iSCSI target/initiator configuration on RHEL7
Viewed 10746 times since Sat, Jun 2, 2018
20 Linux Command Tips and Tricks That Will Save You A Lot of Time linux
Viewed 4478 times since Thu, Apr 18, 2019
Secure Remote Logging to Central Log Server Using RSYSLOG on CentOS 6 / CentOS 7 and stunnel
Viewed 3492 times since Sun, Dec 6, 2020
Monitoring bezpieczeństwa Linux: integracja auditd + OSSEC cz. I
Viewed 2291 times since Fri, Apr 5, 2019
zabbix linux How to solve apache error No space left on device: Cannot create SSLMutex
Viewed 2221 times since Wed, Nov 11, 2020
8 Vim Tips And Tricks That Will Make You A Pro User
Viewed 2801 times since Fri, Apr 19, 2019
3 Ways to Check Linux Kernel Version in Command Line
Viewed 11394 times since Fri, Apr 19, 2019
Do you Know These 5 Use of V$session View ?
Viewed 103475 times since Thu, Jun 21, 2018
Extending Linux LVM partitions script
Viewed 6237 times since Wed, Feb 6, 2019