summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/getpriority.3c
blob: 66eb6c99a83e858489074b4a3120126a502f98e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The Berkeley software License Agreement specifies the terms and conditions
.\" for redistribution.
.\"
.\"
.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.
.\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved.
.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH GETPRIORITY 3C "Apr 1, 2008"
.SH NAME
getpriority, setpriority \- get and set the nice value
.SH SYNOPSIS
.LP
.nf
#include <sys/resource.h>

\fBint\fR \fBgetpriority\fR(\fBint\fR \fIwhich\fR, \fBid_t\fR \fIwho\fR);
.fi

.LP
.nf
\fBint\fR \fBsetpriority\fR(\fBint\fR \fIwhich\fR, \fBid_t\fR \fIwho\fR, \fBint\fR \fIvalue\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBgetpriority()\fR function obtains the nice value of a process, thread,
or set of processes.  The \fBsetpriority()\fR function sets the nice value of a
process, thread, or set of processes to \fIvalue\fR+\fBNZERO\fR, where
\fBNZERO\fR is defined to be 20.
.sp
.LP
Target entities are specified by the values of the \fIwhich\fR and \fIwho\fR
arguments.  The \fIwhich\fR argument can be one of the following values:
\fBPRIO_PROCESS\fR, \fBPRIO_PGRP\fR, \fBPRIO_USER\fR, \fBPRIO_GROUP\fR,
\fBPRIO_SESSION\fR, \fBPRIO_LWP\fR, \fBPRIO_TASK\fR, \fBPRIO_PROJECT\fR,
\fBPRIO_ZONE\fR, or \fBPRIO_CONTRACT\fR, indicating that the \fIwho\fR argument
is to be interpreted as a process ID, a process group ID, an effective user ID,
an effective group ID, a session ID, a thread (lwp) ID, a task ID, a project
ID, a zone ID, or a process contract ID, respectively.  A 0 value for the
\fIwho\fR argument specifies the current process, process group, or user. A 0
value for the \fIwho\fR argument is treated as valid group ID, session ID,
thread (lwp) ID, task ID, project ID, zone ID, or process contract ID. A
\fBP_MYID\fR value for the \fIwho\fR argument can be used to specify the
current group, session, thread, task, project, zone, or process contract,
respectively.
.sp
.LP
If a specified process is multi-threaded, the nice value set with
\fBsetpriority()\fR affects all threads in the process.
.sp
.LP
If more than one process is specified, \fBgetpriority()\fR returns \fBNZERO\fR
less than the lowest nice value pertaining to any of the specified entities,
and \fBsetpriority()\fR sets the nice values of all of the specified processes
to \fIvalue\fR+\fBNZERO\fR.
.sp
.LP
The default nice value is \fBNZERO\fR. Lower nice values cause more favorable
scheduling. The range of valid nice values is 0 to \fBNZERO\fR*2-1. If
\fIvalue\fR+\fBNZERO\fR is less than the system's lowest supported nice value,
\fBsetpriority()\fR sets the nice value to the lowest supported value. If
\fIvalue\fR+\fBNZERO\fR is greater than the system's highest supported nice
value, \fBsetpriority()\fR sets the nice value to the highest supported value.
.sp
.LP
Only a process with appropriate privileges can lower the nice value.
.sp
.LP
Any process or thread using \fBSCHED_FIFO\fR or \fBSCHED_RR\fR is unaffected by
a call to \fBsetpriority()\fR. This is not considered an error. A process or
thread that subsequently reverts to \fBSCHED_OTHER\fR will not have its
priority affected by such a \fBsetpriority()\fR call.
.sp
.LP
The effect of changing the nice value varies depending on the scheduling policy
in effect.
.sp
.LP
Since \fBgetpriority()\fR can return the value -1 on successful completion, it
is necessary to set \fBerrno\fR to 0 prior to a call to \fBgetpriority()\fR. If
\fBgetpriority()\fR returns the value -1, then \fBerrno\fR can be checked to
see if an error occurred or if the value is a legitimate nice value.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBgetpriority()\fR returns an integer in the range
from -\fBNZERO\fR to \fBNZERO\fR-1.  Otherwise, \fB\(mi1\fR is returned and
\fBerrno\fR is set to indicate the error.
.sp
.LP
Upon successful completion, \fBsetpriority()\fR returns \fB0\fR. Otherwise,
\fB\(mi1\fR is returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
.sp
.LP
The \fBgetpriority()\fR and \fBsetpriority()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBESRCH\fR\fR
.ad
.RS 10n
No process or thread could be located using the \fIwhich\fR and \fIwho\fR
argument values specified.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The value of the \fIwhich\fR argument was not recognized, or the value of the
\fIwho\fR argument is not a valid process ID, process group ID, user ID, group
ID, session ID, thread (lwp) ID, task ID, project ID, or zone ID.
.RE

.sp
.LP
In addition, \fBsetpriority()\fR may fail if:
.sp
.ne 2
.na
\fB\fBEPERM\fR\fR
.ad
.RS 10n
A process was located, but neither the real nor effective user ID of the
executing process match the effective user ID of the process whose nice value
is being changed.
.RE

.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 10n
A request was made to change the nice value to a lower numeric value and the
current process does not have appropriate privileges.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fRExample using \fBgetpriority()\fR
.sp
.LP
The following example returns the current scheduling priority for the process
ID returned by the call to \fBgetpid\fR(2).

.sp
.in +2
.nf
#include <sys/resource.h>
\&...
int which = PRIO_PROCESS;
id_t pid;
int ret;

pid = getpid();
ret = getpriority(which, pid);
.fi
.in -2

.LP
\fBExample 2 \fRExample using \fBsetpriority()\fR
.sp
.LP
The following example sets the nice value for the current process to 0.

.sp
.in +2
.nf
#include <sys/resource.h>
\&...
int which = PRIO_PROCESS;
id_t pid;
int value = -20;
int ret;

pid = getpid();
ret = setpriority(which, pid, value);
.fi
.in -2

.SH USAGE
.sp
.LP
The \fBgetpriority()\fR and \fBsetpriority()\fR functions work with an offset
nice value (\fIvalue\fR-\fBNZERO\fR). The nice value is in the range 0 to
2*\fBNZERO\fR-1, while the return value for \fBgetpriority()\fR and the third
parameter for \fBsetpriority()\fR are in the range -\fBNZERO\fR to
\fBNZERO\fR-1.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
Standard	See \fBstandards\fR(7).
.TE

.SH SEE ALSO
.sp
.LP
.BR nice (1),
.BR renice (1),
.BR sched_get_priority_max (3C),
.BR sched_setscheduler (3C),
.BR attributes (7),
.BR standards (7)