Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operation not permitted while setting new priority for thread

Tags:

c++

pthreads

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.

like image 332
Invictus Avatar asked May 22 '12 15:05

Invictus


People also ask

What is thread priority in Linux?

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..

What is a thread's priority and how it is used in scheduling?

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.

What is thread priority in OS?

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.


2 Answers

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.

like image 105
Mike Seymour Avatar answered Oct 08 '22 19:10

Mike Seymour


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)

like image 39
bjlaub Avatar answered Oct 08 '22 19:10

bjlaub