pthread_policy_example.c Fedora上运行修改_xueyueguangshen-ChinaUnix博客

网友投稿 925 2022-09-23

pthread_policy_example.c Fedora上运行修改_xueyueguangshen-ChinaUnix博客

pthread_policy_example.c Fedora上运行修改_xueyueguangshen-ChinaUnix博客

因为树上的例子在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);}elseprintf("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);}elseprintf("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);}elseprintf("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);}elseprintf("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);}

结果:set the datachstate to PTHREAD_CTEATE_DETACHEDCreate new thread successThe max priority of SCHED_OTHER is 99; the min is 1The priority of current thread is 0Set the schedulparam successThe priority of current thread is 10Thread_function if running . Arguement was 134520660Waiting for thread finished ...Waiting for thread finished ...Second thread setting finished flag,and exitting nowOhter thread finished ,bye!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python字符串处理的8招秘籍(python字符串处理函数汇总)
下一篇:F5负载均衡之检查命令的说明_quanshengaa-ChinaUnix博客
相关文章

 发表评论

暂时没有评论,来抢沙发吧~