ITPub博客

首页 > 应用开发 > > 把程序绑定在某个CPU上运行

把程序绑定在某个CPU上运行

作者:xiaojun63 时间:2019-08-27 16:18:22 0 删除 编辑
NAME
       sched_setaffinity, sched_getaffinity - set and get a thread's CPU affinity mask 
       设置并获取线程的CPU关联掩码
SYNOPSIS
       #define _GNU_SOURCE /* See feature_test_macros(7) */
       #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);

一般使用

1. 查看系统有几个CPU/核心.    lscpu 

CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1  //每个核心上只能跑一个线程, 好的CPU都能跑多个
Core(s) per socket: 4  //每个槽上有 4个核
Socket(s):          1  //只有一个CPU槽有效.

2. 查看自己的程序在各个CPU上的资源专用. 代码内容

#define _GNU_SOURCE
#include <sched.h>
    cpu_set_t set; 
    CPU_SET(childCPU, &set); //指定在某个CPU core 上运行        
    if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) //出错的情况.

命令行模式下, 只能设置优先级别.

linux nice 命令详解

  功能说明:设置优先权。

  语  法:nice [-n <优先等级>][--help][--version][执行指令]

  补充说明:nice指令可以改变程序执行的优先权等级。

  参  数:-n<优先等级>或-<优先等级>或--adjustment=<优先等级>  设置欲执行的指令的优先权等级。等级的范围从-20-19,其中-20最高,19最低,只有系统管理者可以设置负数的等级。

linux renice 命令详解

  功能说明:调整优先权。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/658/viewspace-3136843/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    243
  • 访问量
    3052