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?
I don’t know if these are the exact causes of your problems but they should help you hone in on it.
(1)
pthread_setschedparamreturns a 0 on success and a positive number otherwise. Sowill never execute. It should be something like:
As an aside, it isn’t 100% clear what you are doing but
pthread_killlooks about as ugly a way to do it as possible.(2)
syscall(SYS_gettid)gets the OS threadID.pthread__setschedparamexpects the pthreads thread id, which is different. The pthreads thread id is returned bypthread_createandpthread_selfin the datatypepthread_t. Change thepthread__setschedparamto 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.