linux c 绑定cpu,linux c 绑定那个cpu执行

#include

#include

#include

#include

#include

#define __USE_GNU

#include

#include

#include

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值