tags:

views:

70

answers:

2

I just started with pthreads and don't know much about it. I was trying to set the priorities of pthreads using pthread_setschedprio() but it returns "EINVAL". Here is my code:

#include <pthread.h>
#include <errno.h>

void *Run(void *ptr);

class Thread {
public:
    pthread_t myPthread;

    void start() {
        pthread_create( &myPthread, NULL, Run, (void*)this );
    }

    Thread() { }
    virtual void run() = 0;
};

void *Run( void *ptr ) {
   Thread* tmp;
   tmp = (Thread*)ptr;
   tmp->run();
}


class MyThread : public Thread {
public:
    virtual void run() {
        while (1) {
            printf("Running\n");
            sleep(1);
        }
    }
};




MyThread t1;


int main()
{
    int status;

    t1.start();
    status= pthread_setschedprio(t1.myPthread, 300);
    if(status!=0)
    {
        if(status==EPERM)
        {
            fprintf(stderr,"EPERM\n");
        }
        else if(status==EINVAL)
        {
            fprintf(stderr,"EINVAL\n");
        }
        else
        {
            fprintf(stderr,"neither EPERM nor EINVAL\n");
        }

        fprintf(stderr,"error %d\n",status);

        errno=status;

        perror("");

        exit(1);
    }
   pthread_join( t1.myPthread, NULL);

    exit(0);
}

When I try to compile it, I get EINVAL Running error 22 Invalid argument

Can anybody tell me why I get the error and how I can solve it?

+1  A: 

From this link it looks like it means the priority you specified is invalid for the current scheduling policy. If you look at this page you can determine valid priorities for the different scheduling policies. Finally you can use pthread_getschedparam to determine the current scheduling policy you are using if you are unsure.

Eric LaForce
I added pthread_t thread;struct sched_param param;int policy;pthread_getschedparam(thread, printf("policy = %d\n", policy);printf("priority = %d\n",param.sched_priority);if(policy!=0) { if(policy==SCHED_FIFO) { fprintf(stderr,"SCHED_FIFO\n"); } else if(status==SCHED_RR) { fprintf(stderr,"SCHED_RR\n"); } else if(status==SCHED_OTHER) { fprintf(stderr,"SCHED_OTHER\n"); } }but now it gives me policy = -1217785868priority = 134515296What does that mean?
sleep
What is the return value of pthread_getschedparam?
Eric LaForce
The return value is 3
sleep
Well looking at the Unix error codes a value of 3 is ESRCH. From the pthread_getschedparam() man page, a ESRCH value indicates "The value specified by thread does not refer to an existing thread". I just noticed you are declaring thread, and not using the thread from your previous post. Were you just trying this as an example?
Eric LaForce
sleep
The most logical point of failure would be the sched_param - param. Do you have the scheduling priority set in the param structure?
Eric LaForce
What do you mean by "scheduling priority set in the param structure"? I declared the struct sched_param param but did not set anything.
sleep
The sched_param structure looks like:struct sched_param { int sched_priority;};So what I mean is did you initialize/set the sched_priority in this structure? I am not 100% on this, but that seems the only place where "one of the scheduling parameters associated with the scheduling policy policy is invalid".
Eric LaForce
But sched.h should have set the sched_priority in struct sched_param already, right?
sleep
Not it only defines it, it does not initialize it.
Eric LaForce
Ok it is finally compiling without any error *phew* but when I try to implement two threads with different priorities, it is not performing as expected. Do I have to define two different struct sched_param if I want to implement two threads with two different priorities?
sleep
Eric LaForce
Thank you!! You are awesome!!
sleep
Can I get a check for the right answer then :)
Eric LaForce
A: 

Linux normally has its priorities from 0-139. Real-time priorities ranging from 0-99 and dynamic priorities ranging from -19-20 (but actually acting as 100-139).

Negative PR values are the procps tools' way of showing real time threads. In this case the Error is occuring because of the use of the priority 300, which is out of range.

Thread has a scheduling policy and Priority and the parameters passed are to be interpreted along with the Policy.

sched.h actually defines the different types of policies as

#define SCHED_OTHER 0
#define SCHED_FIFO  1
#define SCHED_RR    2

pthread_create is by default creating thread in SCHED_OTHER policy and that is the reason why you observed a zero.

superuser privilages are required if we need to change the priority of a FIFO or RR (Round Robin) Real time threads.

pv
I tried changing the priority to 20 but it still did not work. It still gives the error "Invalid argument". I then tried changing the priority of a FIFO with superuser privilege but that didn't work either. I still get EINVAL Error 134516192
sleep