* Alejandro Colomar <alx.manpages@xxxxxxxxx>, 2022-12-11 18:33:
-to the nice value for the calling thread.
+to the nice value for the calling process.
nice() affecting the whole process, not just the calling thread, is what
POSIX requires, and what glibc documents, but it's not actually how it
works on Linux at the moment[*].
This is documented in the setpriority(2) man page:
"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."
It would be prudent to document this bug in the nice(2) man page too.
[*] I've tested this with Linux 6.0 + glibc 2.36.
See the attached test program.
--
Jakub Wilk
#define _GNU_SOURCE
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void print_nice()
{
printf("[%jd] nice = %d\n", (intmax_t) gettid(), nice(0));
}
static void* thread(void *arg)
{
print_nice();
nice(17);
print_nice();
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pt;
print_nice();
int rc = pthread_create(&pt, NULL, thread, NULL);
if (rc < 0)
abort();
rc = pthread_join(pt, NULL);
if (rc < 0)
abort();
print_nice();
}