Linux如何将进程绑定运行在指定CPU上

Linux如何将进程绑定运行在指定CPU上,第1张

绑定cpu方式:

c语言:

sched_setaffinity(0, sizeof(mask), &mask)

shell命令:

在grup启动时传给内核参数: isolcpus=2-15, 这里表示隔离第3到16个CPU, Linux程序只跑在第一和第二个CPU上, 空闲的CPU我们可以指定跑进程了.

这是内核文档里对内核参数的解释:

isolcpus= [KNL,SMP] Isolate CPUs from the general scheduler.

Format:

<cpu number>,...,<cpu number>

or

<cpu number>-<cpu number>

(must be a positive range in ascending order)

or a mixture

<cpu number>,...,<cpu number>-<cpu number>

This option can be used to specify one or more CPUs

to isolate from the general SMP balancing and scheduling

algorithms. You can move a process onto or off an

"isolated" CPU via the CPU affinity syscalls or cpuset.

<cpu number>begins at 0 and the maximum value is

"number of CPUs in system - 1".

This option is the preferred way to isolate CPUs. The

alternative -- manually setting the CPU mask of all

tasks in the system -- can cause problems and

suboptimal load balancer performance.

linux nice 命令详解

功能说明:设置优先权

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

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

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

--help  在线帮助。

   --version  显示版本信息。

linux renice 命令详解

功能说明:调整优先权。

语法:renice [优先等级][-g <程序群组名称>...][-p <程序识别码>...][-u <用户名称>...]

补充说明:renice指令可重新调整程序执行的优先权等级。预设是以程序识别码指定程序调整其优先权,您亦可以指定程序群组或用户名称调整优先权等级,并修改所有隶属于该程序群组或用户的程序的优先权。等级范围从-20--19,只有系统管理者可以改变其他用户程序的优先权,也仅有系统管理者可以设置负数等级。

参数:

-g <程序群组名称> 使用程序群组名称,修改所有隶属于该程序群组的程序的优先权。

-p <程序识别码> 改变该程序的优先权等级,此参数为预设值。

-u <用户名称> 指定用户名称,修改所有隶属于该用户的程序的优先权。

taskset设定cpu亲和力,cpu亲和力是指

CPU调度程序属性关联性是“锁定”一个进程,使他只能在一个或几个cpu线程上运行。对于一个给定的系统上设置的cpu。给定CPU亲和力和进程不会运行在任何其他CPU。注意,Linux调度器还支持自然CPU关联:(不能让这个cpu只为这一个进程服务)

这里要注意的是我们可以把某个程序限定在某一些CPU上运行,但这并不意味着该程序可以独占这些CPU,其实其他程序还是可以利用这些CPU运行。如果要精确控制CPU,taskset就略嫌不足,cpuset才是可以

-a, --all-tasks *** 作所有的任务线程-p, --pid *** 作已存在的pid-c, --cpu-list 通过列表显示方式设置CPU

(1)指定1和2号cpu运行25718线程的程序

taskset -cp 1,2 25718

(2),让某程序运行在指定的cpu上 taskset -c 1,2,4-7 tar jcf test.tar.gz test

(3)指定在1号CPU上后台执行指定的perl程序

taskset –c 1 nohup perl pi.pl &

程序命令输入更改已经运行程序的优先级。

#taskset:

-p,设定一个已存在的pid,而不是重新开启一个新任务

-c,指定一个处理,可以指定多个,以逗号分隔,也可指定范围,如:2,4,5,6-8。

切换某个进程到指定的cpu上:taskset-cp313290。让某程序运行在指定的cpu上:taskset-c1,2,4-7tarjcftest。tar。gztest。

需要注意的是,taskset-cp313290在设定一个已经存在的pid时,子进程并不会继承父进程的,因此像tarzcfxxx tar。gzxxx这样的命令,最好在启动时指定cpu,如果在已经启动的情况下,则需要指定tar调用的gzip进程。

优点:

1、Linux由众多微内核组成,其源代码完全开源。

2、Linux继承了Unix的特性,具有非常强大的网络功能,其支持所有的因特网协议,包括TCP/IPv4、TCP/IPv6和链路层拓扑程序等,且可以利用Unix的网络特性开发出新的协议栈。

3、Linux系统工具链完整,简单 *** 作就可以配置出合适的开发环境,可以简化开发过程,减少开发中仿真工具的障碍,使系统具有较强的移植性。

大概的介绍一下Linux 的指定CPU运行,包括进程和线程。linux下的top命令是可以查看当前的cpu的运行状态,按1可以查看系统有多少个CPU,以及每个CPU的运行状态。

可是如何查看线程的CPU呢?top

-Hp pid,pid就是你当前程序的进程号,如果是多线程的话,是可以查看进程内所有线程的CPU和内存使用情况。

pstree可以查看主次线程,同样的pstree -p pid。可以查看进程的线程情况。

taskset这个其实才是重点,可以查看以及设置当前进程或线程运行的CPU(设置亲和力)。

taskset -pc pid,查看当前进程的cpu,当然有的时候不只是一个,taskset -pc cpu_num pid ,cpu_num就是设置的cpu。

这样的话基本的命令和 *** 作其实大家都知道了,接下来就是在代码中完成这些 *** 作,并通过命令去验证代码的成功率。

进程制定CPU运行:

[cpp] view plain copy

#include<stdlib.h>

#include<stdio.h>

#include<sys/types.h>

#include<sys/sysinfo.h>

#include<unistd.h>

#define __USE_GNU

#include<sched.h>

#include<ctype.h>

#include<string.h>

int main(int argc, char* argv[])

{

//sysconf获取有几个CPU

int num = sysconf(_SC_NPROCESSORS_CONF)

int created_thread = 0

int myid

int i

int j = 0

//原理其实很简单,就是通过cpu_set_t进行位与 *** 作

cpu_set_t mask

cpu_set_t get

if (argc != 2)

{

printf("usage : ./cpu num\n")

exit(1)

}

myid = atoi(argv[1])

printf("system has %i processor(s). \n", num)

//先进行清空,然后设置掩码

CPU_ZERO(&mask)

CPU_SET(myid, &mask)

//设置进程的亲和力

if (sched_setaffinity(0, sizeof(mask), &mask) == -1)

{

printf("warning: could not set CPU affinity, continuing...\n")

}

while (1)

{

CPU_ZERO(&get)

//获取当前进程的亲和力

if (sched_getaffinity(0, sizeof(get), &get) == -1)

{

printf("warning: cound not get cpu affinity, continuing...\n")

}

for (i = 0i <numi++)

{

if (CPU_ISSET(i, &get))

{

printf("this process %d is running processor : %d\n",getpid(), i)

}

}

}

return 0

}

进程设置CPU运行,其实只能是单线程。多线程设定CPU如下:

[cpp] view plain copy

#define _GNU_SOURCE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

#include <sched.h>

void *myfun(void *arg)

{

cpu_set_t mask

cpu_set_t get

char buf[256]

int i

int j

//同样的先去获取CPU的个数

int num = sysconf(_SC_NPROCESSORS_CONF)

printf("system has %d processor(s)\n", num)

for (i = 0i <numi++) {

CPU_ZERO(&mask)

CPU_SET(i, &mask)

//这个其实和设置进程的亲和力基本是一样的

if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) <0) {

fprintf(stderr, "set thread affinity failed\n")

}

CPU_ZERO(&get)

if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) <0) {

fprintf(stderr, "get thread affinity failed\n")

}

for (j = 0j <numj++)

{

if (CPU_ISSET(j, &get))

{

printf("thread %d is running in processor %d\n", (int)pthread_self(), j)

}

}

j = 0

while (j++ <100000000) {

memset(buf, 0, sizeof(buf))

}

}

pthread_exit(NULL)

}

int main(int argc, char *argv[])

{

pthread_t tid

if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0)

{

fprintf(stderr, "thread create failed\n")

return -1

}

pthread_join(tid, NULL)

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8458682.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存