2012-06-08 31 views
0

我想以某種方式使用pthread庫中的讀寫鎖,即作者優先於讀者。我在我的男人頁讀取Pthread - 設置調度程序參數

如果線程執行調度選項的支持,並參與了鎖的線程與調度策略的SCHED_FIFO或SCHED_RR執行,則調用線程不應獲得,如果一個作家鎖持有該鎖或者如果具有較高或相同優先級的寫入者被鎖定在鎖上;否則,調用線程將獲得該鎖。

所以我寫的小功能,設置了線程調度選項。

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); 
    } 
} 

}

對不起那些a,b,c但我試圖malloc一切,我仍然可以在電話會議中pthread_setschedparamSIGSEGV,我想知道爲什麼嗎?

回答

1

我不知道這些是否是問題的確切原因,但他們應該幫助您磨合。

(1)pthread_setschedparam否則返回成功a 0和一個正數。所以

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

永遠不會執行。它應該是這樣的:

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

順便說一句,這是不是100%清楚你在做什麼,但看上去pthread_kill約醜陋的方式做到這一點越好。

(2)syscall(SYS_gettid)獲取操作系統threadID。 pthread__setschedparam預計pthreads線程ID,這是不同的。 pthreads線程ID由pthread_createpthread_self返回,數據類型爲pthread_t。將pthread__setschedparam更改爲使用此類型和適當的值,看看是否有所改進。

(3)您需要以特權用戶身份運行以更改計劃。嘗試以root或sudo或其他方式運行程序。

+0

1. pthread_kill是通知主線程,另一個遇到致命錯誤,我忽略sigint執行清理的信號處理程序 – Andna

+0

2.你是正確的關於系統調用和pthread_self,我改變了傳遞參數到pthread_self,它的工作3.also你就在這裏。我非常感謝你的幫助。 – Andna