当前位置: 首页 > news >正文

linux 进程/线程设置核亲和性

1,线程绑定内核

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

// 定义一个函数,用于设置线程的CPU亲和性
void set_thread_affinity(pthread_t thread, int cpu_id) {
    cpu_set_t cpuset;
    int s;

    // 清空CPU集合并添加指定的CPU
    CPU_ZERO(&cpuset);
    CPU_SET(cpu_id, &cpuset);

    // 设置线程的CPU亲和性
    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0) {
        perror("pthread_setaffinity_np");
        exit(EXIT_FAILURE);
    }

    // 验证设置是否成功
    s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0) {
        perror("pthread_getaffinity_np");
        exit(EXIT_FAILURE);
    }

    // 打印CPU亲和性设置结果
    for (int i = 0; i < CPU_SETSIZE; i++) {
        if (CPU_ISSET(i, &cpuset)) {
            printf("Thread %lu is running on CPU %d\n", thread, i);
        }
    }
}

// 线程函数
void* thread_function(void* arg) {
    // 设置本线程的CPU亲和性到CPU 0
    set_thread_affinity(pthread_self(), 0);

    // 执行线程的工作...
    return NULL;
}

int main() {
    pthread_t thread;

    // 创建线程
    if (pthread_create(&thread, NULL, thread_function, NULL) != 0) {
        perror("pthread_create");
        return 1;
    }

    // 等待线程结束
    if (pthread_join(thread, NULL) != 0) {
        perror("pthread_join");
        return 1;
    }

    return 0;
}

设置本线程的CPU亲和性到CPU 0使用的函数是 set_thread_affinity(pthread_self(), 0),使用CPU_ZEROCPU_SET宏来初始化一个CPU集合并设置亲和性掩码。

 2,进程绑定内核,绑定某个进程到某一个核(核亲和性)

#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>

int main() {
    cpu_set_t cpuset;
    pid_t pid;

    // 获取当前进程的PID
    pid = getpid();

    // 清空CPU集合并设置亲和性掩码
    CPU_ZERO(&cpuset);
    // 假设我们要绑定到第0个CPU核心上
    CPU_SET(0, &cpuset);

    // 设置当前进程的CPU亲和性
    if (sched_setaffinity(pid, sizeof(cpuset), &cpuset) == -1) {
        perror("sched_setaffinity");
        exit(EXIT_FAILURE);
    }

    // 打印进程ID和设置的CPU亲和性
    printf("Process %d bound to CPU %d\n", pid, 0);

    // 为了演示效果,让进程睡眠一段时间
    sleep(60);

    return EXIT_SUCCESS;
}

查看绑定效果,taskset -p <PID>

taskset -p 688

pid 688's current affinity mask: 1

http://www.dtcms.com/a/111239.html

相关文章:

  • MySQL统计信息
  • JS dom修改元素的style样式属性
  • 删除Linux服务器上多余的系统启动项,并重装Ubuntu系统
  • Java 连接 WebSocket 入门教程
  • 【Web 服务器】的工作原理
  • 第十八节课:Python编程基础复习
  • wx206基于ssm+vue+uniapp的优购电商小程序
  • NLP高频面试题(三十五)——LLaMA / ChatGLM / BLOOM的区别
  • AI + 慢病逆转 1
  • USB传输(Transaction)过程简介
  • swift-oc和swift block和代理
  • ElasticSearch JavaRestClient查询之高亮显示
  • JS用ES6和ES5分别实现:8字节长整数和字节数组的互转
  • 软考系统架构师 — 4 嵌入式软件
  • H.266/VVC SCC技术学习:块差分脉冲编码调整(block differential pulse coded modulation, BDPCM)
  • 生信入门:专栏概要与内容目录
  • AI算法大全初见面
  • Redisson使用详解
  • 《Maven高级应用:继承聚合设计与私服Nexus实战指南》
  • 嵌入式学习笔记——SPI协议
  • “一路有你”公益行携手《东方星动》走进湖南岳阳岑川镇中心小学
  • AI Agent设计模式二:Parallelization
  • 【新能源汽车整车动力学模型深度解析:面向MATLAB/Simulink仿真测试工程师的硬核指南】
  • PyTorch:解锁AI新时代的钥匙
  • Python基于时间序列分析的降雨量预测系统的设计与实现【附源码、文档说明】
  • 一周学会Pandas2 Python数据处理与分析-Jupyter Notebook安装
  • C++类的特殊成员函数:构造、拷贝构造与析构函数详解
  • F#语言的折线图
  • Prolog语言的强化学习
  • MySQL 知识点详解(索引、存储引擎、事务、锁机制、优化)