I have created two threads . By default they have a priority of 0
which i can see using
pthread_getschedparam and then i try to increase their priority to say 2
and 3
respectively . But Whn i try do do so i get an error
error setting priority for T1: (1), Operation not permitted
error setting priority for T2: (1), Operation not permitted
I have used the scheduling policy of SCHED_RR
for them
int sched = SCHED_RR;
and then performed this :-
if (pthread_setschedparam(t1, sched, &t1_param) != 0)
{
std::cout << "error setting priority for T1: (" << errno << "), " <<
strerror(errno) << std::endl;
}
What is the reason why I am not able to modify my threads priority because priority is within limit of 1
to 99
for SCHED_RR.
However, on Linux, the nice value is a per-thread attribute: different threads in the same process may have different nice values. The range of the nice value varies across UNIX systems. On modern Linux, the range is -20 (high priority) to +19 (low priority). On some other systems, the range is -20..
Threads are scheduled to run based on their scheduling priority. Each thread is assigned a scheduling priority. The priority levels range from zero (lowest priority) to 31 (highest priority). Only the zero-page thread can have a priority of zero.
Under some operating systems, the thread with the highest priority (of those threads that can be executed) is always scheduled to run first. If multiple threads with the same priority are all available, the scheduler cycles through the threads at that priority, giving each thread a fixed time slice in which to execute.
DISCLAIMER: I'm not an expert at Linux security, and the following advice might compromise or damage your computer.
In recent versions of Linux, there is a resource limit, RLIMIT_RTPRIO
, which specifies the maximum real-time priority you can use. You can check this from the shell:
> ulimit -r
0
On my version of Ubuntu (and probably yours too) there's also a hard limit of zero, so you can't simply use ulimit
or setrlimit
to raise this. One way to raise the hard limit is to add a line to /etc/security/limits.conf
like this (replacing <username>
with your username):
<username> hard rtprio 99
Then you should be able to use ulimit
(from the shell) or setrlimit
(from your program) to set the soft limit to the priority you need; alternatively, you could set that automatically by adding a second line to limits.conf
, replacing hard
with soft
.
> ulimit -Hr # show hard limit
99
> ulimit -r
0
> ulimit -Sr 1 # set soft limit
> ulimit -r
1
Do be careful running programs with real-time priority; it can kill the system if it starts misbehaving.
See this article for an explanation.
By default, user tasks in Linux have the scheduling policy SCHED_OTHER
. In order to change that to a realtime policy (i.e. SCHED_RR
as you are attempting to do), you need to be root. You could try running your program as root to verify this.
(also note this article is a little outdated - Linux 2.2. You might want to research this to see if the behavior has changed in newer kernels)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With