zoukankan      html  css  js  c++  java
  • 线程绑定CPU核-sched_setaffinity

     

     

    CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行.
    一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码可能会获得更好的性能.
    一个CPU的亲合力掩码用一个cpu_set_t结构体来表示一个CPU集合,下面的几个宏分别对这个掩码集进行操作:
       ·CPU_ZERO() 清空一个集合
       ·CPU_SET()与CPU_CLR()分别对将一个给定的CPU号加到一个集合或者从一个集合中去掉.
       ·CPU_ISSET()检查一个CPU号是否在这个集合中.

    下面两个函数就是用来设置获取线程CPU亲和力状态: 
        ·sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask) 
          该函数设置进程为pid的这个进程,让它运行在mask所设定的CPU上.如果pid的值为0,则表示指定的是当前进程,使当前进程运行在mask所设定的那些CPU上.第二个参数cpusetsize是mask所指定的数的长度.通常设定为sizeof(cpu_set_t).如果当前pid所指定的进程此时没有运行在mask所指定的任意一个CPU上,则该指定的进程会从其它CPU上迁移到mask的指定的一个CPU上运行. 
        ·sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask) 
          该函数获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中.即获得指定pid当前可以运行在哪些CPU上.同样,如果pid的值为0.也表示的是当前进程

     

    1. cpu_set_t的定义  
    2.   
    3. # define __CPU_SETSIZE 1024  
    4. # define __NCPUBITS (8 * sizeof (__cpu_mask))  
    5. typedef unsigned long int __cpu_mask;  
    6. # define __CPUELT(cpu) ((cpu) / __NCPUBITS)  
    7. # define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))  
    8. typedef struct  
    9. {  
    10. __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];  
    11. } cpu_set_t;  
    12.   
    13. # define __CPU_ZERO(cpusetp)   
    14. do {   
    15. unsigned int __i;   
    16. cpu_set_t *__arr = (cpusetp);   
    17. for (__i = 0; __i < sizeof (cpu_set_t) / sizeof (__cpu_mask); ++__i)   
    18. __arr->__bits[__i] = 0;   
    19. while (0)  
    20. # define __CPU_SET(cpu, cpusetp)   
    21. ((cpusetp)->__bits[__CPUELT (cpu)] |= __CPUMASK (cpu))  
    22. # define __CPU_CLR(cpu, cpusetp)   
    23. ((cpusetp)->__bits[__CPUELT (cpu)] &= ~__CPUMASK (cpu))  
    24. # define __CPU_ISSET(cpu, cpusetp)   
    25. (((cpusetp)->__bits[__CPUELT (cpu)] & __CPUMASK (cpu)) != 0)  
    cpu_set_t的定义
    
    # define __CPU_SETSIZE 1024
    # define __NCPUBITS (8 * sizeof (__cpu_mask))
    typedef unsigned long int __cpu_mask;
    # define __CPUELT(cpu) ((cpu) / __NCPUBITS)
    # define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))
    typedef struct
    {
    __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
    } cpu_set_t;
    
    # define __CPU_ZERO(cpusetp) 
    do { 
    unsigned int __i; 
    cpu_set_t *__arr = (cpusetp); 
    for (__i = 0; __i < sizeof (cpu_set_t) / sizeof (__cpu_mask); ++__i) 
    __arr->__bits[__i] = 0; 
    } while (0)
    # define __CPU_SET(cpu, cpusetp) 
    ((cpusetp)->__bits[__CPUELT (cpu)] |= __CPUMASK (cpu))
    # define __CPU_CLR(cpu, cpusetp) 
    ((cpusetp)->__bits[__CPUELT (cpu)] &= ~__CPUMASK (cpu))
    # define __CPU_ISSET(cpu, cpusetp) 
    (((cpusetp)->__bits[__CPUELT (cpu)] & __CPUMASK (cpu)) != 0)


     测试代码:

     

    1. #include<stdlib.h>  
    2. #include<stdio.h>  
    3. #include<sys/types.h>  
    4. #include<sys/sysinfo.h>  
    5. #include<unistd.h>  
    6.    
    7. #define __USE_GNU  
    8. #include<sched.h>  
    9. #include<ctype.h>  
    10. #include<string.h>  
    11. #include<pthread.h>  
    12. #define THREAD_MAX_NUM 100  //1个CPU内的最多进程数  
    13.    
    14. int num=0;  //cpu中核数  
    15. void* threadFun(void* arg)  //arg  传递线程标号(自己定义)  
    16. {  
    17.          cpu_set_t mask;  //CPU核的集合  
    18.          cpu_set_t get;   //获取在集合中的CPU  
    19.          int *a = (int *)arg;   
    20.          printf("the a is:%d ",*a);  //显示是第几个线程  
    21.          CPU_ZERO(&mask);    //置空  
    22.          CPU_SET(*a,&mask);   //设置亲和力值  
    23.          if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力  
    24.          {  
    25.                    printf("warning: could not set CPU affinity, continuing... ");  
    26.          }  
    27.          while (1)  
    28.          {  
    29.                    CPU_ZERO(&get);  
    30.                    if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力  
    31.                    {  
    32.                             printf("warning: cound not get thread affinity, continuing... ");  
    33.                    }  
    34.                    int i;  
    35.                    for (i = 0; i < num; i++)  
    36.                    {  
    37.                             if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力  
    38.                             {  
    39.                                      printf("this thread %d is running processor : %d ", i,i);  
    40.                             }  
    41.                    }  
    42.          }  
    43.    
    44.          return NULL;  
    45. }  
    46.    
    47. int main(int argc, char* argv[])  
    48. {  
    49.          num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数  
    50.          pthread_t thread[THREAD_MAX_NUM];  
    51.          printf("system has %i processor(s).  ", num);  
    52.          int tid[THREAD_MAX_NUM];  
    53.          int i;  
    54.          for(i=0;i<num;i++)  
    55.          {  
    56.                    tid[i] = i;  //每个线程必须有个tid[i]  
    57.                    pthread_create(&thread[0],NULL,threadFun,(void*)&tid[i]);  
    58.          }  
    59.          for(i=0; i< num; i++)  
    60.          {  
    61.                    pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束  
    62.          }  
    63.          return 0;  
    64. }  
    #include<stdlib.h>
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/sysinfo.h>
    #include<unistd.h>
     
    #define __USE_GNU
    #include<sched.h>
    #include<ctype.h>
    #include<string.h>
    #include<pthread.h>
    #define THREAD_MAX_NUM 100  //1个CPU内的最多进程数
     
    int num=0;  //cpu中核数
    void* threadFun(void* arg)  //arg  传递线程标号(自己定义)
    {
             cpu_set_t mask;  //CPU核的集合
             cpu_set_t get;   //获取在集合中的CPU
             int *a = (int *)arg; 
             printf("the a is:%d
    ",*a);  //显示是第几个线程
             CPU_ZERO(&mask);    //置空
             CPU_SET(*a,&mask);   //设置亲和力值
             if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力
             {
                       printf("warning: could not set CPU affinity, continuing...
    ");
             }
             while (1)
             {
                       CPU_ZERO(&get);
                       if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力
                       {
                                printf("warning: cound not get thread affinity, continuing...
    ");
                       }
                       int i;
                       for (i = 0; i < num; i++)
                       {
                                if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
                                {
                                         printf("this thread %d is running processor : %d
    ", i,i);
                                }
                       }
             }
     
             return NULL;
    }
     
    int main(int argc, char* argv[])
    {
             num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数
             pthread_t thread[THREAD_MAX_NUM];
             printf("system has %i processor(s). 
    ", num);
             int tid[THREAD_MAX_NUM];
             int i;
             for(i=0;i<num;i++)
             {
                       tid[i] = i;  //每个线程必须有个tid[i]
                       pthread_create(&thread[0],NULL,threadFun,(void*)&tid[i]);
             }
             for(i=0; i< num; i++)
             {
                       pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束
             }
             return 0;
    }


     

    编译命令:gcc bind.c -o bind -lpthread

    执行:./bind

    输出结果:略

    特别注意:

    #define __USE_GNU不要写成#define _USE_GNU

    #include<pthread.h>必须写在#define __USE_GNU之后,否则编译会报错

    查看你的线程情况可以在执行时在另一个窗口使用top -H来查看线程的情况,查看各个核上的情况请使用top命令然后按数字“1”来查看。

  • 相关阅读:
    OpenCV (十二)阈值操作
    OpenCV (十一)图像金字塔
    OpenCV (十)提取水平线与垂直线
    OpenCV (九)形态学操作:开操作,闭操作,形态学梯度,顶帽,黑帽
    电商平台资料学习
    四、eslint配置rules
    三、export和export default区别
    二、安装国际化
    一、vue安装
    vue执行命令时提示错误——vue : 无法加载文件 C:UsersAdministratorAppDataRoaming pmvue.ps1,因为在此系统上禁止运行脚本
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/8202622.html
Copyright © 2011-2022 走看看