How to increase thread priority in pthreads?

I am using pthread on Linux. I would like to increase the priority of the stream by setting sched_param.priority parameters. However, I could not find much information from the network regarding the stream priority range that I could set, or about the description of the stream priority.

In addition, I would like to know about the relative priority of the thread, since I would not want the priority of the thread to be too high and as a result the OS will stop. Can anyone help me with this?

+41
c ++ multithreading linux pthreads
Sep 06 2018-10-10T00:
source share
3 answers

The default Linux scheduling policy is SCHED_OTHER , which do not have priority choices, and have a nice level to configure inside the policy.

You will need to switch to another scheduling policy using the pthread_setschedparam function (see also man sched_setscheduler )

โ€œNormalโ€ planning policies: (by 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 Planning Policies:

  SCHED_FIFO a first-in, first-out policy; and SCHED_RR a round-robin policy. 

In your case, perhaps you can use SCHED_BATCH , as this does not require root privileges.

Warning: misuse of real-time scheduling policies can damage your system. To do this, you need root privileges to perform this kind of operation.

To be sure of the capabilities of your machine, you can use the chrt tool from util-linux .
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 spend less time (which I often use):

 alias batchmake='time chrt --batch 0 make --silent' 

During a stay with user privileges, this leads to the fact that make is 15% (in my case).

Edit: introduction of nice , SCHED_BATCH , SCHED_IDLE and chrt . For accuracy! :)

+46
Sep 07 2018-10-22T00:
source share
โ€” -

POSIX defines the request, so you can set the OS for a valid priority range.

int sched_get_priority_max(int policy);

int sched_get_priority_min(int policy);

Do not expect increased priority to strangle the device. Actually, don't expect it to do anything unless you are already using 100% of the processor cycles. Do not be surprised if the request indicates that the priority is not higher than the default value.

+22
Sep 06 '10 at 5:52
source share

The current answer from levif (recommending SCHED_BATCH) is incorrect for the current implementation of the NPTL stream on Linux (you can check which implementation your kernel has by running "getconf GNU_LIBPTHREAD_VERSION").

In today's kernel, only real-time scheduling policies allow sched_priority to be set โ€” it is always 0 for non-RT policies (SCHED_OTHER, SCHED_BATCH and SCHED_IDLE). Your only choice for policies other than RT is to set โ€œgoodโ€ values, for example. by setpriority (). There are no good characteristics of the exact behavior that can be expected to be set to โ€œgood,โ€ but at least in theory this can vary from kernel version to kernel version. For current Linux kernels, โ€œniceโ€ has a very strong effect, similar to priority, so you can use it to a large extent interchangeably. To increase the frequency of your flow, you want to lower your โ€œgoodโ€ value. This requires the CAP_SYS_NICE capability (usually root, although this is optional, see http://man7.org/linux/man-pages/man7/capabilities.7.html and http://man7.org/linux/man-pages /man3/cap_set_proc.3.html ).

In fact, SCHED_BATCH is intended for the opposite case of what the request requested: it is designed for intensive and lengthy work tasks that can live with lower priority. He tells the scheduler to fine a bit the waking priority for threads.

Also, in order to answer one of the early comments (I don't have enough reputation for commenting back yet - some tips for this answer would help :)). Yes, the bad news is that the POSIX.1 specification says that โ€œgoodโ€ affects processes, not individual threads. The good news is that Linux thread implementations (both NPTL and Linux source streams) violate the specification and allow individual threads to be influenced. It seemed funny to me that this is often called up in the ERRORS section of the manual pages. I would say that the error was in the POSIX.1 specification, which was supposed to allow such behavior, and not in implementations that were forced to provide it, despite the specification, and did it consciously and intentionally. In other words, not a mistake.

Most of this information is described in detail on the schedule page (7) (which for some reason is not supplied on my Fedora 20 system): http://man7.org/linux/man-pages/man7/sched.7.html

If you really want to influence sched_priority, you can look at real-time policies such as SCHED_RR).

+22
May 23 '14 at 21:36
source share



All Articles