- 论坛徽章:
- 0
|
cpu.c-
- #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;
- }
复制代码 上段代码中有几个地方不明白,请大家帮忙解释,谢谢了~~
1)created_thread ------------------------这个变量好像没有用到,为什么?是某些函数调用需要的变量吗?
2)char* argv[]-----------------------------这个字符指针数组表示意思?
3)atoi(argv[])--------------------------------------这个函数表示什么意思?是获取进程id的吗?
4)CPU_SET(myid, &mask)函数应该是将进程号为myid的进程加入到mask掩码指定的CPU中运行???
5)CPU_ZERO(&get);这里将CPU集合清空,怎么后面还有CPU_ISSET(i, &get)函数???这时cpu i肯定不在 &get里面了吧?
以上几个问题,看的不是很明白,希望大家指点一下咯。
另外,如果我想绑定具体的进程到某个CPU,例如现有进程a、b、c、d,现在我想绑定a、b、c到CPU 0上,进程d绑定CPU 1上,该怎么做?假如我不知道进程a、b、c、d
的进程号的情况下。
如果不是进程,是可执行的应用程序可以吗?比如四个不同阶数矩阵乘法。
最近看到这个,觉得很有兴趣,想问问大家,谢谢了~~~ |
|