Blame man2/sched_setattr.2

Packit 7cfc04
.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" and Copyright (C) 2014 Peter Zijlstra <peterz@infradead.org>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH SCHED_SETATTR 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
sched_setattr, sched_getattr \-
Packit 7cfc04
set and get scheduling policy and attributes
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sched.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int sched_setattr(pid_t " pid ", struct sched_attr *" attr ,
Packit 7cfc04
.BI "                  unsigned int " flags );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int sched_getattr(pid_t " pid ", struct sched_attr *" attr ,
Packit 7cfc04
.BI "                  unsigned int " size ", unsigned int " flags );
Packit 7cfc04
.fi
Packit 7cfc04
.\" FIXME . Add feature test macro requirements
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.SS sched_setattr()
Packit 7cfc04
The
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
system call sets the scheduling policy and
Packit 7cfc04
associated attributes for the thread whose ID is specified in
Packit 7cfc04
.IR pid .
Packit 7cfc04
If
Packit 7cfc04
.I pid
Packit 7cfc04
equals zero,
Packit 7cfc04
the scheduling policy and attributes of the calling thread will be set.
Packit 7cfc04
.PP
Packit 7cfc04
Currently, Linux supports the following "normal"
Packit 7cfc04
(i.e., non-real-time) scheduling policies as values that may be specified in
Packit 7cfc04
.IR policy :
Packit 7cfc04
.TP 14
Packit 7cfc04
.BR SCHED_OTHER
Packit 7cfc04
the standard round-robin time-sharing policy;
Packit 7cfc04
.\" In the 2.6 kernel sources, SCHED_OTHER is actually called
Packit 7cfc04
.\" SCHED_NORMAL.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SCHED_BATCH
Packit 7cfc04
for "batch" style execution of processes; and
Packit 7cfc04
.TP
Packit 7cfc04
.BR SCHED_IDLE
Packit 7cfc04
for running
Packit 7cfc04
.I very
Packit 7cfc04
low priority background jobs.
Packit 7cfc04
.PP
Packit 7cfc04
Various "real-time" policies are also supported,
Packit 7cfc04
for special time-critical applications that need precise control over
Packit 7cfc04
the way in which runnable threads are selected for execution.
Packit 7cfc04
For the rules governing when a process may use these policies, see
Packit 7cfc04
.BR sched (7).
Packit 7cfc04
The real-time policies that may be specified in
Packit 7cfc04
.IR policy
Packit 7cfc04
are:
Packit 7cfc04
.TP 14
Packit 7cfc04
.BR SCHED_FIFO
Packit 7cfc04
a first-in, first-out policy; and
Packit 7cfc04
.TP
Packit 7cfc04
.BR SCHED_RR
Packit 7cfc04
a round-robin policy.
Packit 7cfc04
.PP
Packit 7cfc04
Linux also provides the following policy:
Packit 7cfc04
.TP 14
Packit 7cfc04
.B SCHED_DEADLINE
Packit 7cfc04
a deadline scheduling policy; see
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
for details.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I attr
Packit 7cfc04
argument is a pointer to a structure that defines
Packit 7cfc04
the new scheduling policy and attributes for the specified thread.
Packit 7cfc04
This structure has the following form:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct sched_attr {
Packit 7cfc04
    u32 size;              /* Size of this structure */
Packit 7cfc04
    u32 sched_policy;      /* Policy (SCHED_*) */
Packit 7cfc04
    u64 sched_flags;       /* Flags */
Packit 7cfc04
    s32 sched_nice;        /* Nice value (SCHED_OTHER,
Packit 7cfc04
                              SCHED_BATCH) */
Packit 7cfc04
    u32 sched_priority;    /* Static priority (SCHED_FIFO,
Packit 7cfc04
                              SCHED_RR) */
Packit 7cfc04
    /* Remaining fields are for SCHED_DEADLINE */
Packit 7cfc04
    u64 sched_runtime;
Packit 7cfc04
    u64 sched_deadline;
Packit 7cfc04
    u64 sched_period;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.B size
Packit 7cfc04
This field should be set to the size of the structure in bytes, as in
Packit 7cfc04
.IR "sizeof(struct sched_attr)" .
Packit 7cfc04
If the provided structure is smaller than the kernel structure,
Packit 7cfc04
any additional fields are assumed to be '0'.
Packit 7cfc04
If the provided structure is larger than the kernel structure,
Packit 7cfc04
the kernel verifies that all additional fields are 0;
Packit 7cfc04
if they are not,
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR E2BIG
Packit 7cfc04
and updates
Packit 7cfc04
.I size
Packit 7cfc04
to contain the size of the kernel structure.
Packit 7cfc04
.IP
Packit 7cfc04
The above behavior when the size of the user-space
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure does not match the size of the kernel structure
Packit 7cfc04
allows for future extensibility of the interface.
Packit 7cfc04
Malformed applications that pass oversize structures
Packit 7cfc04
won't break in the future if the size of the kernel
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure is increased.
Packit 7cfc04
In the future,
Packit 7cfc04
it could also allow applications that know about a larger user-space
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure to determine whether they are running on an older kernel
Packit 7cfc04
that does not support the larger structure.
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_policy
Packit 7cfc04
This field specifies the scheduling policy, as one of the
Packit 7cfc04
.BR SCHED_*
Packit 7cfc04
values listed above.
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_flags
Packit 7cfc04
This field contains flags controlling scheduling behavior.
Packit 7cfc04
Only one such flag is currently defined:
Packit 7cfc04
.BR SCHED_FLAG_RESET_ON_FORK .
Packit 7cfc04
As a result of including this flag, children created by
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
do not inherit privileged scheduling policies.
Packit 7cfc04
See
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
for details.
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_nice
Packit 7cfc04
This field specifies the nice value to be set when specifying
Packit 7cfc04
.IR sched_policy
Packit 7cfc04
as
Packit 7cfc04
.BR SCHED_OTHER
Packit 7cfc04
or
Packit 7cfc04
.BR SCHED_BATCH .
Packit 7cfc04
The nice value is a number in the range \-20 (high priority)
Packit 7cfc04
to +19 (low priority); see
Packit 7cfc04
.BR sched (7).
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_priority
Packit 7cfc04
This field specifies the static priority to be set when specifying
Packit 7cfc04
.IR sched_policy
Packit 7cfc04
as
Packit 7cfc04
.BR SCHED_FIFO
Packit 7cfc04
or
Packit 7cfc04
.BR SCHED_RR .
Packit 7cfc04
The allowed range of priorities for these policies can be determined using
Packit 7cfc04
.BR sched_get_priority_min (2)
Packit 7cfc04
and
Packit 7cfc04
.BR sched_get_priority_max (2).
Packit 7cfc04
For other policies, this field must be specified as 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_runtime
Packit 7cfc04
This field specifies the "Runtime" parameter for deadline scheduling.
Packit 7cfc04
The value is expressed in nanoseconds.
Packit 7cfc04
This field, and the next two fields,
Packit 7cfc04
are used only for
Packit 7cfc04
.BR SCHED_DEADLINE
Packit 7cfc04
scheduling; for further details, see
Packit 7cfc04
.BR sched (7).
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_deadline
Packit 7cfc04
This field specifies the "Deadline" parameter for deadline scheduling.
Packit 7cfc04
The value is expressed in nanoseconds.
Packit 7cfc04
.TP
Packit 7cfc04
.I sched_period
Packit 7cfc04
This field specifies the "Period" parameter for deadline scheduling.
Packit 7cfc04
The value is expressed in nanoseconds.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument is provided to allow for future extensions to the interface;
Packit 7cfc04
in the current implementation it must be specified as 0.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS sched_getattr()
Packit 7cfc04
The
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
system call fetches the scheduling policy and the
Packit 7cfc04
associated attributes for the thread whose ID is specified in
Packit 7cfc04
.IR pid .
Packit 7cfc04
If
Packit 7cfc04
.I pid
Packit 7cfc04
equals zero,
Packit 7cfc04
the scheduling policy and attributes of the calling thread
Packit 7cfc04
will be retrieved.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I size
Packit 7cfc04
argument should be set to the size of the
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure as known to user space.
Packit 7cfc04
The value must be at least as large as the size of the initially published
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure, or the call fails with the error
Packit 7cfc04
.BR EINVAL .
Packit 7cfc04
.PP
Packit 7cfc04
The retrieved scheduling attributes are placed in the fields of the
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure pointed to by
Packit 7cfc04
.IR attr .
Packit 7cfc04
The kernel sets
Packit 7cfc04
.I attr.size
Packit 7cfc04
to the size of its
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure.
Packit 7cfc04
.PP
Packit 7cfc04
If the caller-provided
Packit 7cfc04
.I attr
Packit 7cfc04
buffer is larger than the kernel's
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure,
Packit 7cfc04
the additional bytes in the user-space structure are not touched.
Packit 7cfc04
If the caller-provided structure is smaller than the kernel
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure and the kernel needs to return values outside the provided space,
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR E2BIG .
Packit 7cfc04
As with
Packit 7cfc04
.BR sched_setattr (),
Packit 7cfc04
these semantics allow for future extensibility of the interface.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument is provided to allow for future extensions to the interface;
Packit 7cfc04
in the current implementation it must be specified as 0.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
and
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
return 0.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the cause of the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
and
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
can both fail for the following reasons:
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I attr
Packit 7cfc04
is NULL; or
Packit 7cfc04
.I pid
Packit 7cfc04
is negative; or
Packit 7cfc04
.I flags
Packit 7cfc04
is not zero.
Packit 7cfc04
.TP
Packit 7cfc04
.B ESRCH
Packit 7cfc04
The thread whose ID is
Packit 7cfc04
.I pid
Packit 7cfc04
could not be found.
Packit 7cfc04
.PP
Packit 7cfc04
In addition,
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
can fail for the following reasons:
Packit 7cfc04
.TP
Packit 7cfc04
.B E2BIG
Packit 7cfc04
The buffer specified by
Packit 7cfc04
.I size
Packit 7cfc04
and
Packit 7cfc04
.I attr
Packit 7cfc04
is too small.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I size
Packit 7cfc04
is invalid; that is, it is smaller than the initial version of the
Packit 7cfc04
.I sched_attr
Packit 7cfc04
structure (48 bytes) or larger than the system page size.
Packit 7cfc04
.PP
Packit 7cfc04
In addition,
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
can fail for the following reasons:
Packit 7cfc04
.TP
Packit 7cfc04
.B E2BIG
Packit 7cfc04
The buffer specified by
Packit 7cfc04
.I size
Packit 7cfc04
and
Packit 7cfc04
.I attr
Packit 7cfc04
is larger than the kernel structure,
Packit 7cfc04
and one or more of the excess bytes is nonzero.
Packit 7cfc04
.TP
Packit 7cfc04
.B EBUSY
Packit 7cfc04
.B SCHED_DEADLINE
Packit 7cfc04
admission control failure, see
Packit 7cfc04
.BR sched (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I attr.sched_policy
Packit 7cfc04
is not one of the recognized policies;
Packit 7cfc04
.I attr.sched_flags
Packit 7cfc04
contains a flag other than
Packit 7cfc04
.BR SCHED_FLAG_RESET_ON_FORK ;
Packit 7cfc04
or
Packit 7cfc04
.I attr.sched_priority
Packit 7cfc04
is invalid; or
Packit 7cfc04
.I attr.sched_policy
Packit 7cfc04
is
Packit 7cfc04
.BR SCHED_DEADLINE
Packit 7cfc04
and the deadline scheduling parameters in
Packit 7cfc04
.I attr
Packit 7cfc04
are invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The caller does not have appropriate privileges.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The CPU affinity mask of the thread specified by
Packit 7cfc04
.I pid
Packit 7cfc04
does not include all CPUs in the system
Packit 7cfc04
(see
Packit 7cfc04
.BR sched_setaffinity (2)).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
These system calls first appeared in Linux 3.14.
Packit 7cfc04
.\" FIXME . Add glibc version
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
These system calls are nonstandard Linux extensions.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.BR sched_setattr ()
Packit 7cfc04
provides a superset of the functionality of
Packit 7cfc04
.BR sched_setscheduler (2),
Packit 7cfc04
.BR sched_setparam (2),
Packit 7cfc04
.BR nice (2),
Packit 7cfc04
and (other than the ability to set the priority of all processes
Packit 7cfc04
belonging to a specified user or all processes in a specified group)
Packit 7cfc04
.BR setpriority (2).
Packit 7cfc04
Analogously,
Packit 7cfc04
.BR sched_getattr ()
Packit 7cfc04
provides a superset of the functionality of
Packit 7cfc04
.BR sched_getscheduler (2),
Packit 7cfc04
.BR sched_getparam (2),
Packit 7cfc04
and (partially)
Packit 7cfc04
.BR getpriority (2).
Packit 7cfc04
.SH BUGS
Packit 7cfc04
In Linux versions up to
Packit 7cfc04
.\" FIXME . patch sent to Peter Zijlstra
Packit 7cfc04
3.15,
Packit 7cfc04
.BR sched_settattr ()
Packit 7cfc04
failed with the error
Packit 7cfc04
.BR EFAULT
Packit 7cfc04
instead of
Packit 7cfc04
.BR E2BIG
Packit 7cfc04
for the case described in ERRORS.
Packit 7cfc04
.\" In Linux versions up to up 3.15,
Packit 7cfc04
.\" FIXME . patch from Peter Zijlstra pending
Packit 7cfc04
.\" .BR sched_setattr ()
Packit 7cfc04
.\" allowed a negative
Packit 7cfc04
.\" .I attr.sched_policy
Packit 7cfc04
.\" value.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR chrt (1),
Packit 7cfc04
.BR nice (2),
Packit 7cfc04
.BR sched_get_priority_max (2),
Packit 7cfc04
.BR sched_get_priority_min (2),
Packit 7cfc04
.BR sched_getaffinity (2),
Packit 7cfc04
.BR sched_getparam (2),
Packit 7cfc04
.BR sched_getscheduler (2),
Packit 7cfc04
.BR sched_rr_get_interval (2),
Packit 7cfc04
.BR sched_setaffinity (2),
Packit 7cfc04
.BR sched_setparam (2),
Packit 7cfc04
.BR sched_setscheduler (2),
Packit 7cfc04
.BR sched_yield (2),
Packit 7cfc04
.BR setpriority (2),
Packit 7cfc04
.BR pthread_getschedparam (3),
Packit 7cfc04
.BR pthread_setschedparam (3),
Packit 7cfc04
.BR pthread_setschedprio (3),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR cpuset (7),
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
.ad
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.