Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing thread priority and scheduler in linux

I have a single threaded application. If i use below code, 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 such as below, i dont get an error. What is the difference between the two for a single threaded application and is the below function really changing the scheduler and priority or am i missing some error handling?

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);
}
like image 240
Jimm Avatar asked Jan 05 '13 20:01

Jimm


1 Answers

The error may be caused by a limit set on realtime priority (ulimit -r to check, ulimit -r 99 to allow 1-99 priorities). As of pthread_setschedparam being successful: if you compiled without -pthread option, this function is just a stub, like some other pthread functions. With -pthread option, the results should be identical (strace shows that the same system call is used).

like image 88
Anton Kovalenko Avatar answered Oct 13 '22 00:10

Anton Kovalenko