Chapter 37. 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.
37.1. The chrt utility Copy linkLink copied to clipboard!
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.
37.2. Displaying the process priority using the chrt utility Copy linkLink copied to clipboard!
You can display the current scheduling policy and scheduling priority for a specified process.
Procedure
Run the
chrtutility with the-poption, specifying a running process.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
37.3. Displaying the process priority using sched_getscheduler() Copy linkLink copied to clipboard!
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.csource file and open it in a text editor.{EDITOR} get_sched.c$ {EDITOR} get_sched.cCopy 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
policyvariable holds the scheduler policy for the specified process.Compile the program.
gcc get_sched.c -o get_sched
$ gcc get_sched.c -o get_schedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the program with varying policies.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
37.4. Displaying the valid range for a scheduler policy Copy linkLink copied to clipboard!
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.csource file and open it in a text editor.{EDITOR} sched_get.c$ {EDITOR} sched_get.cCopy 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
-1anderrnois set toEINVAL.NoteBoth
SCHED_FIFOandSCHED_RRcan be any number within the range of1to99. 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_getCopy 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.
37.5. Displaying the timeslice for a process Copy linkLink copied to clipboard!
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.csource file and open it in a text editor.{EDITOR} sched_timeslice.c$ {EDITOR} sched_timeslice.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
sched_timeslice.cfile.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_timesliceCopy 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
37.6. Displaying the scheduling policy and associated attributes for a process Copy linkLink copied to clipboard!
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.csource file and open it in a text editor.{EDITOR} sched_timeslice.c$ {EDITOR} sched_timeslice.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
sched_timeslice.cfile.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile the
sched_timeslice.cfile.gcc sched_timeslice.c -o sched_timeslice
$ gcc sched_timeslice.c -o sched_timesliceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check the output of the
sched_timesliceprogram.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
37.7. The sched_attr structure Copy linkLink copied to clipboard!
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 withE2BIGerror whensched_attrstructure 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
nicevalue to be set when usingSCHED_OTHERorSCHED_BATCHscheduling policies. Thenicevalue 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_FIFOorSCHED_RR. For other policies, specify priority as0.
SCHED_DEADLINE fields must be specified only for deadline scheduling:
-
sched_runtime: Specifies the
runtimeparameter for deadline scheduling. The value is expressed in nanoseconds. -
sched_deadline: Specifies the
deadlineparameter for deadline scheduling. The value is expressed in nanoseconds. -
sched_period: Specifies the
periodparameter for deadline scheduling. The value is expressed in nanoseconds.