1.\" $NetBSD: sched.3,v 1.14 2016/07/06 15:55:02 wiz Exp $ 2.\" 3.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd July 5, 2016 31.Dt SCHED 3 32.Os 33.Sh NAME 34.Nm sched_setparam , 35.Nm sched_getparam , 36.Nm sched_setscheduler , 37.Nm sched_getscheduler , 38.Nm sched_get_priority_max , 39.Nm sched_get_priority_min , 40.Nm sched_rr_get_interval , 41.Nm sched_yield , 42.Nm sched_protect 43.Nd process scheduling 44.Sh LIBRARY 45.Lb librt 46.Sh SYNOPSIS 47.In sched.h 48.Ft int 49.Fn sched_setparam "pid_t pid" "const struct sched_param *param" 50.Ft int 51.Fn sched_getparam "pid_t pid" "struct sched_param *param" 52.Ft int 53.Fn sched_setscheduler "pid_t pid" "int policy" "const struct sched_param *param" 54.Ft int 55.Fn sched_getscheduler "pid_t pid" 56.Ft int 57.Fn sched_get_priority_max "int policy" 58.Ft int 59.Fn sched_get_priority_min "int policy" 60.Ft int 61.Fn sched_rr_get_interval "pid_t pid" "struct timespec *interval" 62.Ft int 63.Fn sched_yield "void" 64.Ft int 65.Fn sched_protect "int priority" 66.Sh DESCRIPTION 67This section describes the functions used to get scheduling information 68about processes, and control the scheduling of processes. 69.Pp 70Available scheduling policies (classes) are: 71.Bl -tag -width SCHED_OTHER 72.It Dv SCHED_OTHER 73Time-sharing (TS) scheduling policy. 74The default policy in 75.Nx . 76.It Dv SCHED_FIFO 77First in, first out (FIFO) scheduling policy. 78.It Dv SCHED_RR 79Round robin scheduling policy. 80.El 81.Pp 82The 83.Fa struct sched_param 84contains at least one member: 85.Bl -tag -width flags 86.It Fa sched_priority 87Specifies the priority of the process. 88For 89.Dv SCHED_OTHER , 90must be 91.Dv PRI_NONE ; 92the kernel will dynamically assign priorities to the thread based on 93CPU load. 94.El 95.Sh FUNCTIONS 96.Bl -tag -width compact 97.It Fn sched_setparam pid param 98Sets the scheduling parameters for the process specified by 99.Fa pid 100to 101.Fa param . 102If the value of 103.Fa pid 104is equal to zero, then the calling process is used. 105.It Fn sched_getparam pid param 106Gets the scheduling parameters of the process specified by 107.Fa pid 108into the structure 109.Fa param . 110If the value of 111.Fa pid 112is equal to zero, then the calling process is used. 113.It Fn sched_setscheduler pid policy param 114Set the scheduling policy and parameters for the process specified by 115.Fa pid . 116If the value of 117.Fa pid 118is equal to zero, then the calling process is used. 119.It Fn sched_getscheduler pid 120Returns the scheduling policy of the process specified by 121.Fa pid . 122If the value of 123.Fa pid 124is equal to zero, then the calling process is used. 125.It Fn sched_get_priority_max policy 126Returns the maximal priority which may be used for the scheduling policy 127specified by 128.Fa policy . 129.It Fn sched_get_priority_min policy 130Returns the minimal priority which may be used for the scheduling policy 131specified by 132.Fa policy . 133.It Fn sched_rr_get_interval pid interval 134Returns the time quantum into the structure 135.Fa interval 136of the process specified by 137.Fa pid . 138If the value of 139.Fa pid 140is equal to zero, then the calling process is used. 141The process must be running at 142.Fa SCHED_RR 143scheduling policy. 144.It Fn sched_yield 145Yields a processor voluntarily and gives other threads a chance to run 146without waiting for an involuntary preemptive switch. 147.It Fn sched_setaffinity_np pid size cpuset 148Set the affinity mask specified by 149.Fa cpuset 150for the process specified by 151.Fa pid . 152At least one valid CPU must be set in the mask. 153.It Fn sched_getaffinity_np pid size cpuset 154Get the affinity mask of the process specified by 155.Fa pid 156into the 157.Fa cpuset . 158.It Fn sched_protect priority 159Performs priority protection for 160.Dv PTHREAD_PRIO_PROTECT 161protocol. 162This function will increase the priority of the caller thread to 163.Fa priority . 164.El 165.Sh IMPLEMENTATION NOTES 166Setting CPU 167.Xr affinity 3 168requires super-user privileges. 169Ordinary users can be allowed to control CPU affinity 170of their threads via the 171.Pa security.models.extensions.user_set_cpu_affinity 172.Xr sysctl 7 . 173See 174.Xr secmodel_extensions 9 . 175.Pp 176Portable applications should not use the 177.Fn sched_setaffinity_np 178and 179.Fn sched_getaffinity_np 180functions. 181.Sh RETURN VALUES 182.Fn sched_protect , 183.Fn sched_setparam , 184.Fn sched_getparam , 185.Fn sched_rr_get_interval , 186and 187.Fn sched_yield 188return 0 on success. 189Otherwise, \-1 is returned and 190.Va errno 191is set to indicate the error. 192.Pp 193.Fn sched_setscheduler 194returns the previously used scheduling policy on success. 195Otherwise, \-1 is returned and 196.Va errno 197is set to indicate the error. 198.Pp 199.Fn sched_getscheduler 200returns the scheduling policy on success. 201Otherwise, \-1 is returned and 202.Va errno 203is set to indicate the error. 204.Pp 205.Fn sched_get_priority_max 206and 207.Fn sched_get_priority_min 208return the maximal/minimal priority value on success. 209Otherwise, \-1 is returned and 210.Va errno 211is set to indicate the error. 212.Pp 213.Fn sched_setaffinity_np 214and 215.Fn sched_getaffinity_np 216return 0 on success. 217Otherwise, \-1 is returned and 218.Va errno 219is set to indicate the error. 220.Sh ERRORS 221The 222.Fn sched_setparam 223and 224.Fn sched_setscheduler 225functions fail if: 226.Bl -tag -width Er 227.It Bq Er EINVAL 228At least one of the specified scheduling parameters was invalid. 229.It Bq Er EPERM 230The calling process has no appropriate privileges to perform the operation. 231.It Bq Er ESRCH 232No process can be found corresponding to the PID specified by 233.Fa pid , 234and the value of 235.Fa pid 236is not zero. 237.El 238.Pp 239The 240.Fn sched_getparam 241and 242.Fn sched_getscheduler 243functions fail if: 244.Bl -tag -width Er 245.It Bq Er EPERM 246The calling process is not a super-user and its effective user id does not 247match the effective user-id of the specified process. 248.It Bq Er ESRCH 249No process can be found corresponding to that specified by 250.Fa pid , 251and the value of 252.Fa pid 253is not zero. 254.El 255.Pp 256The 257.Fn sched_get_priority_max 258and 259.Fn sched_get_priority_min 260functions fail if: 261.Bl -tag -width Er 262.It Bq Er EINVAL 263The specified scheduling policy is invalid. 264.El 265.Pp 266The 267.Fn sched_rr_get_interval 268function fails if: 269.Bl -tag -width Er 270.It Bq Er ESRCH 271No process can be found corresponding to that specified by 272.Fa pid , 273and the value of 274.Fa pid 275is not zero. 276.El 277.Sh SEE ALSO 278.Xr affinity 3 , 279.Xr cpuset 3 , 280.Xr pset 3 , 281.Xr schedctl 8 282.Sh STANDARDS 283These functions, except 284.Fn sched_setaffinity_np 285and 286.Fn sched_getaffinity_np , 287are expected to conform the 288.St -p1003.1-2001 289standard. 290.Sh HISTORY 291The scheduling functions appeared in 292.Nx 5.0 . 293