linux线程绑定cpu
函数介绍
#define __USE_GNU
#include <sched.h>
void CPU_ZERO(cpu_set_t *set);
void CPU_SET(int cpu, cpu_set_t *set);
void CPU_CLR(int cpu, cpu_set_t *set);
int CPU_ISSET(int cpu, cpu_set_t *set);
法一
int cpu = 0;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu, &mask);
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
{
perror("pthread_setaffinity_np");
}
法二
int cpu = 1;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu, &mask);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
int cpu:指定cpu使用。可以只使用一个核,也可以同时指定多核。实测还是指定一个核效果更好
线程中使用
void *func(void *ptr)
{
pthread_detach(pthread_self());
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(1, &mask);
#if 0
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
{
perror("pthread_setaffinity_np");
}
#else
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
#endif
while(1);
}
linux线程绑定cpu的更多相关文章
- Linux编程之《进程/线程绑定CPU》
Intro----- 通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上. /***** ...
- 线程绑定CPU核-sched_setaffinity
CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...
- Linux 线程占用CPU过高定位分析
今天朋友问我一个Linux程序CPU占用涨停了,该如何分析, CPU占用过高,模拟CPU占用过高的情况 先上一段代码: #include <iostream> #include <t ...
- 为线程绑定CPU
// learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...
- 线程绑定cpu
#include <stdio.h> #include <pthread.h> #include <sys/sysinfo.h> #include <unis ...
- Windows10 临时将线程绑定至指定CPU的方法
本文首发:https://www.somata.work/2019/WindowsThreadBind.html 将线程绑定至指定CPU,这个应该时很多管理员需要了解认知的操作了吧,这样可以在一定程度 ...
- NGINX源代码剖析 之 CPU绑定(CPU亲和性)
作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自&quo ...
- linux下将不同线程绑定到不同core和cpu上——pthread_setaffinity_np
=============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主 ...
- Linux进程或线程绑定到CPU
Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程 ...
随机推荐
- NSURLSession的知识小记
1.NSURLSession的使用流程 使用NSRULSession对象创建Task, 然后执行Task 2.获取NSURLSession ()获得共享的Session + (NSURLSession ...
- Python—基本数据类型
核心数据类型: 数字(int整型.float浮点型.complex复数.bool布尔型) 字符串 str 列表(List) 元组(Tuple) 字典(Dictionary) 集合() 数字 整数,浮点 ...
- MySQL 主从复制(实时热备)原理与配置
MySQL是现在普遍使用的数据库,但是如果宕机了必然会造成数据丢失.为了保证MySQL数据库的可靠性,就要会一些提高可靠性的技术.MySQL主从复制可以做到实时热备数据.本文介绍MySQL主从复制原理 ...
- Lnmp架构部署动态网站环境.2019-7-3-1.2
Nginx安装 一.安装准备 Pcre(Perl Compatible Regular Expressions,兼容正则表达式)安装pcre库是为了使Nginx支持HTTP Rewrite模块. 安装 ...
- 修改postgresql 密码
sudo -u postgres psql -c "alter user postgres password '123456';"
- HTML 中img标签不显示
异常 图片请求响应吗: 403, url响应如下: Request Method: GET Status Code: 403 Forbidden Remote Address: ***.***.**. ...
- input 控件常用属性
- day2_窗口句柄切换
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/16 14:21 # @Author : 大坏男孩 # @File : d ...
- 基于C++的STL的vector实现静态链表,要求包含插入,删除,和查找功能
//main.cpp部分 #include"List.cpp" int main() { StaticList<int> SL; SL.Insert(,); SL.In ...
- Python中xlrd和xlwt模块使用方法----》》数据库数据导出(之一)
xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. (1) 打开excel文件并获取所有sheet >>> import xlrd >>& ...