linux线程的优先级设置

linux并不是实时操作系统,把下面的代码运行一遍就能够理解了,代码有很详细的注释。

  1. #include    <stdio.h> 
  2. #include    <stdlib.h> 
  3. #include    <unistd.h> 
  4. #include    <pthread.h> 
  5. #include    <signal.h> 
  6. #include    <string.h> 
  7. void * thr_fun(void *arg) 
  8.     int policy, ret; 
  9.     struct sched_param param; 
  10.     //获取线程调度参数 
  11.     ret = pthread_getschedparam(pthread_self(), &policy, &param); 
  12.     if(ret!=0) 
  13.     { 
  14.         printf("pthread_getschedparam %s/n", strerror(ret) ); 
  15.         exit(1); 
  16.     } 
  17.     if (policy == SCHED_FIFO) 
  18.     { 
  19.         printf("policy:SCHED_FIFO/n"); 
  20.     } 
  21.     else if (policy == SCHED_OTHER) 
  22.     { 
  23.         printf("policy:SCHED_OTHER/n"); 
  24.     } 
  25.     else if (policy == SCHED_RR) 
  26.     { 
  27.         printf("policy:SCHED_RR/n"); 
  28.     } 
  29.     printf("param:%d/n", param.sched_priority);  
  30.     long long i; 
  31.     while (1) { 
  32.         i++; 
  33.         i *= 2; 
  34.     } 
  35.     pthread_exit(NULL);  
  36. int main(void
  37.     int ret; 
  38.     pthread_t tid; 
  39.     pthread_attr_t attr; 
  40.     int policy, inher; 
  41.     struct sched_param param; 
  42.      
  43.     //初始化线程属性 
  44.     pthread_attr_init(&attr); 
  45.     //获取继承的调度策略 
  46.     ret = pthread_attr_getinheritsched(&attr, &inher); 
  47.     if (ret!=0) 
  48.     { 
  49.         printf("pthread_attr_getinheritsched/n%s/n", strerror(ret)); 
  50.         exit(1); 
  51.     } 
  52.     // 
  53.     if (inher == PTHREAD_EXPLICIT_SCHED)  
  54.     { 
  55.         printf("PTHREAD_EXPLICIT_SCHED/n"); 
  56.     } 
  57.     else if (inher == PTHREAD_INHERIT_SCHED)  
  58.     {    
  59.         printf("PTHREAD_INHERIT_SCHED/n"); 
  60.         inher = PTHREAD_EXPLICIT_SCHED; 
  61.     } 
  62.     //设置继承的调度策略 
  63.     //必需设置inher的属性为 PTHREAD_EXPLICIT_SCHED,否则设置线程的优先级会被忽略 
  64.     ret = pthread_attr_setinheritsched(&attr, inher); 
  65.     if (ret!=0) 
  66.     { 
  67.         printf("pthread_attr_setinheritsched/n%s/n", strerror(ret)); 
  68.         exit(1); 
  69.     } 
  70.      
  71.     policy = SCHED_FIFO;//在Ubuntu9.10上需要root权限 
  72.     //设置线程调度策略 
  73.     ret = pthread_attr_setschedpolicy(&attr, policy); 
  74.     if (ret!=0) 
  75.     { 
  76.         printf(" pthread_attr_setschedpolicy/n%s/n", strerror(ret)); 
  77.         exit(1); 
  78.     } 
  79.     param.sched_priority = 3; 
  80.     //设置调度参数 
  81.     ret = pthread_attr_setschedparam(&attr, &param); 
  82.     if (ret!=0) 
  83.     { 
  84.         printf(" pthread_attr_setschedparam/n%s/n", strerror(ret)); 
  85.         exit(1); 
  86.     } 
  87.     //创建线程 
  88.     ret = pthread_create(&tid, &attr, thr_fun, NULL); 
  89.     if (ret!=0) 
  90.     { 
  91.         printf("pthread_create/n%s/n", strerror(ret)); 
  92.         exit(1); 
  93.     } 
  94.     while (1) { 
  95.         printf("hello world/n"); 
  96.     } 
  97.     pthread_join(tid, NULL); 
  98.     pthread_exit(NULL); 

线程中不要用perror,请使用strerror。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值