linux下绑定进程到指定CPU的操作方法

这篇具有很好参考价值的文章主要介绍了linux下绑定进程到指定CPU的操作方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

taskset简介

 # taskset 
Usage: taskset [options] [mask | cpu-list] [pid|cmd [args...]]


Show or change the CPU affinity of a process.

Options:
 -a, --all-tasks         operate on all the tasks (threads) for a given pid
 -p, --pid               operate on existing given pid
 -c, --cpu-list          display and specify cpus in list format
 -h, --help              display this help
 -V, --version           output version information

The default behavior is to run a new command:
    taskset 03 sshd -b 1024
You can retrieve the mask of an existing task:
    taskset -p 700
Or set it:
    taskset -p 03 700
List format uses a comma-separated list instead of a mask:
    taskset -pc 0,3,7-11 700
Ranges in list format can take a stride argument:
    e.g. 0-31:2 is equivalent to mask 0x55555555

For more details see taskset(1).

查看cpu的核数

cat /proc/cpuinfo

root@w0112-virtual-machine:/home/w0112# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 158
model name	: Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz
stepping	: 10
microcode	: 0xde
cpu MHz		: 2304.000
cache size	: 8192 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec arat
bugs		:
bogomips	: 4608.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 43 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 158
model name	: Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz
stepping	: 10
microcode	: 0xde
cpu MHz		: 2304.000
cache size	: 8192 KB
physical id	: 2
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 2
initial apicid	: 2
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec arat
bugs		:
bogomips	: 4608.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 43 bits physical, 48 bits virtual
power management:

processor指出是第几个cpu处理器

root@w0112-virtual-machine:/home/w0112# cat /proc/cpuinfo  | grep processor
processor    : 0
processor    : 1

cpu cores指出每个处理器的核心数量

root@w0112-virtual-machine:/home/w0112# cat /proc/cpuinfo  | grep "cpu cores"
cpu cores    : 1
cpu cores    : 1

由此可以看出我的虚拟机有两个处理器,每个处理器有一个核

虚拟机可以通过如下设置配置

taskset使用

测试脚本bb.sh

#!/bin/sh
while [ 1 ]
do
date=`date`
echo "date:$date"
sleep 5
done

默认不绑定cpu

运行脚本

./bb.sh &

查看脚本程序的进程ID

root@w0112-virtual-machine:/home/w0112#  ps | grep bb.sh
  3037 pts/13   00:00:00 bb.sh

查看默认bb.sh绑定到那个核

语法:taskset -p PID

taskset -p 3037

root@w0112-virtual-machine:/home/w0112# taskset -p 3037
pid 3037's current affinity mask: 3

3表示默认使用了cpu0 cpu1

使用CPU编号绑定核

启动绑定到指定CPU

语法:taskset -c cpu-list PID

指定绑定到cpu0

root@w0112-virtual-machine:/home/w0112# taskset -c 0 ./bb.sh&
[2] 3175

查看进程运行在那个核

root@w0112-virtual-machine:/home/w0112# taskset -p 3175
pid 3175's current affinity mask: 1
 1表示cpu0

指定绑定到cpu0和cpu1

root@w0112-virtual-machine:/home/w0112# taskset -c 0,1 ./bb.sh &
[1] 3307

查看绑定结果

root@w0112-virtual-machine:/home/w0112# taskset -p 3307
pid 3307's current affinity mask: 3

3表示cpu0和cpu1  

启动后绑定

运行测试脚本

./bb.sh &

root@w0112-virtual-machine:/home/w0112# ./bb.sh &
[1] 3389

进程ID 3389

绑定到cpu0

语法: taskset -cp cpu PID

绑定到CPU0

root@w0112-virtual-machine:/home/w0112# taskset -cp 0 3389
pid 3389's current affinity list: 0,1
pid 3389's new affinity list: 0
默认绑定到两个核,修改之后绑定到CPU0

查看绑定结果

oot@w0112-virtual-machine:/home/w0112# taskset -p 3389
pid 3389's current affinity mask: 1
mask为1表示cpu0

绑定到cpu0和cpu1

绑定指令

root@w0112-virtual-machine:/home/w0112# taskset -cp 0,1 3389
pid 3389's current affinity list: 0
pid 3389's new affinity list: 0,1
affinity是从0开始 mask是从1开始

查看绑定结果

root@w0112-virtual-machine:/home/w0112# taskset -p 3389
pid 3389's current affinity mask: 3
mask 3表示cpu0和cpu1 对应二进制0011,即是cpu0和cpu1

使用mask形式绑定核

指令介绍

语法:taskset -p mask PID

按照二进制形式从最低为到最高位分别表示cpu的核的序号

0xffffffffffffffff :表示是64核

0x0000000000000001:表示是cpu的第1核

0x0000000000000007:表示是cpu的第1、2、3个核

绑定cpu的两个核

root@w0112-virtual-machine:/home/w0112# taskset -p 0x3 3389
pid 3389's current affinity mask: 3
pid 3389's new affinity mask: 3

查看绑定结果

root@w0112-virtual-machine:/home/w0112# taskset -p 3389
pid 3389's current affinity mask: 3

判断进程目前分配到cpu ID

ps -o pid,psr,comm -p <PID>

root@w0112-virtual-machine:/home/w0112# ps -o pid,psr,comm -p 3389
   PID PSR COMMAND
  3389   0 bb.sh
表示进程bb.sh目前在cpu0上运行,PSR显示的是当前运行在的核的编号,如果没有绑定cpu,可能会出现运行在1上,这个由内核调度来完成的


 文章来源地址https://www.toymoban.com/news/detail-628487.html


 


 

到了这里,关于linux下绑定进程到指定CPU的操作方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux下多核CPU指定程序运行的核

    查看CPU核心数量:lscpu 1.4.1 通过运行时的参数设置 1.4.2 通过代码设置 查看程序的PID 查看程序可运行的核 得出该程序可以在0-3 4个核上运行。 假设我们要使程序运行在第2个核上: 查看程序的PID 查看程序可运行的CPU核 得出设置成功,已将程序绑定在CPU的第2个核上。

    2024年02月21日
    浏览(28)
  • Linux 查看占用资源cpu、内存最大的进程命令

    1、详细查询命令: 查看cpu最大进程,或者内存最大进程。 命令解析:该命令组合实际上是下面两句命令组合。 其中: 以上组合获得cpu或者内存最大的进程。 2、简单查询命令: 根据以上,也可以简化命令为 命令: 按下交互命令快捷键M(按内存排序),P(按cpu占用排序)

    2024年02月11日
    浏览(51)
  • Linux 查看进程和线程CPU和内存占用情况

    linux 下查看进程内的线程有哪些 首先通过进程名称,假设为SensorDev 找到pid号。 ps -p {pid} -T 可以得到该进程里面运行的各线程的id(表现出来是spid)、对应的线程名称(不超过16字符)、运行时间等; cat /proc/{pid}/status |grep Threads 只能显示线程的个数。 top -p {pid} ,然后按H t

    2024年02月08日
    浏览(47)
  • 优化命令之taskset——查询或设置进程绑定CPU

    目录 一:taskset概述 二:安装taskset工具 2.1taskset语法 2.2taskset用法 2.2.1指定PID为8528的进程在CPU1上运行 2.2.2更改具体某一进程(或 线程)CPU亲和性 2.2.3 查看具体某一进程(或 线程)的处理器亲和性(掩码) 2.2.4为具体某一进程(或 线程)CPU亲和性指定一组范围 三:依托nginx进

    2023年04月16日
    浏览(19)
  • 【Linux】Linux突然发现CPU占用100%该怎么办?如何排查进程(带你一文解决)

    目录 Linux的CPU发现占用率100%,一般会由什么情况引起? 1、进程负载过高: 2、错误配置的服务或应用程序: 3、恶意软件或病毒: 4、不良硬件或驱动程序: 5、系统内核问题: 6、CPU过热: 对于排查CPU占用率过高的原因,可以采取以下步骤和工具: 一、top/htop方法 1、使用

    2024年02月08日
    浏览(32)
  • Linux查询内存或CPU占用最多的几个进程

    一、可以使用以下命令查使用内存最多的10个进程 方法1: ps -aux | sort -k4nr | head -10 如果是最高的三个,10改为3即可 命令解释:  1. ps:参数a指代all——所有的进程,u指代userid——执行该进程的用户id,x指代显示所有程序,不以终端机来区分。ps -aux的输出格式如下: USER  

    2024年04月17日
    浏览(29)
  • Linux如何查看当前占用CPU和内存最多的进程

    查看占用 CPU 最高的前10个进程 查看占用内存(MEM)最高的前10个进程 输入 top 命令,然后按下大写M按照内存MEM排序,按下大写P按照CPU排序

    2024年02月17日
    浏览(41)
  • 如何在Linux上通过cgroup限制一个进程使用CPU和内存

    Cgroup(Control Group)是 Linux 内核的一个功能,可以通过它来限制进程的 CPU 和内存占用。Cgroup 实现了对系统资源的细粒度控制和管理,可以将一组进程放入同一个 Cgroup 中,并对该 Control Group 中的所有进程共享相应的资源配额。 下面举个实际的例子,演示如何使用 Cgroup 限制一

    2024年02月15日
    浏览(28)
  • 【Linux】查看系统各种信息的常用命令 (CPU、内存、进程、网口、磁盘、硬件、等等)

    Linux是一种开源的类Unix操作系统,它有很多不同的发行版,如Ubuntu、CentOS、Debian等。Linux系统提供了很多命令行工具,可以让用户方便地查看和管理系统的各种信息,如硬件配置、内存使用、进程状态、网络连接等。本文将介绍一些常用的命令,以及它们的用法和示例。 使用

    2024年02月15日
    浏览(34)
  • Linux 服务器 CPU 详细信息查看、物理 CPU 以及逻辑 CPU

    什么是CPU CPU: (Central Processing Unit)也称为中央处理器,主要通过内部总线,建立起芯片内各部件之间的信息传输通路 查看CPU详细信息 : 我们平常在操作Linux服务器时,如何能够知道服务器的CPU的详细信息呢 [xxx@xxx ~]#  cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 16: CPU 核心数 Intel®

    2024年01月25日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包