Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1844214
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: LINUX

2013-05-24 16:38:37

参考网上代码写了一段代码, :)


点击(此处)折叠或打开

  1. void setthreadhighpriority(pthread_attr_t* attr)
  2. {
  3.     struct sched_param sched;
  4.     int policy;
  5.     int minp;
  6.     int maxp;
  7.     int priority, inher;
  8.     int toset;
  9.     int ret;

  10.     if(0 != getuid())
  11.     {
  12.         return;
  13.     }
  14.     
  15.     ret = pthread_attr_getinheritsched(attr, &inher);
  16.     if (ret!=0 )
  17.     {
  18.         printf("pthread_attr_getinheritschedn%sn", strerror(ret));
  19.         return;
  20.     }

  21.     if (inher == PTHREAD_EXPLICIT_SCHED)
  22.     {
  23.         printf("PTHREAD_EXPLICIT_SCHEDn");
  24.     }
  25.        else if (inher == PTHREAD_INHERIT_SCHED)
  26.     {
  27.         inher = PTHREAD_EXPLICIT_SCHED;
  28.     }
  29.     
  30.     ret = pthread_attr_setinheritsched(attr, inher);
  31.     if (ret!=0)
  32.     {
  33.         printf("pthread_attr_setinheritschedn%sn", strerror(ret));
  34.         return;
  35.     }

  36.     ret = pthread_attr_getschedpolicy(attr, &policy);
  37.     if(ret)
  38.     {
  39.         printf(" line[%d] ret %dn ", __LINE__, ret);
  40.         return;
  41.     }

  42.     if(policy == SCHED_OTHER)
  43.     {
  44.         ret = pthread_attr_setschedpolicy(attr, SCHED_RR);
  45.         if(ret)
  46.         {
  47.             printf(" line[%d] ret %dn ", __LINE__, ret);
  48.             return;
  49.         }

  50.         ret = pthread_attr_getschedpolicy(attr, &policy);
  51.         if(ret)
  52.         {
  53.             printf(" line[%d] ret %dn ", __LINE__, ret);
  54.             return;
  55.         }

  56.         /*
  57.         switch (policy)
  58.         {
  59.             case SCHED_FIFO:
  60.                 printf("policy= SCHED_FIFOn") ;
  61.                 break ;
  62.             case SCHED_RR:
  63.                 printf("policy= SCHED_RRn") ;
  64.                 break ;
  65.             case SCHED_OTHER:
  66.                 printf("policy=SCHED_OTHERn") ;
  67.                 break ;
  68.             default :
  69.                 printf ( "policy=UNKNOWNn" ) ;
  70.                 break ;
  71.         }
  72.         */
  73.     }
  74.     
  75.     maxp = sched_get_priority_max(policy);
  76.     if(maxp < 0)
  77.     {
  78.         printf(" line[%d] ret %dn ", __LINE__, maxp);
  79.         return;
  80.     }
  81.     minp = sched_get_priority_min(policy);
  82.     if(minp < 0)
  83.     {
  84.         printf(" line[%d] ret %dn ", __LINE__, minp);
  85.         return;
  86.     }

  87.     ret = pthread_attr_getschedparam(attr, &sched);
  88.     if(ret)
  89.     {
  90.         printf(" line[%d] ret %dn ", __LINE__, ret);
  91.         return;
  92.     }
  93.     
  94.     priority = sched.__sched_priority;
  95.     if(priority == 0)
  96.     {
  97.         priority = 30;
  98.     }
  99.     
  100.     if(priority > maxp || priority < minp)
  101.     {
  102.         printf(" priority not setn");
  103.         return;
  104.     }

  105.     toset = priority + (maxp - priority)*6/10;
  106.     sched.__sched_priority = toset;
  107.     ret = pthread_attr_setschedparam(attr, &sched);
  108.     if(ret)
  109.     {
  110.         printf(" line[%d] ret %dn ", __LINE__, ret);
  111.         return;
  112.     }
  113.     ret = pthread_attr_getschedparam(attr, &sched);
  114.     if(ret)
  115.     {
  116.         printf(" line[%d] ret %dn ", __LINE__, ret);
  117.         return;
  118.     }

  119.     printf("before:%d now:%dn", priority, sched.__sched_priority);
  120.     return ;
  121. }


阅读(1015) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~