23.3.4 Extensible Scheduling

The type struct sched_attr and the functions sched_setattr and sched_getattr are used to implement scheduling policies with multiple parameters (not just priority and niceness).

It is expected that these interfaces will be compatible with all future scheduling policies.

For additional information about scheduling policies, consult consult the manual pages https://man7.org/linux/man-pages/man7/sched.7.html and https://man7.org/linux/man-pages/man2/sched_setattr.2.html. See Linux (The Linux Kernel).

Note: Calling the sched_setattr function is incompatible with support for PTHREAD_PRIO_PROTECT mutexes.

Data Type: struct sched_attr

The sched_attr structure describes a parameterized scheduling policy.

Portability note: In the future, additional fields can be added to struct sched_attr at the end, so that the size of this data type changes. Do not use it in places where this matters, such as structure fields in installed header files, where such a change could impact the application binary interface (ABI).

The following generic fields are available.

size

The actually used size of the data structure. See the description of the functions sched_setattr and sched_getattr below how this field is used to support extension of struct sched_attr with more fields.

sched_policy

The scheduling policy. This field determines which fields in the structure are used, and how the sched_flags field is interpreted.

sched_flags

Scheduling flags associated with the scheduling policy.

In addition to the generic fields, policy-specific fields are available. For additional information, consult the manual page https://man7.org/linux/man-pages/man2/sched_setattr.2.html. See Linux (The Linux Kernel).

Function: int sched_setaddr (pid_t tid, struct sched_attr *attr, unsigned int flags)

| MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This functions applies the scheduling policy described by *attr to the thread tid (the value zero denotes the current thread).

It is recommended to initialize unused fields to zero, either using memset, or using a structure initializer. The attr->size field should be set to sizeof (struct sched_attr), to inform the kernel of the structure version in use.

The flags argument must be zero. Other values may become available in the future.

On failure, sched_setattr returns -1 and sets errno. The following errors are related the way extensibility is handled.

E2BIG

A field in *attr has a non-zero value, but is unknown to the kernel. The application could try to apply a modified policy, where more fields are zero.

EINVAL

The policy in attr->sched_policy is unknown to the kernel, or flags are set in attr->sched_flags that the kernel does not know how to interpret. The application could try with fewer flags set, or a different scheduling policy.

This error also occurs if attr is NULL or flags is not zero.

EPERM

The current thread is not sufficiently privileged to assign the policy, either because access to the policy is restricted in general, or because the current thread does not have the rights to change the scheduling policy of the thread tid.

Other error codes depend on the scheduling policy.

Function: int sched_getaddr (pid_t tid, struct sched_attr *attr, unsigned int size, unsigned int flags)

| MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function obtains the scheduling policy of the thread tid (zero denotes the current thread) and store it in *attr, which must have space for at least size bytes.

The flags argument must be zero. Other values may become available in the future.

Upon success, attr->size contains the size of the structure version used by the kernel. Fields with offsets greater or equal to attr->size are not updated by the kernel. To obtain predictable values for unknown fields, use memset to set all size bytes to zero prior to calling sched_getattr.

On failure, sched_getattr returns -1 and sets errno. If errno is E2BIG, this means that the buffer is not large large enough, and the application could retry with a larger buffer.