免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6745 | 回复: 3
打印 上一主题 下一主题

[C] [已解决]多核系统中怎么查看进程的某个线程被分配到哪个cpu上? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-02 10:33 |只看该作者 |倒序浏览
本帖最后由 wwdwwd 于 2010-04-02 11:54 编辑

[求教]多核系统中怎么查看进程的某个线程被分配到哪个cpu上?

论坛徽章:
0
2 [报告]
发表于 2010-04-02 10:51 |只看该作者

  1.        #define _GNU_SOURCE
  2.        #include <pthread.h>

  3.        int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
  4.                                   const cpu_set_t *cpuset);
  5.        int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
  6.                                   cpu_set_t *cpuset);
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-04-02 11:54 |只看该作者
问题解决了,linux下用sched_getcpu即可,不过要求内核高于2.6.19,glibc高于2.6。另外,cpu的亲和性只是说可以把某个线程绑定到某个cpu上,而不能获取当前的线程正被绑定到某个cpu上。

http://www.kernel.org/doc/man-pa ... sched_getcpu.3.html
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<sys/types.h>
  4. #include<sys/sysinfo.h>
  5. #include<unistd.h>

  6. #define _GNU_SOURCE
  7. #include <sched.h>


  8. #include<ctype.h>
  9. #include<string.h>
  10. #include<pthread.h>


  11. void get_process() {
  12.                 printf("cpu is %d\n",sched_getcpu());
  13. }

  14. void *test(void *arg) {
  15.        long long i;
  16.       get_process();
  17.         return (void*)0;

  18. }
  19. int main(int argc, char* argv[])
  20. {
  21.                 pthread_t tid;
  22.               pthread_create(&tid,NULL,test,NULL);
  23.               pthread_create(&tid,NULL,test,NULL);
  24.               pthread_create(&tid,NULL,test,NULL);
  25.               pthread_create(&tid,NULL,test,NULL);
  26.               pthread_create(&tid,NULL,test,NULL);
  27.                 get_process();
  28.                 return 0;
  29. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-04-02 12:17 |只看该作者
学习学习,咱就用过双核
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP