因爲樹上的例子在Fedora15,Fedora上都沒能跑起來,估計是Fedora和RedHat的調度策略有一些不同吧,所以自己改了一下,把調度策略和優先級值改了,然後正確顯示:
點擊(此處)摺疊或打開
- #include
#include
#include
#include
void *thread_function(void *arg);
char message[]="Hello World";
int thread_finished=0;
int main(int argc,char argv[])
{
int res;
int max_priority,min_priority;
pthread_t a_thread;
void *thread_result;
pthread_attr_t thread_attr;
struct sched_param scheduling_value;
res=pthread_attr_init(thread_attr);//初始化線程屬性
if(res!=0)
{
perror("Attribute creation failed");
exit(EXIT_FAILURE);
}
res=pthread_attr_setschedpolicy(thread_attr,SCHED_FIFO);//設置線程調度策略
if(res!=0)
{
perror("Setting schedpolicy failed");
exit(EXIT_FAILURE);
}
res=pthread_attr_setdetachstate(thread_attr,PTHREAD_CREATE_DETACHED);//設置狀態
if(res!=0)
{
perror("Setting detachstate failed");
exit(EXIT_FAILURE);
}
else
printf("set the datachstate to PTHREAD_CTEATE_DETACHED\n");
res=pthread_create(a_thread,thread_attr,thread_function,(void *)message);//創建線程
if(res!=0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
else
printf("Create new thread success");
max_priority=sched_get_priority_max(SCHED_FIFO);//獲取最大優先級
min_priority=sched_get_priority_min(SCHED_FIFO);//最小優先級
printf("The max priority of SCHED_OTHER is %d; the min is %d\n",max_priority,min_priority);
/*
res=pthread_attr_setinheritsched(thread_attr,PTHREAD_EXPLICIT_SCHED);
printf("\nres_before-2:%d\n",res);
res=pthread_attr_setschedpolicy(thread_attr,SCHED_FIFO);
printf("\nres_before-1:%d\n",res);
if(res!=0)
{
perror("set schedpolicy failed");
exit(EXIT_FAILURE);
}
max_priority=sched_get_priority_max(SCHED_FIFO);
min_priority=sched_get_priority_min(SCHED_FIFO);
printf("The max priority of SCHED_FIFO is %d; the min is %d\n",max_priority,min_priority);
*/
res=pthread_attr_getschedparam(thread_attr,scheduling_value);
if(res!=0)
{
perror("get schedpolicy failed");
exit(EXIT_FAILURE);
}
else
printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
scheduling_value.sched_priority=10;
//printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
res=pthread_attr_setschedparam(thread_attr,scheduling_value);//設置優先級
//printf("\nres_before:%d\n",res);
if(res!=0)
{
perror("Setting schedpolicy failed\n");
exit(EXIT_FAILURE);
}
else
printf("Set the schedulparam success\n");
//printf("The priority of current thread is %d\n",scheduling_value.sched_priority);//讀調度參數
res=pthread_attr_getschedparam(thread_attr,scheduling_value);
//printf("\nres_after:%d\n",res);
if(res!=0)
{
perror("Get schedpolicy failed");
exit(EXIT_FAILURE);
}
else
printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
(void)pthread_attr_destroy(thread_attr);//刪除屬性
sleep(2);
while(!thread_finished)
{
printf("Waiting for thread finished ...\n");
sleep(1);
}
printf("Ohter thread finished ,bye!\n");
exit(1);
}
void *thread_function(void *arg)
{
printf("Thread_function if running . Arguement was %d\n",(char *)arg);
sleep(4);
printf("Second thread setting finished flag,and exitting now\n");
thread_finished=1;
pthread_exit(NULL);
}