linux 环境下指定线程优先级

本文介绍Linux中线程的三种调度策略:SCHED_OTHER、SCHED_FIFO和SCHED_RR,并展示了如何通过API设置线程优先级及示例代码。

写这篇文章主要是为了记录以便今后的查找,如有错误,望批评指正。

Linux内核的三种调度策略:

  1,SCHED_OTHER 分时调度策略,
  2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃
  3,SCHED_RR实时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平
Linux线程优先级设置
   首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:

  int sched_get_priority_max(int policy);

  int sched_get_priority_min(int policy);

  SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。
设置和获取优先级通过以下两个函数

int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
  int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
 param.sched_priority = 51; //设置优先级

   系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

上面的param使用了下面的这个数据结构:

struct sched_param
{
    int __sched_priority; //所要设定的线程优先级
};

网上有很多的关于linux环境下测试自己系统所支持的优先级的例子,可以借鉴过来从而确定优先级取值范围,下面是指定线程优先级的示例。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void Thread1()
{
  sleep(1);
  int i,j;
  int policy;
  struct sched_param param;
  pthread_getschedparam(pthread_self(),&policy,&param);
  if(policy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if(policy == SCHED_RR);
  printf("SCHED_RR 1 \n");
  if(policy==SCHED_FIFO)
    printf("SCHED_FIFO\n");

while(1)

{

    printf("thread 1\n");
  }
  printf("Pthread 1 exit\n");
}

void Thread2()
{
  sleep(1);
  int i,j,m;
  int policy;
  struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,&param);
 if(policy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if(policy == SCHED_RR);
  printf("SCHED_RR\n");
  if(policy==SCHED_FIFO)
    printf("SCHED_FIFO\n");

  while(1)
  {
    printf("thread 2\n");
  }
  printf("Pthread 2 exit\n");
}

void Thread3()
{
  sleep(1);
  int i;
  int policy;
  struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,&param);
 if(policy == SCHED_OTHER)
    printf("SCHED_OTHER\n");
  if(policy == SCHED_RR)
    printf("SCHED_RR \n");
  if(policy==SCHED_FIFO)
    printf("SCHED_FIFO\n");

  while(1)
  {
    printf("thread 3\n");
  }
  printf("Pthread 3 exit\n");
}

int main()
{
  int i;
  i = getuid();
  if(i==0)
    printf("The current user is root\n");
  else
    printf("The current user is not root\n");

  pthread_t ppid1,ppid2,ppid3;
  struct sched_param param;

  pthread_attr_t attr1,attr2,attr3;
  
  pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
pthread_attr_init(&attr3);
 
 param.sched_priority = 51;
 pthread_attr_setschedpolicy(&attr2,SCHED_RR);
 pthread_attr_setschedparam(&attr2,&param);
 pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话

 param.sched_priority = 21;
 pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
 pthread_attr_setschedparam(&attr1,&param);
 pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
 
 pthread_create(&ppid3,&attr3,(void *)Thread3,NULL);
 pthread_create(&ppid2,&attr2,(void *)Thread2,NULL);
 pthread_create(&ppid1,&attr1,(
void *)Thread1,NULL);
 
 pthread_join(ppid3,NULL);
 pthread_join(ppid2,NULL);
 pthread_join(ppid1,NULL);

 

pthread_attr_destroy(&attr3);

 pthread_attr_destroy(&attr2);

pthread_attr_destroy(&attr1);

 return 0;

}

通过判断param.sched_priority的值来确定哪个线程会先要执行。由于每个线程均是一个white(1)的死循环,因此当程序运行起来后,优先级高的线程会独占资源,而优先级较低的线程则没有机会执行。

另:用gcc编译时 需要添加库 示例为:gcc -o pthread test.c -lpthread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值