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;

  • 相关阅读:
    20191318实验四 《Python程序设计》实验报告
    20191318实验三 Socket编程技术
    20191318实验二 Python程序设计入门
    实验一 Python程序设计入门
    《信息安全专业导论》第十二周学习总结
    《信息安全专业导论》第十一周学习总结
    markdown画思维导图
    markdown页面内跳转
    20191206 2019-2020-2 《Python程序设计》实验四报告
    20191206 实验三《Python程序设计》实验报告
  • 原文地址:https://www.cnblogs.com/ybgame/p/2216777.html
Copyright © 2011-2022 走看看