看到很多程序都是根据CPU个数来创建线程个数,当时很不理解他们之间的关系,请教了项目组的同事后才有了大致了解。

 1. 相关系统函数

下面的函数可以通过man命令查询到。

SYNOPSIS#define _GNU_SOURCE#include <pthread.h>int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,const cpu_set_t *cpuset);int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,cpu_set_t *cpuset);Compile and link with -pthread.
DESCRIPTIONThe  pthread_setaffinity_np()  sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset.  If the call is successful, and the thread isnot currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.The pthread_getaffinity_np() function returns the CPU affinity mask of the thread thread in the buffer pointed to by cpuset.For more details on CPU affinity masks, see sched_setaffinity(2).  For a description of a set of macros that can be used to manipulate and inspect CPU sets,  seeCPU_SET(3).The  argument cpusetsize is the length (in bytes) of the buffer pointed to by cpuset.  Typically, this argument would be specified as sizeof(cpu_set_t).  (It maybe some other value, if using the macros described in CPU_SET(3) for dynamically allocating a CPU set.)
RETURN VALUEOn success, these functions return 0; on error, they return a non-zero error number.
简而言之,这两个函数一个设置在哪个CPU核上运行,另一个获取设置的参数(mask),既可以查询出当前进程在运行在哪个核上CPU_ZERO()       Clears set, so that it contains no CPUs.设置为空,没有任何CPUCPU_SET()        Add CPU cpu to set.将某个核加入CPU集合CPU_CLR()        Remove CPU cpu from set.将某个核清理出CPU集合CPU_ISSET()      Test to see if CPU cpu is a member of set.判断某个核是否在CPU集合中设置。cpu集合可以认为是一个掩码,每个设置的位都对应一个可以合法调度的 cpu,而未设置的位则对应一个不可调度的 CPU。换而言之,线程都被绑定了,只能在那些对应位被设置了的处理器上运行。通常,掩码中的所有位都被置位了,也就是可以在所有的cpu中调度。CPU_COUNT()      Return the number of CPUs in set.

 2.  下面是我的测试代码。

测试环境:

系统: SUSE 10

linux 内核版本:2.6.32.12

CPU: 8核

        2.1  根据CPU核个数创建线程,并绑定

void *myfunWithMultiThread(void *arg){cpu_set_t mask;cpu_set_t get;int i = 0;int num = 0;int cpuID = *(int *)arg;CPU_ZERO(&mask);CPU_SET(cpuID, &mask);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");}num = sysconf(_SC_NPROCESSORS_CONF);for (i = 0; i < num; i++) {if (CPU_ISSET(i, &get)) {printf("thread %d is running in processor %d\n", (int)pthread_self(), i);printf("Original setting is in processor  %d\n\n", cpuID);}}sleep(10);}int main(int argc, char *argv[]){pthread_t tid;int num = 0;int i =0;num = sysconf(_SC_NPROCESSORS_CONF);int id[16];printf("System has %d processor(s),so create %d threads\n", num,num);for(i = 0; i < num; i++){id[i] = i;if (pthread_create(&tid, NULL, (void *)myfunWithMultiThread, (void *)&id[i]) != 0){fprintf(stderr, "thread create failed\n");return -1;}}pthread_join(tid, NULL);return 0;}

运行结果:

System has 8 processor(s),so create 8 threads
thread 1188759312 is running in processor 5
Original setting is in processor  5
thread 1205544720 is running in processor 3
Original setting is in processor  3
thread 1197152016 is running in processor 4
Original setting is in processor  4
thread 1230722832 is running in processor 0
Original setting is in processor  0
thread 1213937424 is running in processor 2
Original setting is in processor  2
thread 1222330128 is running in processor 1
Original setting is in processor  1
thread 1180366608 is running in processor 6
Original setting is in processor  6
thread 1171973904 is running in processor 7
Original setting is in processor  7

2.2 不绑定测试代码

System has 8 processor(s),so create 8 threads
thread -196520176 is running in processor 0
thread -196520176 is running in processor 1
thread -196520176 is running in processor 2
thread -196520176 is running in processor 3
thread -196520176 is running in processor 4
thread -196520176 is running in processor 5
thread -196520176 is running in processor 6
thread -196520176 is running in processor 7
thread -221698288 is running in processor 0
thread -221698288 is running in processor 1
thread -221698288 is running in processor 2
thread -221698288 is running in processor 3
thread -221698288 is running in processor 4
thread -221698288 is running in processor 5
thread -221698288 is running in processor 6
thread -221698288 is running in processor 7
thread -213305584 is running in processor 0
thread -213305584 is running in processor 1
thread -213305584 is running in processor 2
thread -213305584 is running in processor 3
thread -213305584 is running in processor 4
thread -213305584 is running in processor 5
thread -213305584 is running in processor 6
thread -213305584 is running in processor 7
thread -230090992 is running in processor 0
thread -230090992 is running in processor 1
thread -230090992 is running in processor 2
thread -204912880 is running in processor 0
thread -204912880 is running in processor 1
thread -204912880 is running in processor 2
thread -238483696 is running in processor 0
thread -230090992 is running in processor 3

由以上结果可以看出,linux是不会默认对线程进行和CPU进行绑定的,如果有需要必须自己显式的调用函数绑定。

3. 结论

线程与CPU进行绑定可以减少线程在不同核之间切换的开销,但是要遵循一定的规则,并不是每个线程都与特定CPU绑定效率高。一般来说计算密集型的程序与CPU绑定与不绑定相比效果要好得多,但对于IO密集型的程序来说,影响不是太大。

转载于:https://blog.51cto.com/0987654321/1409979

linux 线程与CPU绑定相关推荐

  1. 线程锁定CPU linux,linux 线程与CPU绑定

    看到很多程序都是根据CPU个数来创建线程个数,当时很不理解他们之间的关系,请教了项目组的同事后才有了大致了解. 1. 相关系统函数 下面的函数可以通过man命令查询到. SYNOPSIS #defin ...

  2. linux 指定cpu运行线程,linux 线程与CPU绑定

    看到很多程序都是根据CPU个数来创建线程个数,当时很不理解他们之间的关系,请教了项目组的同事后才有了大致了解. 1. 相关系统函数 下面的函数可以通过man命令查询到.SYNOPSIS #define ...

  3. linux看线程占用率,查看linux线程的CPU占用率

    测试代码如下: #include #include void *thread_routine(void *arg) { prctl(PR_SET_NAME, "child_thread&qu ...

  4. Linux 线程占用CPU过高定位分析

    今天朋友问我一个Linux程序CPU占用涨停了,该如何分析, CPU占用过高,模拟CPU占用过高的情况 先上一段代码: 1 #include <iostream> 2 #include & ...

  5. Linux -- 进程或线程独占CPU

    如果想让特定进程或线程独占某一或某些CPU,我们需要做三件事. 一,隔离CPU,避免其它线程run在被隔离的CPU上. 二,绑定所有的interrupts到非隔离的CPU上,避免被隔离的CPU收到in ...

  6. Linux线程性能分析和CPU亲和力

    一,线程迁移和负载均衡 Linux系统在多核CPU和SMP系统上有完善的负载均衡支持.在SMP系统中,每个CPU的核都有一个迁移线程守护程序migration(一般是系统最高优先级139,实时99), ...

  7. linux 指定cpu运行线程,关于linux:如何查看运行线程的CPU核心?

    在Linux中,假设线程的pid是[pid],从目录/ proc / [pid]我们可以获得许多有用的信息. 例如,这些proc文件,/ proc / [pid] / status,/ proc / ...

  8. Linux设置进程CPU亲和力(核心绑定)

    文章目录 1. 获取CPU核数 2. 线程绑定CPU核心 ① 概念 ② 函数 ③ 例子 1. 获取CPU核数 int CPU_NUM = sysconf(_SC_NPROCESSORS_CONF); ...

  9. 线程或进程绑定到特定的cpu

    常用的宏定义有: 1) 对cpu集进行初始化, 将其设置为空集 void CPU_ZERO(cpu_set_t *set); 2) 将指定的cpu加入到cpu集中 void CPU_SET(int c ...

最新文章

  1. 巧妙的 排序+去重——C语言
  2. 拿到了B轮融资,但这家创业公司还是被天使投资人玩死了
  3. Spring源码解析之:Spring Security启动细节和工作模式--转载
  4. [原]HTML5系列培训一(20140326)
  5. js中使用camel框架_使用Fabric8在Kubernetes中使用Camel和CDI
  6. flutter text 最大长度_Flutter小技巧之TextField换行自适应
  7. (Mark)JS中的上下文
  8. 立镖机器人浙江_立镖现身LogiMAT 2019 彰显中国仓储分拣技术
  9. 多少并发量算高并发_Linux服务端最大并发数是多少?
  10. 剑指offer——面试题22:栈的压入、弹出序列
  11. postgress无法远程连接问题解决方案
  12. 简单个人静态HTML网页设计作品——广西北海家乡旅游景点 10页 DIV布局个人介绍网页模板代码 DW个人网站制作成品 web网页制作与实现
  13. 微型计算机的中央处理器由什么组成,中央处理器由什么组成?
  14. Dalvik字节码和Smali基本语法
  15. Git从远程仓库克隆
  16. 防火墙阻止tftp_H3C防火墙常见问题汇总
  17. Vue 数组删除和修改元素后页面立即刷新
  18. 对极验geetest滑块验证码图片还原算法的研究
  19. How to make a ipcamera
  20. 【人工智能项目】- 卷积神经网络实现游客评价情绪鉴别

热门文章

  1. 机器学习入门|快速掌握逻辑回归模型
  2. OpenAI联合创始人:AI的极限?我真的不知道!
  3. 数据标注-人工智能高速路上的基石
  4. 「AlphaGo 之父」David Silver最新演讲,传授强化学习的十大原则
  5. SSH连接问题:1.Software caused connection abort2.client_loop: send disconnect: Connection reset
  6. 程序员带半箱辣条参加东京奥运,网友:这不是辣条,是狗粮!
  7. 雷军狂撒 20 亿 ,给小米、金山员工豪派“大红包”,网友:又是别人家的公司!...
  8. AI 迎来重要发展契机,开发者的机会在哪里?
  9. “硅谷之父”传奇:拯救斯坦福大学、培养大批高科技人才、指导创立惠普
  10. 人间真实!如果我有这套装备,我也能码到凌晨...... | 每日趣闻