Chapter 36. Displaying the priority for a process
You can display information about the priority of a process and information about the scheduling policy for a process using the sched_getattr
attribute.
Prerequisites
- You have administrator privileges.
36.1. The chrt utility
The chrt
utility checks and adjusts scheduler policies and priorities. It can start new processes with the desired properties or change the properties of a running process.
Additional resources
-
chrt(1)
man page
36.2. Displaying the process priority using the chrt utility
You can display the current scheduling policy and scheduling priority for a specified process.
Procedure
Run the
chrt
utility with the-p
option, specifying a running process.# chrt -p 468 pid 468's current scheduling policy: SCHED_FIFO pid 468's current scheduling priority: 85 # chrt -p 476 pid 476's current scheduling policy: SCHED_OTHER pid 476's current scheduling priority: 0
36.3. Displaying the process priority using sched_getscheduler()
Real-time processes use a set of functions to control policy and priority. You can use the sched_getscheduler()
function to display the scheduler policy for a specified process.
Procedure
Create the
get_sched.c
source file and open it in a text editor.$ {EDITOR} get_sched.c
Add the following lines into the file.
#include <sched.h> #include <unistd.h> #include <stdio.h> int main() { int policy; pid_t pid = getpid(); policy = sched_getscheduler(pid); printf("Policy for pid %ld is %i.\n", (long) pid, policy); return 0; }
The
policy
variable holds the scheduler policy for the specified process.Compile the program.
$ gcc get_sched.c -o get_sched
Run the program with varying policies.
$ chrt -o 0 ./get_sched Policy for pid 27240 is 0. $ chrt -r 10 ./get_sched Policy for pid 27243 is 2. $ chrt -f 10 ./get_sched Policy for pid 27245 is 1.
Additional resources
-
sched_getscheduler(2)
man page
36.4. Displaying the valid range for a scheduler policy
You can use the sched_get_priority_min()
and sched_get_priority_max()
functions to check the valid priority range for a given scheduler policy.
Procedure
Create the
sched_get.c
source file and open it in a text editor.$ {EDITOR} sched_get.c
Enter the following into the file:
#include <stdio.h> #include <unistd.h> #include <sched.h> int main() { printf("Valid priority range for SCHED_OTHER: %d - %d\n", sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER)); printf("Valid priority range for SCHED_FIFO: %d - %d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO)); printf("Valid priority range for SCHED_RR: %d - %d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR)); return 0; }
NoteIf the specified scheduler policy is not known by the system, the function returns
-1
anderrno
is set toEINVAL
.NoteBoth
SCHED_FIFO
andSCHED_RR
can be any number within the range of1
to99
. POSIX is not guaranteed to honor this range, however, and portable programs should use these functions.- Save the file and exit the editor.
Compile the program.
$ gcc sched_get.c -o msched_get
The sched_get
program is now ready and can be run from the directory in which it is saved.
Additional resources
-
sched_get_priority_min(2)
man page -
sched_get_priority_max(2)
man page
36.5. Displaying the timeslice for a process
The SCHED_RR
(round-robin) policy differs slightly from the SCHED_FIFO
(first-in, first-out) policy. SCHED_RR
allocates concurrent processes that have the same priority in a round-robin rotation. In this way, each process is assigned a timeslice. The sched_rr_get_interval()
function reports the timeslice allocated to each process.
Though POSIX requires that this function must work only with processes that are configured to run with the SCHED_RR
scheduler policy, the sched_rr_get_interval()
function can retrieve the timeslice length of any process on Linux.
Timeslice information is returned as a timespec
. This is the number of seconds and nanoseconds since the base time of 00:00:00 GMT, 1 January 1970:
struct timespec { time_t tv_sec; /* seconds / long tv_nsec; / nanoseconds */ };
Procedure
Create the
sched_timeslice.c
source file and open it in a text editor.$ {EDITOR} sched_timeslice.c
Add the following lines to the
sched_timeslice.c
file.#include <stdio.h> #include <sched.h> int main() { struct timespec ts; int ret; /* real apps must check return values */ ret = sched_rr_get_interval(0, &ts); printf("Timeslice: %lu.%lu\n", ts.tv_sec, ts.tv_nsec); return 0; }
- Save the file and exit the editor.
Compile the program.
$ gcc sched_timeslice.c -o sched_timeslice
Run the program with varying policies and priorities.
$ chrt -o 0 ./sched_timeslice Timeslice: 0.38994072 $ chrt -r 10 ./sched_timeslice Timeslice: 0.99984800 $ chrt -f 10 ./sched_timeslice Timeslice: 0.0
Additional resources
-
nice(2)
man page -
getpriority(2)
man page -
setpriority(2)
man page
36.6. Displaying the scheduling policy and associated attributes for a process
The sched_getattr()
function queries the scheduling policy currently applied to the specified process, identified by PID. If PID equals to zero, the policy of the calling process is retrieved.
The size
argument should reflect the size of the sched_attr
structure as known to userspace. The kernel fills out sched_attr::size
to the size of its sched_attr
structure.
If the input structure is smaller, the kernel returns values outside the provided space. As a result, the system call fails with an E2BIG
error. The other sched_attr
fields are filled out as described in The sched_attr structure.
Procedure
Create the
sched_timeslice.c
source file and open it in a text editor.$ {EDITOR} sched_timeslice.c
Add the following lines to the
sched_timeslice.c
file.#define _GNU_SOURCE #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <linux/unistd.h> #include <linux/kernel.h> #include <linux/types.h> #include <sys/syscall.h> #include <pthread.h> #define gettid() syscall(__NR_gettid) #define SCHED_DEADLINE 6 /* XXX use the proper syscall numbers */ #ifdef __x86_64__ #define __NR_sched_setattr 314 #define __NR_sched_getattr 315 #endif struct sched_attr { __u32 size; __u32 sched_policy; __u64 sched_flags; /* SCHED_NORMAL, SCHED_BATCH */ __s32 sched_nice; /* SCHED_FIFO, SCHED_RR */ __u32 sched_priority; /* SCHED_DEADLINE (nsec) */ __u64 sched_runtime; __u64 sched_deadline; __u64 sched_period; }; int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int size, unsigned int flags) { return syscall(__NR_sched_getattr, pid, attr, size, flags); } int main (int argc, char **argv) { struct sched_attr attr; unsigned int flags = 0; int ret; ret = sched_getattr(0, &attr, sizeof(attr), flags); if (ret < 0) { perror("sched_getattr"); exit(-1); } printf("main thread pid=%ld\n", gettid()); printf("main thread policy=%ld\n", attr.sched_policy); printf("main thread nice=%ld\n", attr.sched_nice); printf("main thread priority=%ld\n", attr.sched_priority); printf("main thread runtime=%ld\n", attr.sched_runtime); printf("main thread deadline=%ld\n", attr.sched_deadline); printf("main thread period=%ld\n", attr.sched_period); return 0; }
Compile the
sched_timeslice.c
file.$ gcc sched_timeslice.c -o sched_timeslice
Check the output of the
sched_timeslice
program.$ ./sched_timeslice main thread pid=321716 main thread policy=6 main thread nice=0 main thread priority=0 main thread runtime=1000000 main thread deadline=9000000 main thread period=10000000
36.7. The sched_attr structure
The sched_attr
structure contains or defines a scheduling policy and its associated attributes for a specified thread. The sched_attr
structure has the following form:
struct sched_attr { u32 size; u32 sched_policy u64 sched_flags s32 sched_nice u32 sched_priority /* SCHED_DEADLINE fields */ u64 sched_runtime u64 sched_deadline u64 sched_period };
sched_attr data structure
- size
The thread size in bytes. If the size of the structure is smaller than the kernel structure, additional fields are then assumed to be
0
. If the size is larger than the kernel structure, the kernel verifies all additional fields as0
.NoteThe
sched_setattr()
function fails withE2BIG
error whensched_attr
structure is larger than the kernel structure and updates size to contain the size of the kernel structure.- sched_policy
- The scheduling policy
- sched_flags
Helps control scheduling behavior when a process forks using the
fork()
function. The calling process is referred to as the parent process, and the new process is referred to as the child process. Valid values:-
0
: The child process inherits the scheduling policy from the parent process. -
SCHED_FLAG_RESET_ON_FORK: fork()
: The child process does not inherit the scheduling policy from the parent process. Instead, it is set to the default scheduling policy(struct sched_attr){ .sched_policy = SCHED_OTHER, }
.
-
- sched_nice
-
Specifies the
nice
value to be set when usingSCHED_OTHER
orSCHED_BATCH
scheduling policies. Thenice
value is a number in a range from-20
(high priority) to+19
(low priority). - sched_priority
-
Specifies the static priority to be set when scheduling
SCHED_FIFO
orSCHED_RR
. For other policies, specify priority as0
.
SCHED_DEADLINE
fields must be specified only for deadline scheduling:
-
sched_runtime: Specifies the
runtime
parameter for deadline scheduling. The value is expressed in nanoseconds. -
sched_deadline: Specifies the
deadline
parameter for deadline scheduling. The value is expressed in nanoseconds. -
sched_period: Specifies the
period
parameter for deadline scheduling. The value is expressed in nanoseconds.