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

测试代码:

[cpp] view plain copy

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

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

执行:./bind

输出结果:略

特别注意:

#define __USE_GNU不要写成#define _USE_GNU

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

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

原文:https://blog.csdn.net/lanyzh0909/article/details/50404664

线程绑定CPU核-sched_setaffinity的更多相关文章

  1. Ubuntu系统进程绑定CPU核

    Ubuntu系统进程绑定CPU核 作者:chszs.版权全部,未经允许,不得转载. 博主主页:http://blog.csdn.net/chszs 本文讲述如何在Ubuntu系统中,把指定的进程绑定到 ...

  2. Linux编程之《进程/线程绑定CPU》

    Intro----- 通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上. /***** ...

  3. taskset -pc PID 查看线程占用cpu核

    taskset -pc  PID 可以用于 查看 当前线程 对应绑定的 在 哪个核上面. 这个 可以用于 程序优化, 查看 哪个线程占用的 cpu 比重比较高 首先 可以通过  top  -H   - ...

  4. linux线程绑定cpu

    函数介绍 #define __USE_GNU #include <sched.h> void CPU_ZERO(cpu_set_t *set); void CPU_SET(int cpu, ...

  5. 为线程绑定CPU

    // learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...

  6. 线程绑定cpu

    #include <stdio.h> #include <pthread.h> #include <sys/sysinfo.h> #include <unis ...

  7. Windows10 临时将线程绑定至指定CPU的方法

    本文首发:https://www.somata.work/2019/WindowsThreadBind.html 将线程绑定至指定CPU,这个应该时很多管理员需要了解认知的操作了吧,这样可以在一定程度 ...

  8. NGINX源代码剖析 之 CPU绑定(CPU亲和性)

    作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自&quo ...

  9. linux下将不同线程绑定到不同core和cpu上——pthread_setaffinity_np

    =============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主 ...

随机推荐

  1. GroupBox与Panel控件

    1.GroupBox控件常常用于逻辑地组合一组控件,如RadioButton 及 CheckBox控件,显示一个框架,其上有一个标题. 2.Panel 可以包含多个控件,以便将这些控件编为一组,以便方 ...

  2. ubuntu16.04 下安装opencv2.4.9

    准备工作,安装环境 sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-nump ...

  3. Anaconda2

    Anaconda 是一个打包的python,一次把好多需要的包都安装好了.对于Python2.7把PyQt5都弄好了,不需要自己来编译! 看看这个 http://conda.pydata.org/do ...

  4. sql 注入 与解决

    package cn.itcast.jdbc; import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLExce ...

  5. Getting Started with the G1 Garbage Collector(译)

    原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...

  6. WinForm程序打包工具InnoSetup使用说明图文教程

    WinForm程序打包工具InnoSetup使用说明图文教程 WinForm程序开发测试好了,如果将Debug/Release里面的文件发给客户使用,会让客户觉得你不够专业,但是使用VS自带的打包工具 ...

  7. go with go

    1, vim 安装vim-go 打造GOLANG 专用IDE golang和vim-go安装配置 2, 阅读图书 <Go语言实战> William Kennedy等, 李兆海 译 3,在线 ...

  8. Python 单元测试 之setUP() 和 tearDown()

    setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...

  9. 计算机网络 --万维网www

    万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...

  10. MongoDB的对象的创建

    package com.voice.db; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mon ...