首页 > 系统服务 > 详细

linux cpu亲和性设置

时间:2014-03-12 17:36:55      阅读:1000      评论:0      收藏:0      [点我收藏+]

 1、设置进程的cpu亲和性程序如下,

bubuko.com,布布扣
#Using:g++ setCpuAffinity.c -o setCpuAffinity

#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>

//#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* argv[])
{
        int num = sysconf(_SC_NPROCESSORS_CONF);
        int created_thread = 0;
        int myid, pid;
        int i;
        int j = 0;

        cpu_set_t mask;
        cpu_set_t get;

        if (argc < 3)
        {
                printf("usage : ./setAffinity pid num1,num2,..,numn \n");
                exit(1);
        }

        pid = atoi(argv[1]);
        CPU_ZERO(&mask);
        for (i = 2; i < argc; i++)
        {
                myid = atoi(argv[i]);
                CPU_SET(myid, &mask);
        }
        if (sched_setaffinity(pid, sizeof(mask), &mask) == -1)
        {
                printf("warning: could not set CPU affinity, continuing...\n");
        } else {
                printf("sched_setaffinity succeed !!!\n");

        }
}
bubuko.com,布布扣

 2、获取进程的CPU亲和性程序如下,

bubuko.com,布布扣
#Using:g++ getCpuAffinity.c -o getCpuAffinity

#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>

//#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* argv[])
{
        int num = sysconf(_SC_NPROCESSORS_CONF);
        int pid;
        int i;

        cpu_set_t get;

        if (argc != 2)
        {
                printf("usage : ./setAffinity pid \n");
                exit(1);
        }

        pid = atoi(argv[1]);

        CPU_ZERO(&get);
        if (sched_getaffinity(pid, 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", pid, i);
                }
        }
}
bubuko.com,布布扣

linux cpu亲和性设置,布布扣,bubuko.com

linux cpu亲和性设置

原文:http://www.cnblogs.com/VincentXu/p/3596547.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!