Andna
Andna

Reputation: 6689

Pthread - setting scheduler parameters

I wanted to use read-writer locks from pthread library in a way, that writers have priority over readers. I read in my man pages that

If the Thread Execution Scheduling option is supported, and the threads involved in the lock are executing with the scheduling policies SCHED_FIFO or SCHED_RR, the calling thread shall not acquire the lock if a writer holds the lock or if writers of higher or equal priority are blocked on the lock; otherwise, the calling thread shall acquire the lock.

so I wrote small function that sets up thread scheduling options.

void thread_set_up(int _thread)
{
 struct sched_param *_param=malloc(sizeof (struct sched_param));
 int *c=malloc(sizeof(int));
 *c=sched_get_priority_min(SCHED_FIFO)+1;
 _param->__sched_priority=*c;
 long *a=malloc(sizeof(long));
 *a=syscall(SYS_gettid);
 int *b=malloc(sizeof(int));
 *b=SCHED_FIFO;
 if (pthread_setschedparam(*a,*b,_param) == -1)
 {
    //depending on which thread calls this functions, few thing can happen
    if (_thread == MAIN_THREAD)
        client_cleanup();
    else if (_thread==ACCEPT_THREAD)
    {
        pthread_kill(params.main_thread_id,SIGINT);
        pthread_exit(NULL);
    }
}

}

sorry for those a,b,c but I tried to malloc everything, still I get SIGSEGV on the call to pthread_setschedparam, I am wondering why?

Upvotes: 0

Views: 967

Answers (1)

Duck
Duck

Reputation: 27552

I don't know if these are the exact causes of your problems but they should help you hone in on it.

(1) pthread_setschedparam returns a 0 on success and a positive number otherwise. So

if (pthread_setschedparam(*a,*b,_param) == -1) 

will never execute. It should be something like:

if ((ret = pthread_setschedparam(*a, *b, _param)) != 0)
{ //yada yada 
}

As an aside, it isn't 100% clear what you are doing but pthread_kill looks about as ugly a way to do it as possible.

(2) syscall(SYS_gettid) gets the OS threadID. pthread__setschedparam expects the pthreads thread id, which is different. The pthreads thread id is returned by pthread_create and pthread_self in the datatype pthread_t. Change the pthread__setschedparam to use this type and the proper values instead and see if things improve.

(3) You need to run as a priviledge user to change the schedule. Try running the program as root or sudo or whatever.

Upvotes: 1

Related Questions