Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4731879
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-06-08 21:46:07

 

#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;
}

 

 

:~> 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的使用情况

阅读(1766) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-06-14 17:34:41

[url=http://www.b2cmbtshoes.com/]MBT Shoes[/url] [url=http://www.b2cmbtshoes.com/]Discount MBT[/url] [url=http://www.b2cmbtshoes.com/]mbt sale[/url] [url=http://www.b2cmbtshoes.com/]cheap mbt[/url] [url=http://www.b2cmbtshoes.com/]buy mbt[/url] [url=http://www.b2cmbtshoes.com/]mbt uk[/url] [url=http://www.b2cmbtshoes.com/]mbt footwear[/url] [url=http://www.b2cmbtshoes.com/mbt-chapa-shoes-c-2.html]MBT Chapa Shoes[/url] [url=http://www.b2cmbtshoes.com/mbt-lami-shoes-c-1.html]MBT Lami Shoes