Linux进程或线程绑定到CPU
Linux进程或线程绑定到CPU
为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程。
进程绑定到CPU
Linux提供一个接口,可以将进程绑定到特定的CPU:
#include <sched.h>
int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
参数
pid:进程的id号,如果pid为0,则表示本进程
cpusetsize:mask的大小
mask:运行进程的CPU,可以通过以下函数操作mask
#define CPU_SET(cpu, cpusetp) //设置cpu
#define CPU_CLR(cpu, cpusetp) //删除cpu
#define CPU_ISSET(cpu, cpusetp) //判断cpu
#define CPU_ZERO(cpusetp) //初始化为0
示例代码
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sched.h> void WasteTime()
{
int abc = ;
while(abc--)
{
int tmp = *;
}
sleep(); } int main(int argc, char **argv)
{
cpu_set_t mask;
while()
{ CPU_ZERO(&mask);
CPU_SET(, &mask);
if (sched_setaffinity(, sizeof(mask), &mask) < ) {
perror("sched_setaffinity");
}
WasteTime(); CPU_ZERO(&mask);
CPU_SET(, &mask);
if (sched_setaffinity(, sizeof(mask), &mask) < ) {
perror("sched_setaffinity");
}
WasteTime(); CPU_ZERO(&mask);
CPU_SET(, &mask);
if (sched_setaffinity(, sizeof(mask), &mask) < ) {
perror("sched_setaffinity");
}
WasteTime(); CPU_ZERO(&mask);
CPU_SET(, &mask);
if (sched_setaffinity(, sizeof(mask), &mask) < ) {
perror("sched_setaffinity");
}
WasteTime();
}
}
测试
编译之后运行程序,输入命令top -p 进程id,输入f,输入j,输入回车,可以看到进程在cpu0123之间不停切换。

线程绑定到CPU
不仅仅进程可以绑定到CPU,线程也可以。Linux提供一个接口,可以将线程绑定到特定的CPU:
#include <pthread.h>
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);
该接口与进程绑定到CPU的接口的使用方法基本一致。
当进程绑定到特定的CPU之后,线程还是可以绑定到其他的CPU的,没有冲突。
示例代码
#include <stdio.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h> void WasteTime()
{
int abc = ;
while(abc--)
{
int tmp = *;
}
sleep(); } void *thread_func(void *param)
{
cpu_set_t mask;
while()
{
CPU_ZERO(&mask);
CPU_SET(, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
&mask) < ) {
perror("pthread_setaffinity_np");
} WasteTime(); CPU_ZERO(&mask);
CPU_SET(, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
&mask) < ) {
perror("pthread_setaffinity_np");
} WasteTime();
}
} void *thread_func1(void *param)
{
cpu_set_t mask;
while()
{
CPU_ZERO(&mask);
CPU_SET(, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
&mask) < ) {
perror("pthread_setaffinity_np");
} WasteTime(); CPU_ZERO(&mask);
CPU_SET(, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
&mask) < ) {
perror("pthread_setaffinity_np");
} WasteTime();
}
} int main(int argc, char *argv[])
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(, &mask);
if (sched_setaffinity(, sizeof(mask), &mask) < ) {
perror("sched_setaffinity");
} pthread_t my_thread; if (pthread_create(&my_thread, NULL, thread_func,
NULL) != ) {
perror("pthread_create");
}
if (pthread_create(&my_thread, NULL, thread_func1,
NULL) != ) {
perror("pthread_create");
}
while() { WasteTime(); }
pthread_exit(NULL); }
测试
编译运行之后,输入命令top -p 进程id,输入f,输入j,输入回车,输入H,可以看到主线程一直保持在cpu0,一个线程在cpu12之前切换,另一个线程在cpu34之间切换。

Linux进程或线程绑定到CPU的更多相关文章
- linux 将进程或者线程绑定到指定的cpu上
基本概念 cpu亲和性(affinity) CPU的亲和性, 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器,也称为CPU关联性:再简单的点的描述就将指定的进程或线程绑定到相应的 ...
- linux进程、线程与cpu的亲和性(affinity)
参考:http://www.cnblogs.com/wenqiang/p/6049978.html 最近的工作中对性能的要求比较高,下面简单做一下总结: 一.什么是cpu亲和性(affinity) C ...
- Linux 进程、线程运行在指定CPU核上
/******************************************************************************** * Linux 进程.线程运行在指定 ...
- Linux进程和线程的比較
进程与线程 參考:http://www.cnblogs.com/blueclue/archive/2010/07/16/1778855.html 首先比較Linux进程和线程的创建的差别,以此展开: ...
- Linux -- 进程或线程独占CPU
如果想让特定进程或线程独占某一或某些CPU,我们需要做三件事. 一,隔离CPU,避免其它线程run在被隔离的CPU上. 二,绑定所有的interrupts到非隔离的CPU上,避免被隔离的CPU收到in ...
- Linux进程与线程的区别
进程与线程的区别,早已经成为了经典问题.自线程概念诞生起,关于这个问题的讨论就没有停止过.无论是初级程序员,还是资深专家,都应该考虑过这个问题,只是层次角度不同罢了.一般程序员而言,搞清楚二者的概念, ...
- 操作系统:Linux进程与线程
这里是一部分内容,还会做修改. 一:目的及内容 学习fork(),exec,pthread库函数的使用,阅读源码,分析fork,exec,pthread_create函数的机理 代码实现: 进程A创建 ...
- 分享自己做的一个指定进程以及线程长时间cpu监控的工具
前言: 前面给大家分享过一个工作中用到的编译拷贝脚本,其实工作中还有一些其他工具的使用,今天再来分享一个自己纯手工的CPU监控的脚本.大家可以结合上篇文章与本篇文章一起学习shell. 主要实现功能: ...
- linux --> 进程和线程
进程和线程 进程(process)和线程(thread)是操作系统的基本概念,下面用一个类比,来解释它们. 1. 计算机的核心是CPU,它承担了所有的计算任务.它就像一座工厂,时刻在运行. 2. 假定 ...
随机推荐
- javascript 基础学习整理
1. javascript是动态语言,脚本语言,弱类型语言. 2. javascript代码在html文件中的位置安排,放在<body></body>内部与外部的区别.如何引用 ...
- Search Insert Position——LeetCode
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Rotate List —— LeetCode
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 开源库CImg 数据格式存储之二(RGB 顺序)
在上一篇博客中已经初步说明了GDI和CImg数据的存储格式感谢博友 Imageshop 评论说明 CImg的说明文档中已有详细说明(详见上篇博客说明) CImg的数据格式确实是RRRGGGBBB顺序存 ...
- SRM 396(1-250pt)
DIV1 250pt 题意:对于一个字符串s,若对于每一个i = 0 to s.size()-p-1都有s[i] = s[i+p]则称字符串s是p循环的."CATCATC", &q ...
- DOM解析原理
用于处理XML文档的DOM元素属性 childNodes:返回当前元素所有子元素的数组: firstChild:返回当前元素的第一个下级子元素: lastChild:返回当前元素的最后一个子元素: n ...
- Windows的应用管理工具 PortableApps,Chocolatey和Ninite
以前为了让我的firefox变成portable的,我使用了PortableApps,它不只是做软件的绿色版,而且也是一个软件的管理平台,可以通过它来管理软件的更新,挺方便的. 前段时间试用Scrip ...
- 【Python爬虫】安装 pyQuery 遇到的坑 Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
windows 64位操作系统下,用 Python 抓取网页,并用 pyQuery 解析网页 pyQuery是jQuery在python中的实现,能够以jQuery的语法来操作解析HTML文档,十分方 ...
- [转] Linux文件系统之hard link&symbol link
这个图很清楚的表示出硬链接和软链接的方式. 1.硬链接: 基本定义:硬链接是有着相同inode号的仅文件名不同的文件(该文件名包含路径信息). 理解:如图,hard link和原始file通过同一个i ...
- (转)Vim用法小结
这是我转的一些vim基本用法,可能对初用者会有帮助,独乐乐不如众乐乐,是吧! Vim一般的Unix和Linux下均有安装. 三种状态 Command: 任何输入都会作为编辑命令,而不会出现在屏幕上 ...