当前位置:网站首页>CPU的亲缘性affinity
CPU的亲缘性affinity
2022-08-05 09:13:00 【cheems~】
CPU的亲缘性affinity
前言
本文简单介绍一下CPU亲缘性以及实现方法。
本专栏知识点是通过零声教育的线上课学习,进行梳理总结写下文章,对c/c++linux课程感兴趣的读者,可以点击链接 C/C++后台高级服务器课程介绍 详细查看课程的服务。
何谓亲缘性
亲缘性的作用就是把线程or进程与CPU做黏合,也就是说,做了亲缘性的线程或进程,只会在这一个CPU核上运行,只在这一个CPU核上被调度,且不会切换到其他的CPU核上运行。这就是亲缘性。
亲缘性API介绍
cpu_set_t mask:创建CPU核位图CPU_ZERO(&mask):将位图置空CPU_SET(self_id % num, &mask):将位图的某一位置1sched_setaffinity(self_id, sizeof(mask), &mask):将对应进程或线程绑定到置1的这个CPU核上
测试
八个进程绑定八个CPU核心
//
// Created by 68725 on 2022/8/4.
//
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/mman.h>
#include <sched.h>
void process_affinity(int num) {
pid_t self_id = getpid();
// pid_t self_id = syscall(__NR_gettid);
cpu_set_t mask;
CPU_ZERO(&mask);
//绑定某个CPU核心
CPU_SET(self_id % num, &mask);
//设置亲缘性
// sched_setaffinity(0, sizeof(mask), &mask);
sched_setaffinity(self_id, sizeof(mask), &mask);
while (1);
}
int main() {
//获取cpu数量
int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
int i;
pid_t pid = 0;
//创建与CPU数量一样多的进程
for (i = 0; i < cpu_num; i++) {
pid = fork();
if (pid == (pid_t) 0) {
break;
}
}
if (pid == 0) {
process_affinity(cpu_num);
}
while (1)usleep(1);
}
可以看到瞬间跑满了


八个进程绑定四个CPU核心
//
// Created by 68725 on 2022/8/4.
//
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/mman.h>
#include <sched.h>
void process_affinity(int num) {
pid_t self_id = getpid();
// pid_t self_id = syscall(__NR_gettid);
cpu_set_t mask;
CPU_ZERO(&mask);
//绑定某个CPU核心
CPU_SET(self_id % num, &mask);
//设置亲缘性
// sched_setaffinity(0, sizeof(mask), &mask);
sched_setaffinity(self_id, sizeof(mask), &mask);
while (1);
}
int main() {
//获取cpu数量
int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
int i;
pid_t pid = 0;
//创建与CPU数量一样多的进程
for (i = 0; i < cpu_num; i++) {
pid = fork();
if (pid == (pid_t) 0) {
break;
}
}
if (pid == 0) {
process_affinity(4);
}
while (1)usleep(1);
}
可以看到瞬间4个CPU核跑满


八个进程绑定一个CPU核心
//
// Created by 68725 on 2022/8/4.
//
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/mman.h>
#include <sched.h>
void process_affinity(int num) {
pid_t self_id = getpid();
// pid_t self_id = syscall(__NR_gettid);
cpu_set_t mask;
CPU_ZERO(&mask);
//绑定某个CPU核心
CPU_SET(self_id % num, &mask);
//设置亲缘性
// sched_setaffinity(0, sizeof(mask), &mask);
sched_setaffinity(self_id, sizeof(mask), &mask);
while (1);
}
int main() {
//获取cpu数量
int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
int i;
pid_t pid = 0;
//创建与CPU数量一样多的进程
for (i = 0; i < cpu_num; i++) {
pid = fork();
if (pid == (pid_t) 0) {
break;
}
}
if (pid == 0) {
process_affinity(1);
}
while (1)usleep(1);
}


边栏推荐
猜你喜欢
随机推荐
阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
为什么我推荐使用智能化async?
让程序员崩溃的N个瞬间(非程序员误入)
There is only one switch, how to realize the nqa of master-slave automatic switching
微信小程序请求封装
Luogu P3368: 【模板】树状数组 2
sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
Two-table query average grouping in sql server
如何实现按键的短按、长按检测?
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
The difference between beautiful MM and ordinary MM
selectPage 动态改变参数方法
Why is pnpm hitting npm and yarn dimensionality reduction?
Does flink cdc support synchronization from oracle dg library?
tear apart loneliness
ts/js 函数传参带函数写法
Why do I recommend using smart async?
随时牵手 不要随意分手[转帖]
express hot-reload
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系









