Intro
-----

通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上。

 /************************************************
* 该例程讲解了进程、线程绑定到固定的cpu核心上运行
* 来提高程序运行效率
************************************************/
#include <unistd.h>
#ifndef __USE_GNU
#define __USE_GNU // 为了使用SET_SET()等宏定义,但unistd.h里面好像已经定义了
#endif
#include <sched.h>
#include <pthread.h>
#include <stdio.h>
#include <vector> unsigned int systemCPUNum()
{
// _SC_NPROCESSORS_CONF的值为CPU个数,基于0开始编号
return sysconf(_SC_NPROCESSORS_CONF);
} bool currentProcessAffinity(std::vector<unsigned int>& runningCPUVector)
{
cpu_set_t cpuSet; // 清空一个CPU集合
CPU_ZERO(&cpuSet); // 得到指定进程ID绑定到哪个CPU
int ret = sched_getaffinity(, // 0代表当前进程
sizeof(cpuSet),
&cpuSet);
if (ret < )
{
return false;
} unsigned int cpuNum = systemCPUNum();
runningCPUVector.clear();
for (unsigned int i = ; i < cpuNum; ++i)
{
// 检查一个CPU号是否在一个集合中
if (CPU_ISSET(i, &cpuSet))
{
runningCPUVector.push_back(i);
}
} return true;
} bool setCurrentProcessAffinity(const std::vector<unsigned int>& needBindCPUVector)
{
cpu_set_t cpuSet; // 清空一个CPU集合
CPU_ZERO(&cpuSet); for (auto& iter : needBindCPUVector)
{
CPU_SET(iter, &cpuSet);
} // 将指定进程ID绑定到CPU
int ret = sched_setaffinity(, // 0代表当前进程
sizeof(cpuSet),
&cpuSet);
if (ret < )
{
return false;
} return true;
} bool currentThreadAffinity(std::vector<unsigned int>& runningCPUVector)
{
cpu_set_t cpuSet; // 清空一个CPU集合
CPU_ZERO(&cpuSet); // 得到指定线程ID绑定到哪个CPU
int ret = pthread_getaffinity_np(pthread_self(),
sizeof(cpuSet),
&cpuSet);
if (ret < )
{
return false;
} unsigned int cpuNum = systemCPUNum();
runningCPUVector.clear();
for (unsigned int i = ; i < cpuNum; ++i)
{
// 检查一个CPU号是否在一个集合中
if (CPU_ISSET(i, &cpuSet))
{
runningCPUVector.push_back(i);
}
} return true;
} bool setCurrentThreadAffinity(const std::vector<unsigned int>& needBindCPUVector)
{
cpu_set_t cpuSet; // 清空一个CPU集合
CPU_ZERO(&cpuSet); for (auto& iter : needBindCPUVector)
{
CPU_SET(iter, &cpuSet);
} // 将指定线程ID绑定到CPU
int ret = pthread_setaffinity_np(pthread_self(),
sizeof(cpuSet),
&cpuSet);
if (ret < )
{
return false;
} return true;
} int main()
{
printf("*****Process bind CPU sample*****\n");
unsigned int cpuNum = systemCPUNum();
printf("Current system has %u CPU(s)\n", cpuNum); std::vector<unsigned int> runningCPUVector;
if (!currentProcessAffinity(runningCPUVector))
{
printf("Get current process was bound witch CPU failed\n");
return ;
} for (auto& iter : runningCPUVector)
{
printf("Current process is running at %u CPU\n", iter);
} std::vector<unsigned int> needBindCPUVector {, };
if (!setCurrentProcessAffinity(needBindCPUVector))
{
printf("Current process bind CPU failed\n");
return ;
} printf("Current process bind CPU success\n"); runningCPUVector.clear();
if (!currentProcessAffinity(runningCPUVector))
{
printf("Get current process was bound witch CPU failed\n");
return ;
} for (auto& iter : runningCPUVector)
{
printf("Current process is running at %u CPU\n", iter);
} printf("\n*****Thread bind CPU sample*****\n");
runningCPUVector.clear();
if (!currentThreadAffinity(runningCPUVector))
{
printf("Get current thread was bound witch CPU failed\n");
return ;
} for (auto& iter : runningCPUVector)
{
printf("Thread %lu is running at %u CPU\n", pthread_self(), iter);
} needBindCPUVector.clear();
needBindCPUVector.push_back();
if (!setCurrentThreadAffinity(needBindCPUVector))
{
printf("Current thread bind CPU failed\n");
return ;
} printf("Thread %lu bind CPU success\n", pthread_self()); runningCPUVector.clear();
if (!currentThreadAffinity(runningCPUVector))
{
printf("Get current thread was bound witch CPU failed\n");
return ;
} for (auto& iter : runningCPUVector)
{
printf("Thread %lu is running at %u CPU\n", pthread_self(), iter);
} return ;
}

程序执行的输出结果:
*****Process bind CPU sample*****
Current system has 4 CPU(s)
Current process is running at 0 CPU
Current process is running at 1 CPU
Current process is running at 2 CPU
Current process is running at 3 CPU
Current process bind CPU success
Current process is running at 0 CPU
Current process is running at 2 CPU

*****Thread bind CPU sample*****
Thread 139871129114432 is running at 0 CPU
Thread 139871129114432 is running at 2 CPU
Thread 139871129114432 bind CPU success
Thread 139871129114432 is running at 1 CPU

该例子的github地址:https://github.com/chxuan/samples/blob/master/BindCPU/BindCPU.cpp

Linux编程之《进程/线程绑定CPU》的更多相关文章

  1. Linux编程之《看门狗进程》

    Intro 当我们编写服务器代码时,为了让自己的服务器在意外崩溃时能够及时的重启,软件看门狗就显示出它的作用了,该看门狗进程是通过fork一个子进程(业务进程),父进程一旦捕获到了子进程的结束信号就重 ...

  2. Linux编程之《守护进程》

    Intro ----- 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常 ...

  3. Linux编程 7 (实时监测进程 top, 结束进程kill,killall)

    一. 实时监测进程 top 在一篇里讲到ps命令在收集进程信息时非常有用,但它只能显示某个特定时间点的信息.想要观察那些频繁换进换出的内存进程趋势,用top命令是合适的.使用top命令如下图所示: 在 ...

  4. 10.3、android输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)

    3. 任意进程间通信(socketpair_binder) 进程每执行一次open打开文件,都会在内核中有一个file结构体表示它: 对每一个进程在内核中都会有一个task_struct表示进程,这个 ...

  5. Linux 下如何使用看门狗

      Linux内核有集成WD的选项.将其使能后,系统里就会有watchdog的设备驱动:/dev/watchdog.这样,在应用程序里只需打开这个设备使用即可:#include <fcntl.h ...

  6. Linux 软件看门狗 watchdog 喂狗

    Linux 自带了一个 watchdog 的实现,用于监视系统的运行,包括一个内核 watchdog module 和一个用户空间的 watchdog程序.内核 watchdog 模块通过 /dev/ ...

  7. 【目录】linux 编程

    随笔分类 - linux 编程 Linux编程 24 shell编程(结构化 if [ condition ] 数值比较,字符串比较) 摘要: 一.概述 接着上篇讲的结构化命令,最后讲到了test命令 ...

  8. linux设备驱动归纳总结(十一):写个简单的看门狗驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-112879.html linux设备驱动归纳总结(十一):写个简单的看门狗驱动 xxxxxxxxxxx ...

  9. linux内核中断之看门狗

    一:内核中断 linux内核中的看门狗中断跟之前的裸板的中断差不多,在编写驱动之前,需要线把内核自带的watch dog模块裁剪掉,要不然会出现错误:在Device Drivers /Watchdog ...

  10. Linux C语言编程学习笔记 (1)进程控制入门

    想进行Linux系统开发已经很久了,一直没有付诸实践.今日终于开始学习Linux下的C语言编程,研究一天,终于大概弄明白了Linux系统进程管理的一些基本概念和编程方法,总结下来以方便大家学习和自己实 ...

随机推荐

  1. 【原】Storm Tutorial

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  2. 解决YUM无法正常工作

    1. 错误发生背景 在进行安装依赖包的时候,能够在YUM源中找到相关的RPM包,但是无法进行下载,在单独进行安装RPM包的时候能够进行安装,报错截图如下: 具体的报错信息如下: Error Downl ...

  3. [算法] 冒泡排序 Bubble Sort

    冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  4. Linux重复执行上条命令

    Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行.2.按 !! 并回车执行.3.输入 !-1 并回车执行.4.按 Ctrl+P 并回车执行.

  5. 转载-MySQL 加锁处理分析

    MySQL 加锁处理分析 发表于 2013 年 12 月 13 日 由 hedengcheng 1    背景    1 1.1    MVCC:Snapshot Read vs Current Re ...

  6. Visual Studio 2013智能提示失效解决办法

    各种解决VS2013智能提示失效办法: 1.重置所有设置    工具->导入导出设置->重置所有设置 2.智能提示开关: 工具->选项->文本编辑器->C#->常规 ...

  7. homework6-更加简单的题目

    又把时间搞错了 以为这次要写客户端程序的博客 没想到这次是“怎么吃” 言归正传 cnblog上面有很多技术博客 http://perhaps.cnblogs.com/archive/2005/08/0 ...

  8. mysql注册服务

    http://www.2cto.com/database/201301/185456.html ____________________________________________________ ...

  9. maven 控制台乱码

    在pom.xml加一条配置 <project> …… <properties> <argLine>-Dfile.encoding=UTF-8</argLine ...

  10. DBMS_ERRLOG记录DML错误日志(二)

    上一篇简单介绍了DML记录语句的限制,虽然所有的例子都是利用INSERT语句,但是LOG ERRORS语句并没有这个限制,UPDATE.DELETE和MERGE都可以使用这个语句.下面要说的就是这篇的 ...