CPU线程和进程绑核

  • 前言
  • 一、进程绑核C语言实现
  • 二、线程绑核C语言实现
  • 三、 shell命令绑核

前言

提示:这里可以添加本文要记录的大概内容:

CPU绑定指的是在多核CPU的系统中将进程或线程绑定到指定的CPU核上去执行。在Linux中,我们可以利用CPU affinity属性把进程绑定到一个或多个CPU核上。

CPU Affinity是进程的一个属性,这个属性指明了进程调度器能够把这个进程调度到哪些CPU上。该属性要求进程在某个指定的CPU上尽量长时间地运行而不被迁移到其他处理器。

CPU Affinity分为2种:soft affinity和hard affinity。soft affinity只是一个建议,如果不可避免,调度器还是会把进程调度到其它的CPU上去执行;hard affinity则是调度器必须遵守的规则, 2.6以上版本的Linux内核可以让开发人员可以编程实现hard affinity


一、进程绑核C语言实现

#include <sched.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[])
{cpu_set_t set;int parentCPU, childCPU;int j;int cpu_num = -1;if (argc != 3) {fprintf(stderr, "Usage: %s parent-cpu child-cpu\n", argv[0]);exit(EXIT_FAILURE);}parentCPU = atoi(argv[1]);childCPU = atoi(argv[2]);CPU_ZERO(&set);switch (fork()) {case -1: { /* Error */fprintf(stderr, "fork error\n");exit(EXIT_FAILURE);}case 0: { /* Child */CPU_SET(childCPU, &set);if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) {fprintf(stderr, "child sched_setaffinity error\n");exit(EXIT_FAILURE);}sleep(1);if (-1 != (cpu_num = sched_getcpu())) {fprintf(stdout, "The child process is running on cpu %d, the process id is %d\n", cpu_num, getpid());}usleep(1000 * 1000 * 60);exit(EXIT_SUCCESS);}default: { /* Parent */CPU_SET(parentCPU, &set);if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) {fprintf(stderr, "parent sched_setaffinity error\n");exit(EXIT_FAILURE);}if (-1 != (cpu_num = sched_getcpu())) {fprintf(stdout, "The parent process is running on cpu %d, the process id is %d\n", cpu_num, getpid());}printf("Wait for child to terminate \n");wait(NULL); /* Wait for child to terminate */exit(EXIT_SUCCESS);}}return 0;
}

二、线程绑核C语言实现

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>#define handle_error_en(en, msg) \do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int cpu_num;struct thread_info {pthread_t thread_id;int thread_num;
};static void *thread_start(void *arg) {struct thread_info *tinfo = (struct thread_info *)arg;pthread_t thread = tinfo->thread_id;cpu_set_t cpuset;CPU_ZERO(&cpuset);CPU_SET(tinfo->thread_num, &cpuset);int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);if (s != 0) {handle_error_en(s, "pthread_setaffinity_np");} CPU_ZERO(&cpuset);s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);if (s != 0) {handle_error_en(s, "pthread_getaffinity_np");}for (int j = 0; j < cpu_num; j++) {if (CPU_ISSET(j, &cpuset)) { //如果当前线程运行在CPU j上,则输出信息printf(" thread %d is running on cpu %d\n", tinfo->thread_num, j);}}pthread_exit(NULL);
}int main(int argc, char *argv[])
{cpu_num = sysconf(_SC_NPROCESSORS_CONF); //获取系统的CPU数量struct thread_info *tinfo = (struct thread_info*)calloc(cpu_num, sizeof(struct thread_info));if (tinfo == NULL) {handle_error_en(0, "calloc");}for (int j = 0; j < cpu_num; j++) { //有多少个CPU就创建多少个线程tinfo[j].thread_num = j;int s = pthread_create(&tinfo[j].thread_id, NULL, thread_start, &tinfo[j]);if (s != 0) {handle_error_en(s, "pthread_create");}}for (int j = 0; j < cpu_num; j++) {int s = pthread_join(tinfo[j].thread_id, NULL);if (s != 0) {handle_error_en(s, "pthread_join");}}return 0;
}

三、 shell命令绑核

# 查看某个进程在哪个cpu 上运行
ps -eLo ruser,pid,ppid,psr,args# 查看cpu个数
cat /proc/cpuinfo |grep -c processor# 查看进程所使用的CPU
taskset -cp  25718# 调整进程所使用的CPU
taskset -p 1  25718
taskset -c 1  25718# 使用-p选项指定需要查询的进程号,默认打印的是一个十六进制数,如果使用-cp选项打印的是一个cpu列表,表示相应的cpu核

参考文档
如何将进程、线程与CPU核进行绑定
Linux top 查看CPU、内存、线程等信息

【CPU线程和进程绑核】相关推荐

  1. cpu线程_进程/线程上下文切换会用掉你多少CPU?

    进程是操作系统的伟大发明之一,对应用程序屏蔽了CPU调度.内存管理等硬件细节,而抽象出一个进程的概念,让应用程序专心于实现自己的业务逻辑既可,而且在有限的CPU上可以"同时"进行许 ...

  2. Linux系统给进程绑核

    如何使用 cgroup 的 cpuset 控制器限制进程只使用某几个 CPU,更准确的说是某个几个逻辑核. 参考:https://www.cnblogs.com/shishaochen/p/97351 ...

  3. 【linux 绑核】CPU 绑核

    前言 以下介绍两个用于CPU绑核命令 taskset 适用于已经在运行的程序 numactl 适用于准备运行的程序 目录 1. 命令 taskset 1.1. 查看进程绑核状态 1.2. 指定PID绑 ...

  4. Linux下cpu和绑核

    基本概念 cpu个数 是指物理上cpu的个数. cpu核心数是指物理上,也就是硬件上存在着几个核心.比如,双核就是包括2个相对独立的CPU核心单元组,四核就包含4个相对独立的CPU核心单元组. cpu ...

  5. C/C++线程绑核详解

    在一些大型的工程或者特殊场景中,我们会听到绑核,绑核分为进程绑核和线程绑核.绑核的最终目的都是为了提高程序和性能或者可靠性. 一:为什么需要绑核 操作系统发展至今,已经能很好的平衡运行在操作系统上层的 ...

  6. linux下的绑核命令,Linux下的绑核命令——taskset

    什么是绑核 所谓绑核,其实就是设定某个进程/线程与某个CPU核的亲和力(affinity).设定以后,Linux调度器就会让这个进程/线程只在所绑定的核上面去运行.但并不是说该进程/线程就独占这个CP ...

  7. Linux绑核效率优化

    Linux绑核效率优化 原理概述: cpu一般有多个物理核心,但在运行进程和线程时候,可以将其绑定或者指定到某一个或者多个核心上运行.这样做的好处是:一般在核数比较多的机器上,会有多个CPU共享三级缓 ...

  8. linux内核线程绑定到单个核,linux 将进程或者线程绑定到指定的cpu上

    基本概念 cpu亲和性(affinity) CPU的亲和性, 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器,也称为CPU关联性:再简单的点的描述就将指定的进程或线程绑定到相应的 ...

  9. linux指定cpu运行程序,进程/线程绑定到特定CPU核的linux实现(有代码有实例)

    前言 现在计算机上的CPU大多都是多核的,有4核甚至是8核的.但是一个计算机启动之后其进程数是远远多于CPU核数的,因为操作系统会给自动调度这些进程在CPU核上轮流运行.但是对于应用程序或者进程,其性 ...

最新文章

  1. AI换脸、声音篡改等,明确写入新版《民法典》
  2. FindStringExact
  3. 深度学习框架Caffe, MXNet, TensorFlow, Torch, CNTK性能测试报告
  4. 差值平方和匹配_纯前端实现图片的模板匹配
  5. Java集合框架-概述
  6. 完美替代Mask RCNN!BlendMask:实例分割新标杆
  7. 这样的极客大会千万别停!如今中国太需要为技术传道、为极客正名
  8. 实验3-10 高速公路超速处罚 (15 分)
  9. 去掉input回车自动提交
  10. 400,404,500报错页面总结
  11. 恩佐盒子服务器维护,恩佐宝盒有苹果版吗
  12. C#获取单个字符的拼音声母
  13. 纵横算法之三:算法到底考什么
  14. 2021年山东省安全员C证模拟考试及山东省安全员C证作业模拟考试
  15. win7和ubuntu双系统,直接进入windows启动项选择菜单,而不进入grub的解决方案。
  16. 外汇券商TFS-ICAP因误导客户并使用虚假报告被FCA处罚340万英镑
  17. 页面布局的几种宽度设置方式—html
  18. android 字体加下划线,如何在Android TextView中将字体样式设置为粗体,斜体和下划线?...
  19. echarts画市县乡镇级地图
  20. 郑晓龙新剧携手吴秀波、孙俪 被曝8月将开拍

热门文章

  1. Windows计算器:%号的作用
  2. java (多网卡环境下)发送组播广播(multicast/broadcast)失败问题
  3. 中国十大美女最多学校
  4. 设置Outlook关闭但不退出程序
  5. 【十日谈】将编程的思维用于文件管理
  6. 大学的计算机专业英文,计算机专业大学生英文简历模板
  7. 晨曦记账本记录收支,图表查看收支账目
  8. springboot配置log4j2报错:java.lang.IllegalStateException: Logback configuration error detected:
  9. static,this,private关键字用法
  10. 计算机专业开题报告论证记录如何写,开题论证记录