Change thread priority and scheduler on Linux

I have a single threaded application. If I use the code below, I get sched_setscheduler(): Operation not permitted .

 struct sched_param param; param.sched_priority = 1; if (sched_setscheduler(getpid(), SCHED_RR, &param)) printf(stderr, "sched_setscheduler(): %s\n", strerror(errno)); 

But if I use pthread api, for example, below, I do not get an error. What is the difference between the two for a single-threaded application and is the next function really changing the scheduler and priority, or am I missing some errors?

 void assignRRPriority(int tPriority) { int policy; struct sched_param param; pthread_getschedparam(pthread_self(), &policy, &param); param.sched_priority = tPriority; if(pthread_setschedparam(pthread_self(), SCHED_RR, &param)) printf("error while setting thread priority to %d", tPriority); } 
+6
source share
2 answers

The error can be caused by the restriction set for real-time priority ( ulimit -r to check, ulimit -r 99 to allow priorities 1-99). With pthread_setschedparam will succeed: if you compiled without the -pthread option, this function will just be a stub like some other pthread functions. If the parameter is -pthread , the results should be identical ( strace indicates that the same system call is being used).

+3
source

You lack basic error handling. You need some <there. Try the following:

 if(pthread_setschedparam(pthread_self(), SCHED_RR, &param) < 0) { perror("pthread_setschedparam"); } 
0
source

All Articles