文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux c 绑定那个cpu执行

linux c 绑定那个cpu执行

时间:2010-06-08  来源:ubuntuer

 

#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");
                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 = 0; i < num; i++)
                {
                        if (CPU_ISSET(i, &get))
                        {
                                printf("this process %d is running processor : %d\n",getpid(), i);
                        }
                }
        }
        return 0;
}

 

 

kenthy@Tencent:~> top

top - 21:43:02 up 15 days, 22:09,  2 users,  load average: 0.32, 0.27, 0.28
Tasks: 165 total,   3 running, 160 sleeping,   0 stopped,   2 zombie
Cpu0  : 19.0%us,  4.3%sy,  0.0%ni, 75.7%id,  0.0%wa,  0.0%hi,  1.0%si,  0.0%st
Cpu1  : 13.9%us, 16.6%sy,  0.0%ni, 69.2%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Cpu2  : 13.2%us,  3.6%sy,  0.0%ni, 83.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  5.0%us,  2.6%sy,  0.0%ni, 92.1%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st


top后之后按1  查看每个cpu的使用情况

相关阅读 更多 +
排行榜 更多 +
mirrox模组(玩家自制)手机版下载

mirrox模组(玩家自制)手机版下载

休闲益智 下载
集装箱模拟器手机版下载安装

集装箱模拟器手机版下载安装

模拟经营 下载
哔咔漫画app下载免费2025

哔咔漫画app下载免费2025

浏览阅读 下载