进程和cpu绑定
2016-01-18 16:19
225 查看
#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;
}
#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;
}
相关文章推荐
- alarmmanger set 不生效的问题
- iOS 自定义日期选择器
- boost filesystem测试
- PHP移动文件或者文件夹
- Handler类的简单使用与进度条的更新
- java解压缩文件的实现示例,支持rar和zip格式 ____http://my.oschina.net/manville/blog/280420
- 【机器学习算法】之Apriori
- XML DOM解析 基础概念
- 程序员常见面试之 数据库 知识点小结(三)
- Java 多线程(七) 线程间的通信——wait及notify方法
- win7怎么设置麦克风?win7设置麦克风的方法
- (assign,retain,copy):
- sqlserver的最大连接数测试
- C4.5算法
- 系统调用基本概念
- pb web 环境配置及常见错误处理
- 前闭后开区间表示法
- zxing.dll生成条码
- 【BZOJ 2152】 聪聪可可
- Gradle DSL method not found: 'compile() 解决办法