linux 下设置线程优先级

linux 下设置线程优先级

原文链接:http://www.cnblogs.com/xiaotlili/p/3510224.html

root@ubuntu:tmp# cat test2.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>  


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

  for(i=1;i<10;i++)
  {
    for(j=1;j<5000000;j++)
    {
    }
    printf("thread 1\n");
  }
  printf("Pthread 1 exit\n");
}

void Thread2()
{
  printf("t2\n");
  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 2\n");
  if(policy == SCHED_RR);
    printf("SCHED_RR 2\n");
  if(policy==SCHED_FIFO)
    printf("SCHED_FIFO 2\n");

  for(i=1;i<10;i++)
  {
    for(j=1;j<5000000;j++)
    {
      
    }
    printf("thread 2\n");
  }
  printf("Pthread 2 exit\n");
}

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

  for(i=1;i<10;i++)
  {
    for(j=1;j<5000000;j++)
    {
    }
    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 attr,attr1,attr2;
  
  pthread_attr_init(&attr1);
  pthread_attr_init(&attr);
  pthread_attr_init(&attr2);
  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_RR);
  pthread_attr_setschedparam(&attr1,&param);
  pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
 
  pthread_create(&ppid3,&attr,(void *)Thread3,NULL);
  pthread_create(&ppid2,&attr1,(void *)Thread2,NULL);
  pthread_create(&ppid1,&attr2,(void *)Thread1,NULL);
 
  pthread_join(ppid3,NULL);
  pthread_join(ppid2,NULL);
  pthread_join(ppid1,NULL);
  pthread_attr_destroy(&attr2);
  pthread_attr_destroy(&attr1);
  return 0;
}

运行一下

gcc -o test2 test2.c -lpthread
The current user is root
t2
t3
t1
SCHED_OTHER 3
SCHED_RR 1 
SCHED_RR 2
thread 1
thread 3
thread 2
thread 1
thread 3
thread 2
thread 1
thread 2
thread 3
thread 1
thread 2
thread 3
thread 1
thread 2
thread 3
thread 1
thread 2
thread 3
thread 1
thread 3
thread 1
thread 2
thread 3
thread 1
Pthread 1 exit
thread 2
thread 3
Pthread 3 exit
thread 2
Pthread 2 exit

再跑一次,其实结果会变

root@ubuntu:tmp# ./test2
The current user is root
t3
t1
t2
SCHED_RR 1 
SCHED_RR 2
thread 1
thread 2
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
thread 1
thread 2
SCHED_OTHER 3
thread 1
Pthread 1 exit
thread 2
Pthread 2 exit
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
thread 3
Pthread 3 exit

能肯定的一点事 线程3 抢不过线程 1 2 ,线程 1 2就相对复杂点。所以单靠优先级,来控制线程的先后执行,还是有一点牵强。
基于时间片轮转的实时进程是,不是真正的改变进程的优先级,而是改变进程的基本时间片的长度。所以基于时间片轮转的进程调度,并不能保证高优先级的进程先运行。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值