xref: /netbsd-src/lib/librt/sched.3 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\"	$NetBSD: sched.3,v 1.11 2011/12/04 21:08:44 jym 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 December 4, 2011
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.Nd process scheduling
43.Sh LIBRARY
44.Lb librt
45.Sh SYNOPSIS
46.In sched.h
47.Ft int
48.Fn sched_setparam "pid_t pid" "const struct sched_param *param"
49.Ft int
50.Fn sched_getparam "pid_t pid" "struct sched_param *param"
51.Ft int
52.Fn sched_setscheduler "pid_t pid" "int policy" "const struct sched_param *param"
53.Ft int
54.Fn sched_getscheduler "pid_t pid"
55.Ft int
56.Fn sched_get_priority_max "int policy"
57.Ft int
58.Fn sched_get_priority_min "int policy"
59.Ft int
60.Fn sched_rr_get_interval "pid_t pid" "struct timespec *interval"
61.Ft int
62.Fn sched_yield "void"
63.Sh DESCRIPTION
64This section describes the functions used to get scheduling information
65about processes, and control the scheduling of processes.
66.Pp
67Available scheduling policies (classes) are:
68.Bl -tag -width SCHED_OTHER
69.It Dv SCHED_OTHER
70Time-sharing (TS) scheduling policy.
71The default policy in
72.Nx .
73.It Dv SCHED_FIFO
74First in, first out (FIFO) scheduling policy.
75.It Dv SCHED_RR
76Round robin scheduling policy.
77.El
78.Pp
79The
80.Fa struct sched_param
81contains at least one member:
82.Bl -tag -width flags
83.It Fa sched_priority
84Specifies the priority of the process.
85.El
86.Sh FUNCTIONS
87.Bl -tag -width compact
88.It Fn sched_setparam pid param
89Sets the scheduling parameters for the process specified by
90.Fa pid
91to
92.Fa param .
93If the value of
94.Fa pid
95is equal to zero, then the calling process is used.
96.It Fn sched_getparam pid param
97Gets the scheduling parameters of the process specified by
98.Fa pid
99into the structure
100.Fa param .
101If the value of
102.Fa pid
103is equal to zero, then the calling process is used.
104.It Fn sched_setscheduler pid policy param
105Set the scheduling policy and parameters for the process specified by
106.Fa pid .
107If the value of
108.Fa pid
109is equal to zero, then the calling process is used.
110.It Fn sched_getscheduler pid
111Returns the scheduling policy of the process specified by
112.Fa pid .
113If the value of
114.Fa pid
115is equal to zero, then the calling process is used.
116.It Fn sched_get_priority_max policy
117Returns the maximal priority which may be used for the scheduling policy
118specified by
119.Fa policy .
120.It Fn sched_get_priority_min policy
121Returns the minimal priority which may be used for the scheduling policy
122specified by
123.Fa policy .
124.It Fn sched_rr_get_interval pid interval
125Returns the time quantum into the structure
126.Fa interval
127of the process specified by
128.Fa pid .
129If the value of
130.Fa pid
131is equal to zero, then the calling process is used.
132The process must be running at
133.Fa SCHED_RR
134scheduling policy.
135.It Fn sched_yield
136Yields a processor voluntarily and gives other threads a chance to run
137without waiting for an involuntary preemptive switch.
138.It Fn sched_setaffinity_np pid size cpuset
139Set the affinity mask specified by
140.Fa cpuset
141for the process specified by
142.Fa pid .
143At least one valid CPU must be set in the mask.
144.It Fn sched_getaffinity_np pid size cpuset
145Get the affinity mask of the process specified by
146.Fa pid
147into the
148.Fa cpuset .
149.El
150.Sh IMPLEMENTATION NOTES
151Setting CPU
152.Xr affinity 3
153requires super-user privileges.
154Ordinary users can be allowed to control CPU affinity
155of their threads via the
156.Pa security.models.extensions.user_set_cpu_affinity
157.Xr sysctl 7 .
158See
159.Xr secmodel_extensions 9 .
160.Pp
161Portable applications should not use the
162.Fn sched_setaffinity_np
163and
164.Fn sched_getaffinity_np
165functions.
166.Sh RETURN VALUES
167.Fn sched_setparam ,
168.Fn sched_getparam ,
169.Fn sched_rr_get_interval ,
170and
171.Fn sched_yield
172return 0 on success.
173Otherwise, \-1 is returned and
174.Va errno
175is set to indicate the error.
176.Pp
177.Fn sched_setscheduler
178returns the previously used scheduling policy on success.
179Otherwise, \-1 is returned and
180.Va errno
181is set to indicate the error.
182.Pp
183.Fn sched_getscheduler
184returns the scheduling policy on success.
185Otherwise, \-1 is returned and
186.Va errno
187is set to indicate the error.
188.Pp
189.Fn sched_get_priority_max
190and
191.Fn sched_get_priority_min
192return the maximal/minimal priority value on success.
193Otherwise, \-1 is returned and
194.Va errno
195is set to indicate the error.
196.Pp
197.Fn sched_setaffinity_np
198and
199.Fn sched_getaffinity_np
200return 0 on success.
201Otherwise, \-1 is returned and
202.Va errno
203is set to indicate the error.
204.Sh ERRORS
205The
206.Fn sched_setparam
207and
208.Fn sched_setscheduler
209functions fail if:
210.Bl -tag -width Er
211.It Bq Er EINVAL
212At least one of the specified scheduling parameters was invalid.
213.It Bq Er EPERM
214The calling process has no appropriate privileges to perform the operation.
215.It Bq Er ESRCH
216No process can be found corresponding to the PID specified by
217.Fa pid ,
218and the value of
219.Fa pid
220is not zero.
221.El
222.Pp
223The
224.Fn sched_getparam
225and
226.Fn sched_getscheduler
227functions fail if:
228.Bl -tag -width Er
229.It Bq Er EPERM
230The calling process is not a super-user and its effective user id does not
231match the effective user-id of the specified process.
232.It Bq Er ESRCH
233No process can be found corresponding to that specified by
234.Fa pid ,
235and the value of
236.Fa pid
237is not zero.
238.El
239.Pp
240The
241.Fn sched_get_priority_max
242and
243.Fn sched_get_priority_min
244functions fail if:
245.Bl -tag -width Er
246.It Bq Er EINVAL
247The specified scheduling policy is invalid.
248.El
249.Pp
250The
251.Fn sched_rr_get_interval
252function fails if:
253.Bl -tag -width Er
254.It Bq Er ESRCH
255No process can be found corresponding to that specified by
256.Fa pid ,
257and the value of
258.Fa pid
259is not zero.
260.El
261.Sh SEE ALSO
262.Xr affinity 3 ,
263.Xr cpuset 3 ,
264.Xr pset 3 ,
265.Xr schedctl 8
266.Sh STANDARDS
267These functions, except
268.Fn sched_setaffinity_np
269and
270.Fn sched_getaffinity_np ,
271are expected to conform the
272.St -p1003.1-2001
273standard.
274.Sh HISTORY
275The scheduling functions appeared in
276.Nx 5.0 .
277