pthread nice value setting for default scheduler in C
It's unclear to me what makes you think the setpriority()
function would be in any way applicable to the task you are attempting to perform. From its docs:
The
setpriority()
function shall set the nice value of a process, process group, or user [...].
A thread is none of those.
Moreover,
who is interpreted relative to which (a process identifier for
PRIO_PROCESS
, process group identifier forPRIO_PGRP
, and a user ID
forPRIO_USER
).
You are specifying PRIO_PROCESS
and passing a thread identifier, but a thread identifier is not a process id. pthread_t
is not even required to be an integer type, unlike pid_t
. It is not surprising, therefore, that setpriority()
fails, returning -1 and setting errno
appropriately. You would know that this was happening if you were properly checking your function call results for error codes.
Perhaps you can achieve your objective via the pthread_setschedprio()
function, instead.
is nice() used to change the thread priority or the process priority?
The pthreads man page says:
POSIX.1 also requires that threads share a range of other attributes
(i.e., these attributes are process-wide rather than per-thread):[...]
- nice value (
setpriority
(2))
So, theoretically, the "niceness" value is global to the process and shared by all threads, and you should not be able to set a specific niceness for one or more individual threads.
However, the very same man page also says:
LinuxThreads
The notable features of this implementation are the following:
[...]
- Threads do not share a common nice value.
NPTL
[...]
NPTL still has a few non-conformances with POSIX.1:
- Threads do not share a common nice value.
So it turns out that both threading implementations on Linux (LinuxThreads and NPTL) actually violate POSIX.1, and you can set a specific niceness for one or more individual threads by passing a tid
to setpriority() on these systems.
How to increase thread priority in pthreads?
The default Linux scheduling policy is SCHED_OTHER
, which have no priority choice but a nice
level to tweak inside the policy.
You'll have to change to another scheduling policy using function pthread_setschedparam
(see also man sched_setscheduler
)
'Normal' scheduling policies: (from sched_setscheduler(2)
)
SCHED_OTHER the standard round-robin time-sharing policy;
SCHED_BATCH for "batch" style execution of processes; and
SCHED_IDLE for running very low priority background jobs.
Real-time scheduling policies:
SCHED_FIFO a first-in, first-out policy; and
SCHED_RR a round-robin policy.
In your case maybe you can use SCHED_BATCH
as this does not require root privileges.
Warning: wrong usage of real-time scheduling policies may hang your system. That's why you need root privileges to do this kind of operation.
Just to be sure of what your machine is capable of, you can use chrt
tool fromutil-linux
package.
As an example:
$ chrt -m
SCHED_OTHER min/max priority : 0/0
SCHED_FIFO min/max priority : 1/99
SCHED_RR min/max priority : 1/99
SCHED_BATCH min/max priority : 0/0
SCHED_IDLE min/max priority : 0/0
A way to waste less time (which I often use):
alias batchmake='time chrt --batch 0 make --silent'
While staying with user privileges, this propels the make
by 15% (in my case).
Edit: introducing nice
, SCHED_BATCH
, SCHED_IDLE
and chrt
tool. For accuracy ! :)
Display pthreads priority
You can use a programmatic way.
pthread_getschedparam(pthread_t thread, int *policy,
struct sched_param *param);
This function gives ya the scheduling parameters, in the struct sched_param you can find the scheduling priority as an integer.
Use that and print it to the screen.
For a better explanation, please check this man page:
http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setschedparam.3.html
C++ & boost::threads - How to prioritize thread based on work type?
As far as I know boost doesn't provide an API to do it (the C++11 standard certainly doesn't at least).
On Linux, you can nice
or setpriority
each thread independently from the others. Note however that this is not POSIX-conformant:
According to POSIX, the nice value is a per-process setting. However, under the current Linux/NPTL implementation of POSIX threads, the nice value is a per-thread attribute: different threads in the same process can have different nice values. Portable applications should avoid relying on the Linux behavior, which may be made standards conformant in the future.
Since Linux uses pthreads
(and so does the Linux port of boost
) you could also use pthread_setschedparam
which has the advantage of being more portable than the Linux-specific per-thread nice
behaviour.
In both cases, there's the slight uneasiness due to having to resort to system facilities in order to manage supposedly "opaque" boost
(or std
in C++11) resources, so tread carefully (as with anything implementation-specific).
Related Topics
How to Get Command History by Cursor Key in Linux Tclsh
Where to Put Svn Repository Directory in Linux
Openssl/Rsa - Using a Public Key to Decrypt
Why Does Pages Allocation with Order of 10 or 11 Using _Get_Free_Pages() Usually Fail
Busybox in Embedded Linux Shows "Applet Not Found"
Replace System Call in Linux Kernel 3
Why Is "Autoreconf" Not Used Often
Bash - How to Print Multi Line Strings (With '\N') Using Printf
Linux: How to Enable Execute in Place (Xip) for Ramfs/Tmpfs
Difference Between Unistd.H and Sys/Types.H in Linux
Linux Append Console Output to a Logfile
How to Create a Multi Partition Sd Disk Image Without Root Privileges
Finding Threading Bottlenecks and Optimizing for Wall-Time with Perf
Does File ".Bash_History" Always Record Every Command I Ever Issue
Find The Depth of The Current Path
How to Simulate Two Consecutive Enter Key Presses for a Command in a Bash Script