线程绑定核,线程调度属性

来源:互联网 发布:python 前端开发 编辑:程序博客网 时间:2024/06/10 09:55

1、pthread_setaffinity_np

#include <pthread.h> 
#include <sched.h>

        cpu_set_t mask; 
        cpu_set_t get; 

        CPU_ZERO(&mask); 
        CPU_SET(i, &mask);  // i is cpu core id
        if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) { 
            fprintf(stderr, "set thread affinity failed\n"); 
        } 
        CPU_ZERO(&get); 
        if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) { 
            fprintf(stderr, "get thread affinity failed\n"); 
        } 
        for (j = 0; j < num; j++) { 
            if (CPU_ISSET(j, &get)) { 
                printf("thread %d is running in processor %d\n", (int)pthread_self(), j); 
            } 
        } 

2、sched_setaffinity

       CPU_ZERO(&mask); 
       CPU_SET(myid, &mask); 

      if (sched_setaffinity(0, sizeof(mask), &mask) == -1)  // 0 表示当前线程
        { 
                printf(“warning: could not set CPU affinity, continuing…\n”); 
        } 


3、设置线程调度模式和优先级

#include <pthread.h> 
#include <sched.h> 
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);  
void pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);  
int pthread_attr_setschedparam(pthread_attr_t *restrict attr,  
                         const struct sched_param *restrict param); 

inheritsched可以为如下值。
PTHREAD_INHERIT_SCHED:线程调度属性是从创建者线程继承得到,attr的任何调度属性都被忽略。
PTHREAD_EXPLICIT_SCHED:线程调度属性设置为属性对象attr的调度属性。


policy的值可以为在<sched.h>头文件中定义的以下值。
SCHED_FIFO:先进先出调度策略,执行线程运行到结束。// 除非阻塞或主动切换,否则不释放CPU
SCHED_RR:轮询调度策略,按照时间片将每个线程分配到处理器上。
SCHED_OTHER:另外的调度策略(根据实现定义)。这是任何新创建线程的默认调度策略。


sched_param结构体至少需要定义这个数据成员:
struct sched_param {  
   int sched_priority;  
   //...  
}; 
它可能还有其他的数据成员,以及多个用来返回和设置最小优先级、最大优先级、调度器、参数等的函数。如果调度策略是SCHED_FIFO或SCHED_RR,那么要求具有值的唯一成员是sched_priority。


按照如下方法使用sched_get_priority_max( )和sched_get_priority_max( ),可以得到优先级的最大值和最小值。
int sched_get_priority_max(int policy);  
int sched_get_priority_min(int policy); 


pthread_t ThreadA;  
pthread_attr_t SchedAttr;  
sched_param SchedParam;  
int MidPriority,MaxPriority,MinPriority;  

  // Step 1: initialize attribute object  
   pthread_attr_init(&SchedAttr);  
 
   // Step 2: retrieve min and max priority values for scheduling policy  
   MinPriority = sched_get_priority_max(SCHED_RR);  
   MaxPriority = sched_get_priority_min(SCHED_RR);  
 
   // Step 3: calculate priority value  
   MidPriority = (MaxPriority + MinPriority)/2;  
 
   // Step 4: assign priority value to sched_param structure  
   SchedParam.sched_priority = MidPriority;  
 
   // Step 5: set attribute object with scheduling parameter  
   pthread_attr_setschedparam(&SchedAttr, &SchedParam);  
 
   // Step 6: set scheduling attributes to be determined by attribute object  
   pthread_attr_setinheritsched(&SchedAttr, PTHREAD_EXPLICIT_SCHED);  
 
   // Step 7: set scheduling policy  
   pthread_attr_setschedpolicy(&SchedAttr,SCHED_RR);  
 
   // Step 8: create thread with scheduling attribute object  
   pthread_create(&ThreadA,&SchedAttr,task1,NULL);  


0 0