1getpriority(3C) Standard C Library Functions getpriority(3C)
2
3
4
6 getpriority, setpriority - get and set the nice value
7
9 #include <sys/resource.h>
10
11 int getpriority(int which, id_t who);
12
13
14 int setpriority(int which, id_t who, int value);
15
16
18 The getpriority() function obtains the nice value of a process, thread,
19 or set of processes. The setpriority() function sets the nice value of
20 a process, thread, or set of processes to value+NZERO, where NZERO is
21 defined to be 20.
22
23
24 Target entities are specified by the values of the which and who argu‐
25 ments. The which argument can be one of the following values:
26 PRIO_PROCESS, PRIO_PGRP, PRIO_USER, PRIO_GROUP, PRIO_SESSION, PRIO_LWP,
27 PRIO_TASK, PRIO_PROJECT, PRIO_ZONE, or PRIO_CONTRACT, indicating that
28 the who argument is to be interpreted as a process ID, a process group
29 ID, an effective user ID, an effective group ID, a session ID, a thread
30 (lwp) ID, a task ID, a project ID, a zone ID, or a process contract ID,
31 respectively. A 0 value for the who argument specifies the current
32 process, process group, or user. A 0 value for the who argument is
33 treated as valid group ID, session ID, thread (lwp) ID, task ID,
34 project ID, zone ID, or process contract ID. A P_MYID value for the who
35 argument can be used to specify the current group, session, thread,
36 task, project, zone, or process contract, respectively.
37
38
39 If a specified process is multi-threaded, the nice value set with set‐
40 priority() affects all threads in the process.
41
42
43 If more than one process is specified, getpriority() returns NZERO less
44 than the lowest nice value pertaining to any of the specified entities,
45 and setpriority() sets the nice values of all of the specified pro‐
46 cesses to value+NZERO.
47
48
49 The default nice value is NZERO. Lower nice values cause more favorable
50 scheduling. The range of valid nice values is 0 to NZERO*2-1. If
51 value+NZERO is less than the system's lowest supported nice value, set‐
52 priority() sets the nice value to the lowest supported value. If
53 value+NZERO is greater than the system's highest supported nice value,
54 setpriority() sets the nice value to the highest supported value.
55
56
57 Only a process with appropriate privileges can lower the nice value.
58
59
60 Any process or thread using SCHED_FIFO or SCHED_RR is unaffected by a
61 call to setpriority(). This is not considered an error. A process or
62 thread that subsequently reverts to SCHED_OTHER will not have its pri‐
63 ority affected by such a setpriority() call.
64
65
66 The effect of changing the nice value varies depending on the schedul‐
67 ing policy in effect.
68
69
70 Since getpriority() can return the value -1 on successful completion,
71 it is necessary to set errno to 0 prior to a call to getpriority(). If
72 getpriority() returns the value -1, then errno can be checked to see if
73 an error occurred or if the value is a legitimate nice value.
74
76 Upon successful completion, getpriority() returns an integer in the
77 range from -NZERO to NZERO-1. Otherwise, −1 is returned and errno is
78 set to indicate the error.
79
80
81 Upon successful completion, setpriority() returns 0. Otherwise, −1 is
82 returned and errno is set to indicate the error.
83
85 The getpriority() and setpriority() functions will fail if:
86
87 ESRCH No process or thread could be located using the which and who
88 argument values specified.
89
90
91 EINVAL The value of the which argument was not recognized, or the
92 value of the who argument is not a valid process ID, process
93 group ID, user ID, group ID, session ID, thread (lwp) ID,
94 task ID, project ID, or zone ID.
95
96
97
98 In addition, setpriority() may fail if:
99
100 EPERM A process was located, but neither the real nor effective
101 user ID of the executing process match the effective user ID
102 of the process whose nice value is being changed.
103
104
105 EACCES A request was made to change the nice value to a lower
106 numeric value and the current process does not have appropri‐
107 ate privileges.
108
109
111 Example 1 Example using getpriority()
112
113
114 The following example returns the current scheduling priority for the
115 process ID returned by the call to getpid(2).
116
117
118 #include <sys/resource.h>
119 ...
120 int which = PRIO_PROCESS;
121 id_t pid;
122 int ret;
123
124 pid = getpid();
125 ret = getpriority(which, pid);
126
127
128 Example 2 Example using setpriority()
129
130
131 The following example sets the nice value for the current process to 0.
132
133
134 #include <sys/resource.h>
135 ...
136 int which = PRIO_PROCESS;
137 id_t pid;
138 int value = -20;
139 int ret;
140
141 pid = getpid();
142 ret = setpriority(which, pid, value);
143
144
146 The getpriority() and setpriority() functions work with an offset nice
147 value (value-NZERO). The nice value is in the range 0 to 2*NZERO-1,
148 while the return value for getpriority() and the third parameter for
149 setpriority() are in the range -NZERO to NZERO-1.
150
152 See attributes(5) for descriptions of the following attributes:
153
154
155
156
157 ┌─────────────────────────────┬─────────────────────────────┐
158 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
159 ├─────────────────────────────┼─────────────────────────────┤
160 │Interface Stability │Committed │
161 ├─────────────────────────────┼─────────────────────────────┤
162 │Standard │See standards(5). │
163 └─────────────────────────────┴─────────────────────────────┘
164
166 nice(1), renice(1), sched_get_priority_max(3C), sched_setscheduler(3C),
167 attributes(5), standards(5)
168
169
170
171SunOS 5.11 1 Apr 2008 getpriority(3C)