Linux does not (yet) follow the POSIX.1 standard which says that a renice on a process affects all system scope threads in the process, because according to the pthreads(7) doc threads do not share a common nice value.However, sometimes, it can be convenient to renice everything related to a given process (one example would be Apache child processes and all their threads). So, how can I renice all threads belonging to a given process ?how can I renice all child processes belonging to a given process ?I am looking for a fairly easy solution. I know that process groups can sometimes be helpful, however, they do not always match what I want to do: they can include a broader or different set of processes. Using a cgroup managed by systemd might also be helpful, but even if I am interested to hear about it, I mostly looking for a standard solution.EDIT: also, man (7) pthreads says all of the threads in a process are placed in the same thread group; all members of a thread group share the same PID. So, is it even possible to renice something which doesn't have it's own PID?
How to renice all threads (and children) of one process on Linux?
linux;process;nice;thread
Difference between threads and processesImportant question on Linux, because documentation perpetuates doubts (about threads not having their own PID for example).Note: this answer explains Linux threads precisely. In short: the kernel only handles runnable entities, that is, something which can be run and scheduled. Kernel wise, these entities are called processes. A thread, is just a kind of process that shares (at least) memory space and signal handlers with another one. Every such process has a system-wide unique identifier: the PID (Process ID). For so-called threads, it is sometimes called TID (Thread ID), but from the sysadmin (and kernel!) point of view, TID and PID are the same thing (they share the same namespace).As a result, you can renice each thread individually because they have their own PID1. Finding all PIDs to renice recursivelyWe need to get the PIDs of all processes (normal or thread) which are descendant (children or in the thread group) of the to-be-niced process. This ought to be recursive (considering children's children).Anton Leontiev answer's gives the hint to do so: all folder names in /proc/$PID/task/ are threads' PID containing a children file listing potential children processes. However, it lacks recursivity, so here is a quick & dirty shell script to find them:#!/bin/sh[ $# -eq 1 -a -d /proc/$1/task ] || exit 1PID_LIST=findpids() {        for pid in /proc/$1/task/* ; do                pid=$(basename $pid)                PID_LIST=$PID_LIST$pid                 for cpid in $(cat /proc/$1/task/$pid/children) ; do                        findpids $cpid                done        done}findpids $1echo $PID_LISTIf process PID 1234 is the one you want to recursively nice, now you can do: renice -n 15 -p $(/path/to/findchildren.sh 1234)1 Note that, for POSIX compliance, calling getpid(2) within a thread will not give you the system-wide unique ID (PID) of this runnable entity, but rather the PID of the main process within the thread group. You would need to call gettid(2) instead. See this answer for more information.