现在的CPU几乎都是多核,所以,分配给予进程相同数量的线程是合理的需求

但是,这些线程不一定就均匀跑在这些内核上

所以,我们要指派,“一个线程就运行在一个固定的CPU内核上”

//test.c

#define _GNU_SOURCE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

#include <sched.h>

void *myfun(void *arg)

{

cpu_set_t mask;

cpu_set_t get;

char buf[256];

int i;

int j;

int num = sysconf(_SC_NPROCESSORS_CONF);

printf("system has %d processor(s)\n", num);

for (i = 0; i < num; i++) {

CPU_ZERO(&mask);

CPU_SET(i, &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");

}

for (j = 0; j < num; j++) {

if (CPU_ISSET(j, &get)) {

printf("thread %d is running in processor %d\n", (int)pthread_self(), j);

}

}

j = 0;

while (j++ < 100000000) {

memset(buf, 0, sizeof(buf));

}

}

pthread_exit(NULL);

}

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

{

pthread_t tid;

if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) {

fprintf(stderr, "thread create failed\n");

return -1;

}

pthread_join(tid, NULL);

return 0;

}

//result

#gcc -o test.o -c test.c

#gcc -o test test.o -lpthread

#./test

Finally:

这项技术特别有用,不多说了

linux affinity的更多相关文章

  1. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  2. K8S入门系列之必备扩展组件--> coredns(四)

    摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. https://github.com ...

  3. 【原】二进制部署 k8s 1.18.3

    二进制部署 k8s 1.18.3 1.相关前置信息 1.1 版本信息 kube_version: v1.18.3 etcd_version: v3.4.9 flannel: v0.12.0 cored ...

  4. 基于Containerd安装部署高可用Kubernetes集群

    转载自:https://blog.weiyigeek.top/2021/7-30-623.html 简述 Kubernetes(后续简称k8s)是 Google(2014年6月) 开源的一个容器编排引 ...

  5. Linux 多核下绑定硬件中断到不同 CPU(IRQ Affinity) 转

    硬件中断发生频繁,是件很消耗 CPU 资源的事情,在多核 CPU 条件下如果有办法把大量硬件中断分配给不同的 CPU (core) 处理显然能很好的平衡性能.现在的服务器上动不动就是多 CPU 多核. ...

  6. Linux CPU affinity

    在Linux中,我们知道可以通过nice.renice命令改变进程的执行优先级,优先级高的进程优先执行,从而一定程度上保证重要任务的运行. 除了nice.renice外,可以通过CPU  affini ...

  7. linux进程、线程与cpu的亲和性(affinity)

    参考:http://www.cnblogs.com/wenqiang/p/6049978.html 最近的工作中对性能的要求比较高,下面简单做一下总结: 一.什么是cpu亲和性(affinity) C ...

  8. Linux中CPU亲和性(affinity)

    0.准备知识 超线程技术(Hyper-Threading):就是利用特殊的硬件指令,把两个逻辑内核(CPU core)模拟成两个物理芯片, 让单个处理器都能使用线程级并行计算,进而兼容多线程操作系统和 ...

  9. Linux mips64r2 PCI中断路由机制分析

    Linux mips64r2 PCI中断路由机制分析 本文主要分析mips64r2 PCI设备中断路由原理和irq号分配实现方法,并尝试回答如下问题: PCI设备驱动中断注册(request_irq) ...

随机推荐

  1. python多线程应用——DB2数据库备份

    前言:DB2一个实例下,可以存在多个数据库,之前使用shell备份脚本,但是同一时刻只能备份一个数据库,对于几百G的备份文件,这个速度显然太慢,今天学习了Python多线程,刚好应用一下. 分析:1. ...

  2. Pycharm 在Windows下出现闪退问题(即是在运行一段时间后,自己就退出崩掉了)的解决方法

    Pycharm 在Windows下出现闪退问题(即是在运行一段时间后,自己就退出崩掉了)的解决方法 最近自己下载了最新版本的Pycharm,运行程序过程中发现,在运行一段时间后(比如10几分钟),Py ...

  3. JDK 自带的观察者模式源码分析以及和自定义实现的取舍

    前言 总的结论就是:不推荐使用JDK自带的观察者API,而是自定义实现,但是可以借鉴其好的思想. java.util.Observer 接口源码分析 该接口十分简单,是各个观察者需要实现的接口 pac ...

  4. Debian/Ubuntu pip default install to $HOME/.local

    pip default install to $HOME/.local on Debian/Ubuntu After pip 8.1.1-2 on Debian or Ubuntu you can p ...

  5. python使用requests发送text/xml报文数据

    def client_post_xmldata_requests(request_url,requestxmldata): #功能说明:发送请求报文到指定的地址并获取请求响应报文 #输入参数说明:接收 ...

  6. ubuntu16.04英文版搜狗输入法安装报错

    1.因为是英文版的,所以需要更新中文字体 Systems Settings>Language Support ,会提示自动更新,这个时候KeyBorad input method 选择不了fci ...

  7. iowait

    https://www.cnblogs.com/fuyuanming/articles/6497005.html

  8. LeetCode - 503. Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  9. sublime3 快速生成html头文件

    通过安装emmt插件老师来实现该功能(https://github.com/sergeche/emmet-sublime#readme) 1. 下载好后解压到文件夹: 2. 打开TS3,点击perfe ...

  10. Javaweb实现对mongodb的增删改查(附带源代码)

    运行截图: 删除后的信息 项目源代码:https://www.cnblogs.com/post/readauth?url=/zyt-bg/p/9807396.html