Chapter 31. 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.
31.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.
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.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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
$ {EDITOR} get_sched.c
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines into the file.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
policy
variable holds the scheduler policy for the specified process.Compile the program.
gcc get_sched.c -o get_sched
$ gcc get_sched.c -o get_sched
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the program with varying policies.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
31.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
$ {EDITOR} sched_get.c
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following into the file:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ gcc sched_get.c -o msched_get
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The sched_get
program is now ready and can be run from the directory in which it is saved.
31.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 */ }
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
$ {EDITOR} sched_timeslice.c
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
sched_timeslice.c
file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the file and exit the editor.
Compile the program.
gcc sched_timeslice.c -o sched_timeslice
$ gcc sched_timeslice.c -o sched_timeslice
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the program with varying policies and priorities.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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
$ {EDITOR} sched_timeslice.c
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
sched_timeslice.c
file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile the
sched_timeslice.c
file.gcc sched_timeslice.c -o sched_timeslice
$ gcc sched_timeslice.c -o sched_timeslice
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the output of the
sched_timeslice
program.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
31.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:
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.