关于线程的调度

小喾 2017-12-01 09:32:27
在单核情况下,主线程pthread_create2个子线程使用SCHED_RR,为什么第一个线程可以运行,第二个线程无法运行呢,而主线程可以继续运行到pthread_exit。
void *threadfun1(void * arg) {
3 FUNCTION_BEGIN();
4 // sleep(3);
5 while(1) {
6 // printf("this is threadfun_1\n");
7 }
8 }
9
10 void *threadfun2(void * arg) {
11 FUNCTION_BEGIN();
12 // sleep(1);
13 while(1) {
14 // printf("this is threadfun_2\n");
15 }
16 }
17 int main()
18 {
19 struct timespec tp;
20 pthread_t threadId1,threadId2;
21 pthread_attr_t threadAttr1, threadAttr2;
22 struct sched_param threadParam;
23 struct sched_param threadParam1, threadParam2;
24 int policy1,policy2;
int threadPolicy;
27 int inheritSched = PTHREAD_EXPLICIT_SCHED;
28 int rrMinPriority = sched_get_priority_min(SCHED_RR);
29 int rrMaxPriority = sched_get_priority_max(SCHED_RR);
30 printf("\nrrMinPriority :%d,rrMaxPriority%d",rrMinPriority,rrMaxPriority);
31
32 status = sched_rr_get_interval(0,&tp);
33 NOZERO_EN(status);
34 printf("tv_sec:%d, tv_nsec%d\n",tp.tv_sec,tp.tv_nsec);
35
36
37 status = pthread_attr_init(&threadAttr1);
38 NOZERO_EN(status);
39 status = pthread_attr_init(&threadAttr2);
40 NOZERO_EN(status);
41
42 status = pthread_attr_setinheritsched(&threadAttr1,PTHREAD_EXPLICIT_SCHED);
43 NOZERO_EN(status);
44 status = pthread_attr_setinheritsched(&threadAttr2,PTHREAD_EXPLICIT_SCHED);
45 NOZERO_EN(status);
46
47 status = pthread_attr_setschedpolicy(&threadAttr1,SCHED_RR);
NOZERO_EN(status);
49 status = pthread_attr_setschedpolicy(&threadAttr2,SCHED_RR);
50 NOZERO_EN(status);
51
52 threadParam.sched_priority = (rrMinPriority + rrMaxPriority)/2;
53 status = pthread_attr_setschedparam(&threadAttr1,&threadParam);
54 NOZERO_EN(status);
55 status = pthread_attr_setschedparam(&threadAttr2,&threadParam);
56 NOZERO_EN(status);
NOZERO_EN(pthread_create(&threadId1,&threadAttr1,threadfun1,NULL));
79 NOZERO_EN(pthread_create(&threadId2,&threadAttr1,threadfun2,NULL));
80
81 NOZERO_EN(pthread_getschedparam(threadId1,&policy1 ,&threadParam1));
82 printf("\npolicy1:%d ,priority1:%d",policy1,threadParam1);
83
84 NOZERO_EN(pthread_getschedparam(threadId2,&policy2 ,&threadParam2));
85 printf("\npolicy2:%d ,priority2:%d\n",policy2,threadParam2);
86
87 printf("this is before pthread_exit\n");
88 pthread_exit(NULL);
89
90 }
...全文
1105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxianbin 2018-02-22
  • 打赏
  • 举报
回复
排除法吧,把 attr 、param 一个个的去掉,看哪个的问题
aabbabababaa 2018-02-21
  • 打赏
  • 举报
回复
没用过线程,来学习下。。。

742

社区成员

发帖
与我相关
我的任务
社区描述
该论坛主要探讨Linux系统在IBM Power平台的安装、部署、应用开发等话题,并为网友们提供自由交流的平台。
社区管理员
  • Power Linux社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧