zoukankan      html  css  js  c++  java
  • Linux 操作系统下CPU多核心的绑定 深圳

    现在多CPU的趋势越来越大了. 有时候为了更好地操作机器, 需要将某个进程绑定到具体的CPU上去. 下面给出了一个进程绑定到具体的CPU上去的一个例子.


    cpu.c

    [CODE]
    #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[])
    {
            int num = sysconf(_SC_NPROCESSORS_CONF);
            int created_thread = 0;
            int myid;
            int i;
            int j = 0;

            cpu_set_t mask;
            cpu_set_t get;

            if (argc != 2)
            {
                    printf("usage : ./cpu num\n");  //输入要绑定的CPU序号
                    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)  //绑定到CPU
            {
                    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 = 0; i < num; i++)
                    {
                            if (CPU_ISSET(i, &get))
                            {
                                    printf("this process %d is running processor : %d\n",getpid(), i);
                            }
                    }
            }
            return 0;

  • 相关阅读:
    技术债务墙:一种让技术债务可见并可协商的方法
    墙裂推荐
    shell 脚本通过Webhook 发送消息到微信群
    关于中医的一段对话 [ZZ] -- 思维训练故事
    应用深度神经网络预测学生期末成绩
    Python中的模块引用机制
    批量修改含空格的文件名「Linux」
    Markdown数学公式语法
    批处理修改IP
    FTD团队目录
  • 原文地址:https://www.cnblogs.com/ybgame/p/2216777.html
Copyright © 2011-2022 走看看