1SCHED_SETSCHEDULER(2)      Linux Programmer's Manual     SCHED_SETSCHEDULER(2)
2
3
4

NAME

6       sched_setscheduler,  sched_getscheduler  -  set and get scheduling pol‐
7       icy/parameters
8

SYNOPSIS

10       #include <sched.h>
11
12       int sched_setscheduler(pid_t pid, int policy,
13                              const struct sched_param *param);
14       int sched_getscheduler(pid_t pid);
15

DESCRIPTION

17       The sched_setscheduler() system call sets both  the  scheduling  policy
18       and  parameters  for  the  thread whose ID is specified in pid.  If pid
19       equals zero, the scheduling policy and parameters of the calling thread
20       will be set.
21
22       The scheduling parameters are specified in the param argument, which is
23       a pointer to a structure of the following form:
24
25           struct sched_param {
26               ...
27               int sched_priority;
28               ...
29           };
30
31       In the current implementation, the structure contains only  one  field,
32       sched_priority.   The  interpretation  of param depends on the selected
33       policy.
34
35       Currently, Linux supports the following "normal" (i.e.,  non-real-time)
36       scheduling policies as values that may be specified in policy:
37
38       SCHED_OTHER   the standard round-robin time-sharing policy;
39
40       SCHED_BATCH   for "batch" style execution of processes; and
41
42       SCHED_IDLE    for running very low priority background jobs.
43
44       For each of the above policies, param->sched_priority must be 0.
45
46       Various "real-time" policies are also supported, for special time-crit‐
47       ical applications that need precise  control  over  the  way  in  which
48       runnable  threads  are selected for execution.  For the rules governing
49       when a process may use these policies,  see  sched(7).   The  real-time
50       policies that may be specified in policy are:
51
52       SCHED_FIFO    a first-in, first-out policy; and
53
54       SCHED_RR      a round-robin policy.
55
56       For  each  of  the  above  policies,  param->sched_priority specifies a
57       scheduling priority for the thread.  This is a number in the range  re‐
58       turned   by   calling  sched_get_priority_min(2)  and  sched_get_prior‐
59       ity_max(2) with the specified policy.  On Linux, these system calls re‐
60       turn, respectively, 1 and 99.
61
62       Since  Linux 2.6.32, the SCHED_RESET_ON_FORK flag can be ORed in policy
63       when calling sched_setscheduler().  As a result of including this flag,
64       children  created by fork(2) do not inherit privileged scheduling poli‐
65       cies.  See sched(7) for details.
66
67       sched_getscheduler() returns  the  current  scheduling  policy  of  the
68       thread  identified by pid.  If pid equals zero, the policy of the call‐
69       ing thread will be retrieved.
70

RETURN VALUE

72       On   success,   sched_setscheduler()   returns   zero.    On   success,
73       sched_getscheduler()  returns  the policy for the thread (a nonnegative
74       integer).  On error, both calls return -1, and errno is set to indicate
75       the error.
76

ERRORS

78       EINVAL Invalid arguments: pid is negative or param is NULL.
79
80       EINVAL (sched_setscheduler()) policy is not one of the recognized poli‐
81              cies.
82
83       EINVAL (sched_setscheduler()) param does not make sense for the  speci‐
84              fied policy.
85
86       EPERM  The calling thread does not have appropriate privileges.
87
88       ESRCH  The thread whose ID is pid could not be found.
89

CONFORMING TO

91       POSIX.1-2001,  POSIX.1-2008  (but see BUGS below).  The SCHED_BATCH and
92       SCHED_IDLE policies are Linux-specific.
93

NOTES

95       Further details of the semantics of  all  of  the  above  "normal"  and
96       "real-time"  scheduling  policies  can  be found in the sched(7) manual
97       page.  That page also describes an additional  policy,  SCHED_DEADLINE,
98       which is settable only via sched_setattr(2).
99
100       POSIX  systems  on  which sched_setscheduler() and sched_getscheduler()
101       are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
102
103       POSIX.1 does not detail the permissions that an unprivileged thread re‐
104       quires  in  order to call sched_setscheduler(), and details vary across
105       systems.  For example, the Solaris 7 manual page says that the real  or
106       effective user ID of the caller must match the real user ID or the save
107       set-user-ID of the target.
108
109       The scheduling policy and parameters are in fact per-thread  attributes
110       on Linux.  The value returned from a call to gettid(2) can be passed in
111       the argument pid.  Specifying pid as 0 will operate on  the  attributes
112       of  the  calling  thread, and passing the value returned from a call to
113       getpid(2) will operate on the attributes of  the  main  thread  of  the
114       thread  group.   (If  you  are  using  the  POSIX threads API, then use
115       pthread_setschedparam(3),         pthread_getschedparam(3),         and
116       pthread_setschedprio(3), instead of the sched_*(2) system calls.)
117

BUGS

119       POSIX.1  says  that  on success, sched_setscheduler() should return the
120       previous scheduling policy.  Linux sched_setscheduler() does  not  con‐
121       form to this requirement, since it always returns 0 on success.
122

SEE ALSO

124       chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2),
125       sched_getaffinity(2), sched_getattr(2), sched_getparam(2),
126       sched_rr_get_interval(2), sched_setaffinity(2), sched_setattr(2),
127       sched_setparam(2), sched_yield(2), setpriority(2), capabilities(7),
128       cpuset(7), sched(7)
129

COLOPHON

131       This  page  is  part of release 5.12 of the Linux man-pages project.  A
132       description of the project, information about reporting bugs,  and  the
133       latest     version     of     this    page,    can    be    found    at
134       https://www.kernel.org/doc/man-pages/.
135
136
137
138Linux                             2021-03-22             SCHED_SETSCHEDULER(2)
Impressum