我想指定特定pthread的cpu亲和力。 到目前为止,我发现的所有引用都涉及设置进程(pid_t)而不是线程(pthread_t)的cpu亲和力。 我尝试了一些传递pthread_t的实验,并且按预期它们会失败。 我是否在尝试做一些不可能的事情? 如果没有,您可以发送指针吗? 太感谢了。

这是我为了使生活更轻松而制作的包装纸。其作用是使调用线程"停留"在ID为core_id的内核上:

// core_id = 0, 1, ... n-1, where n is the system's number of cores

int stick_this_thread_to_core(int core_id) {

int num_cores = sysconf(_SC_NPROCESSORS_ONLN);

if (core_id < 0 || core_id >= num_cores)

return EINVAL;

cpu_set_t cpuset;

CPU_ZERO(&cpuset);

CPU_SET(core_id, &cpuset);

pthread_t current_thread = pthread_self();

return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);

}

供将来参考:需要添加#define _GNU_SOURCE和#include 才能在gcc 4.7.2上工作。在arch Linux上完美工作,并通过oprofile和pthread测试。

此外,sysconf和gcc 4.8.1都需要#include。

由于某种原因,它可以在具有两个内核的计算机上运行,??但是在具有四个内核的另一台计算机上,则会出现以下错误:

Segmentation fault (core dumped)

尼斯。除了core_id> num_cores失败外,在这种情况下,另一个参数可以指定默认值:core_id = default_core; -1作为默认值可能意味着失败。

什么是使用此代码或以下@nos答案中的sched_setaffinity更好的方法?

@javapowered:两者都是一样的-pthread_setaffinity_np只是sched_setaffinity()的包装。

我没有将内核转储,但是如果我在单个内核上执行优先级线程,结果将不一致。

假设Linux:

设置相似性的界面是-您可能已经发现:

int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);

传递0作为pid,它将仅适用于当前线程,或者让其他线程使用linux特定的调用pid_t gettid(void);报告其内核pid并将其作为pid传递。

引用手册页

The affinity mask is actually a per-thread attribute that can be

adjusted independently for each of the

threads in a thread group. The value

returned from a call to gettid(2) can

be passed in the argument pid.

Specifying pid as 0 will set the

attribute for the calling thread, and

passing the value returned from a call

to getpid(2) will set the attribute

for the main thread of the thread

group. (If you are using the POSIX

threads API, then use

pthread_setaffinity_np (3) instead of

sched_setaffinity().)

"如果使用的是POSIX线程API,请使用pthread_setaffinity_np(3)而不是sched_setaffinity()"。我怎么知道即时通讯是否使用POSIX API?如何选择使用sched_setaffinity或pthread_setaffinity_np?

在RHEL 7中,这就是人所说的If pid is zero, then the calling process is used.(进程,不是线程)

@javapowered手册页中的那句话是错误的。也阅读"注意"部分。

我遇到了同样的问题,但是我正在使用OSX。是否有类似的方法?

//compilation: gcc -o affinity affinity.c -lpthread

#define _GNU_SOURCE

#include   //cpu_set_t , CPU_SET

#include //pthread_t

#include

void *th_func(void * arg);

int main(void) {

pthread_t thread; //the thread

pthread_create(&thread,NULL,th_func,NULL);

pthread_join(thread,NULL);

return 0;

}

void *th_func(void * arg)

{

//we can set one or more bits here, each one representing a single CPU

cpu_set_t cpuset;

//the CPU we whant to use

int cpu = 2;

CPU_ZERO(&cpuset);       //clears the cpuset

CPU_SET( cpu , &cpuset); //set CPU 2 on cpuset

/*

* cpu affinity for the calling thread

* first parameter is the pid, 0 = calling thread

* second parameter is the size of your cpuset

* third param is the cpuset in which your thread will be

* placed. Each bit represents a CPU

*/

sched_setaffinity(0, sizeof(cpuset), &cpuset);

while (1);

; //burns the CPU 2

return 0;

}

在POSIX环境中,可以使用cpusets进行控制

进程或pthread可以使用哪些CPU。

这种类型的控制称为CPU关联。

函数" sched_setaffinity"接收pthread ID和

一个cpuset作为参数。

当您在第一个参数中使用0时,调用线程

会受到影响

调度程序将根据需要更改cpu关联性;要永久设置它,请参阅/ proc文件系统中的cpuset。

http://man7.org/linux/man-pages/man7/cpuset.7.html

或者,您可以编写一个简短的程序,使用sched_setaffinity定期(每隔几秒钟)设置cpu亲和力

请在下面的示例程序中找到特定pthread的cpu-affinity。

请添加适当的库。

double waste_time(long n)

{

double res = 0;

long i = 0;

while (i

i++;

res += sqrt(i);

}

return res;

}

void *thread_func(void *param)

{

unsigned long mask = 1; /* processor 0 */

/* bind process to processor 0 */

if (pthread_setaffinity_np(pthread_self(), sizeof(mask),

&mask) <0) {

perror("pthread_setaffinity_np");

}

/* waste some time so the work is visible with"top" */

printf("result: %f

", waste_time(2000));

mask = 2;   /* process switches to processor 1 now */

if (pthread_setaffinity_np(pthread_self(), sizeof(mask),

&mask) <0) {

perror("pthread_setaffinity_np");

}

/* waste some more time to see the processor switch */

printf("result: %f

", waste_time(2000));

}

int main(int argc, char *argv[])

{

pthread_t my_thread;

if (pthread_create(&my_thread, NULL, thread_func, NULL) != 0) {

perror("pthread_create");

}

pthread_exit(NULL);

}

使用-D_GNU_SOURCE标志编译以上程序。

您的程序可以运行,但是我看到几个问题:1)pthread_setaffinity_np需要一个cpu_set_t,而不是无符号的long。在传递给相似性函数之前,应该使用CPU_SET,CPU_ZERO等宏来操作掩码2)最后,您不需要使用pthread_create启动新线程即可运行代码的主要部分

linux c设置cpu亲和力,关于多线程:如何设置特定pthread的CPU亲和力?相关推荐

  1. 设置cpu亲和性_如何快速设置一个任务的CPU亲和力?

    设置cpu亲和性 How to set one task's CPU affinity quickly? 如何快速设置一个任务的CPU亲和力? 1, Get this task's ID 1,获取此任 ...

  2. php多线程多核,Linux查看CPU个数/多核/多线程的查看

    在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 过查看,我的开发机器是1个物理CPU ...

  3. linux查看cpupower模式,CPU优化建议使用cpupower设置CPU Performance模式

    前言 CPU动态节能技术用于降低服务器功耗,通过选择系统空闲状态不同的电源管理策 略,可以实现不同程度降低服务器功耗,更低的功耗策略意味着CPU唤醒更慢对性能 影响更大.对于对时延和性能要求高的应用, ...

  4. 面试官:单核 CPU 支持 Java 多线程吗?什么?

    由于现在大多计算机都是多核CPU,多线程往往会比单线程更快,更能够提高并发,但提高并发并不意味着启动更多的线程来执行.更多的线程意味着线程创建销毁开销加大.上下文非常频繁,你的程序反而不能支持更高的T ...

  5. 【Linux 性能优化系列】Linux 性能优化 -- CPU 性能篇(一) 平均负载、上下文切换、CPU 使用率

    [Linux 性能优化系列]Linux 性能优化 -- CPU 性能篇(一) 平均负载.上下文切换.CPU 使用率 [1]相关概念 [1.1]平均负载 平均负载是指单位时间内,系统处于可运行状态和不可 ...

  6. linux与windows下tomcat的java内存设置

    Linux下修改JVM内存大小: 要添加在tomcat 的bin 下catalina.sh文件中,找到cygwin=false,在这一行的前面加入参数,具体如下# vi TOMCAT_HOME/bin ...

  7. CPU核数跟多线程的关系

    CPU核数跟多线程的关系 时间 2014-03-04 20:06:07 CSDN博客 原文  http://blog.csdn.net/qingchen191/article/details/2047 ...

  8. java多线程 cpu分配_java多线程总结(转载)

    Java 多线程编程总结 --------------------------------------------------------------------------------------- ...

  9. python GIL 全局锁,多核cpu下的多线程性能究竟如何?

    python GIL 全局锁,多核cpu下的多线程性能究竟如何?GIL全称Global Interpreter Lock GIL是什么? 首先需要明确的一点是GIL并不是Python的特性,它是在实现 ...

最新文章

  1. Go复盘--再识Go语言
  2. php js 比较大小写,JavaScript中如何实现大小写转换
  3. flutter 怎么拦截请求_flutter中事件传递:禁止用户交互 获取点击事件
  4. linux驱动内核哪个文件夹,linux设备驱动归纳总结(一):内核的相关基础概念...
  5. C++设计模式之四 模板模式
  6. 打造 Microsoft Windows Server 2008 R2 SP1 支持的 Dell 桌面虚拟化解决方案
  7. java stream过滤_Java Stream过滤器
  8. 详解机器翻译前沿技术及落地应用
  9. 基于Silverlight4开发的相关工具
  10. YOLO系列详解:YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5
  11. BeanShell用法笔记
  12. python回归模型调优要运行多久_python机器学习模型参数调优
  13. 高级设计总监的设计方法论——5W1H需求分析法 KANO模型分析法
  14. 常用颜色透明度色值表
  15. python语言支持中文输出_python2输出汉字的解决办法暨python2/python3的编码环境参数的查看-Go语言中文社区...
  16. expect 中的回车和换行
  17. 中国艺术孙溟㠭书画《光》
  18. python网络爬虫(第八章:图像识别与文字处理)
  19. 数据结构PTA习题:基础实验7-2.3 德才论 (25分)——排序
  20. 【数学建模】最小二乘回归+Java代码实现

热门文章

  1. Spotify Music Converter for Mac如何注册?
  2. 5类6类7类网线对比_五类,六类,七类网线都有什么区别
  3. 机器人运动控制-上位机通讯
  4. 如何在Oracle官网上找到以前版本的JDK?
  5. 第二届天府大地艺术季(春)分会场暨晨光社区系列主题活动开启
  6. JAVA WEB实现图书管理系统 —— 主页面
  7. 视频下载工具 lux
  8. Unity中使用调用Shell的命令行
  9. 第五人格服务器维修是什么意思,第五人格重新连接服务器是什么原因 第五人格重新连接服务器失败怎么办...
  10. 阿里云域名免费申请https证书 宝塔配置ssl