1GETPRIORITY(3P) POSIX Programmer's Manual GETPRIORITY(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 getpriority, setpriority - get and set the nice value
13
15 #include <sys/resource.h>
16
17 int getpriority(int which, id_t who);
18 int setpriority(int which, id_t who, int value);
19
20
22 The getpriority() function shall obtain the nice value of a process,
23 process group, or user. The setpriority() function shall set the nice
24 value of a process, process group, or user to value+ {NZERO}.
25
26 Target processes are specified by the values of the which and who argu‐
27 ments. The which argument may be one of the following values:
28 PRIO_PROCESS, PRIO_PGRP, or PRIO_USER, indicating that the who argument
29 is to be interpreted as a process ID, a process group ID, or an effec‐
30 tive user ID, respectively. A 0 value for the who argument specifies
31 the current process, process group, or user.
32
33 The nice value set with setpriority() shall be applied to the process.
34 If the process is multi-threaded, the nice value shall affect all sys‐
35 tem scope threads in the process.
36
37 If more than one process is specified, getpriority() shall return value
38 {NZERO} less than the lowest nice value pertaining to any of the speci‐
39 fied processes, and setpriority() shall set the nice values of all of
40 the specified processes to value+ {NZERO}.
41
42 The default nice value is {NZERO}; lower nice values shall cause more
43 favorable scheduling. While the range of valid nice values is
44 [0,{NZERO}*2-1], implementations may enforce more restrictive limits.
45 If value+ {NZERO} is less than the system's lowest supported nice
46 value, setpriority() shall set the nice value to the lowest supported
47 value; if value+ {NZERO} is greater than the system's highest supported
48 nice value, setpriority() shall set the nice value to the highest sup‐
49 ported value.
50
51 Only a process with appropriate privileges can lower its nice value.
52
53 Any processes or threads using SCHED_FIFO or SCHED_RR shall be unaf‐
54 fected by a call to setpriority(). This is not considered an error. A
55 process which subsequently reverts to SCHED_OTHER need not have its
56 priority affected by such a setpriority() call.
57
58 The effect of changing the nice value may vary depending on the
59 process-scheduling algorithm in effect.
60
61 Since getpriority() can return the value -1 on successful completion,
62 it is necessary to set errno to 0 prior to a call to getpriority(). If
63 getpriority() returns the value -1, then errno can be checked to see if
64 an error occurred or if the value is a legitimate nice value.
65
67 Upon successful completion, getpriority() shall return an integer in
68 the range -{NZERO} to {NZERO}-1. Otherwise, -1 shall be returned and
69 errno set to indicate the error.
70
71 Upon successful completion, setpriority() shall return 0; otherwise, -1
72 shall be returned and errno set to indicate the error.
73
75 The getpriority() and setpriority() functions shall fail if:
76
77 ESRCH No process could be located using the which and who argument
78 values specified.
79
80 EINVAL The value of the which argument was not recognized, or the value
81 of the who argument is not a valid process ID, process group ID,
82 or user ID.
83
84
85 In addition, setpriority() may fail if:
86
87 EPERM A process was located, but neither the real nor effective user
88 ID of the executing process match the effective user ID of the
89 process whose nice value is being changed.
90
91 EACCES A request was made to change the nice value to a lower numeric
92 value and the current process does not have appropriate privi‐
93 leges.
94
95
96 The following sections are informative.
97
99 Using getpriority()
100 The following example returns the current scheduling priority for the
101 process ID returned by the call to getpid().
102
103
104 #include <sys/resource.h>
105 ...
106 int which = PRIO_PROCESS;
107 id_t pid;
108 int ret;
109
110
111 pid = getpid();
112 ret = getpriority(which, pid);
113
114 Using setpriority()
115 The following example sets the priority for the current process ID to
116 -20.
117
118
119 #include <sys/resource.h>
120 ...
121 int which = PRIO_PROCESS;
122 id_t pid;
123 int priority = -20;
124 int ret;
125
126
127 pid = getpid();
128 ret = setpriority(which, pid, priority);
129
131 The getpriority() and setpriority() functions work with an offset nice
132 value (nice value -{NZERO}). The nice value is in the range
133 [0,2*{NZERO} -1], while the return value for getpriority() and the
134 third parameter for setpriority() are in the range [-{NZERO},{NZERO}
135 -1].
136
138 None.
139
141 None.
142
144 nice(), sched_get_priority_max(), sched_setscheduler(), the Base Defi‐
145 nitions volume of IEEE Std 1003.1-2001, <sys/resource.h>
146
148 Portions of this text are reprinted and reproduced in electronic form
149 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
150 -- Portable Operating System Interface (POSIX), The Open Group Base
151 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
152 Electrical and Electronics Engineers, Inc and The Open Group. In the
153 event of any discrepancy between this version and the original IEEE and
154 The Open Group Standard, the original IEEE and The Open Group Standard
155 is the referee document. The original Standard can be obtained online
156 at http://www.opengroup.org/unix/online.html .
157
158
159
160IEEE/The Open Group 2003 GETPRIORITY(3P)