Querying and setting a thread’s scheduling policy and priority
In Chapter 10, The CPU Scheduler – Part 1, in the Thread Priorities section, you learned how to query the scheduling policy and priority of any given thread via the chrt
utility (we also demonstrated a simple Bash script to do so). There, we mentioned the fact that chrt
internally invokes the sched_getattr()
system call in order to query these attributes.
Very similarly, setting the scheduling policy and priority can be performed either by using the chrt
utility (making it simple to do so within a script, for example), or programmatically within a (user space) C application with the sched_setattr()
system call. In addition, the kernel exposes other APIs: sched_{g,s}etscheduler()
and its pthread
library wrapper APIs, pthread_{g,s}etschedparam()
(as these are all user space APIs, we leave it to you to browse through their man pages to get the details and try them out for yourself).