operation not permitted while setting new priority for thread

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)


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.

Tags:

C++

Pthreads