进程和线程的cpu亲和性

上传人:xiao****1972 文档编号:84058265 上传时间:2019-03-02 格式:DOCX 页数:4 大小:19.98KB
返回 下载 相关 举报
进程和线程的cpu亲和性_第1页
第1页 / 共4页
进程和线程的cpu亲和性_第2页
第2页 / 共4页
进程和线程的cpu亲和性_第3页
第3页 / 共4页
进程和线程的cpu亲和性_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《进程和线程的cpu亲和性》由会员分享,可在线阅读,更多相关《进程和线程的cpu亲和性(4页珍藏版)》请在金锄头文库上搜索。

1、进程和线程的亲缘性(affinity)是指可以将进程或者是线程强制限制在可用的CPU子集上运行的特性,它一定程度上把进程/线程在多处理器系统上的调度策略暴露给系统程序员。 CPU的数量和表示在有n个CPU的Linux上,CPU是用0.n-1来进行一一标识的。CPU的数量可以通过proc文件系统下的CPU相关文件得到,如cpuinfo和stat:$ cat /proc/stat | grep cpu0-9+ | wc -l8$ cat /proc/cpuinfo | grep processor | wc -l8在系统编程中,可以直接调用库调用sysconf获得:sysconf(_SC_NPRO

2、CESSORS_ONLN);进程的亲缘性Linux操作系统在2.5.8引入了调度亲缘性相关的系统调用:int sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask);int sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask);其中sched_setaffinity是设定进程号为pid的进程调度亲缘性为mask,也就是说它只能在mask中指定的CPU之间进行调度执行;sched_getaffinity当然就是得到进程号为p

3、id的进程调度亲缘性了。如果pid为0,则操纵当前进程。第二个参数指定mask所指空间的大小,通常为sizeof(cpu_set_t)。第三个参数mask的类型为cpu_set_t,即CPU集合,GNU的c库(需要在include头文件之前定义_USE_GNU)还提供了操作它们的宏:void CPU_CLR(int cpu, cpu_set_t *set);int CPU_ISSET(int cpu, cpu_set_t *set);void CPU_SET(int cpu, cpu_set_t *set);void CPU_ZERO(cpu_set_t *set); 如果我们所关心的只是CP

4、U#0和CPU#1,想确保我们的进程只会运作在CPU#0之上,而不会运作在CPU#1之上。下面程序代码可以完成此事:cpu_set_tset;intret,i;CPU_ZERO(&set);CPU_SET(0,&set);CPU_CLR(1,&set);ret=sched_setaffinity(0,sizeof(cpu_set_t),&set);if(ret=-1)perror(sched_se);for(i=0;i3;i+) intcpu;cpu=CPU_ISSET(i,&set);printf(cpu=%iis%s/n,i,cpu?set:unset);Linux只提供了面向线程的调度亲

5、缘性一种接口,这也是上面只提调度亲缘性而不直言进程亲缘性的原因。当前Linux系统下广泛采用的线程库NPTL(Native Posix Thread Library)是基于线程组来实现的,同一个线程组中的线程对应于一组共享存储空间的轻量级进程,它们各自作为单独调度单位被内核的调度器在系统范围内调度,这种模型也就是我们通常所说的1-1线程模型。正因如此,目前线程的调度范围(可以用函数pthread_attr_getscope和pthread_attr_setscope获取和设置)只能是系统级而不能是进程级。 c库的GNU扩展所提供的有关线程亲缘性的API如下:int pthread_attr_s

6、etaffinity_np (pthread_attr_t *_attr, size_t _cpusetsize, _const cpu_set_t *_cpuset);int pthread_attr_getaffinity_np (_const pthread_attr_t *_attr, size_t _cpusetsize, cpu_set_t *_cpuset);int pthread_setaffinity_np (pthread_t _th, size_t _cpusetsize, _const cpu_set_t *_cpuset);intpthread_getaffinity

7、_np (pthread_t _th, size_t _cpusetsize, cpu_set_t *_cpuset);亲缘性的继承调度亲缘性是被fork出来的子进程所继承的,即使子进程通过exec系列函数更换了执行镜像。因为Linux操作系统下进程和线程的创建都是通过系统调用clone来实现的,所以实际上调度亲缘性也是被用pthread_create创建的线程所继承的。这意味着,如果主线程在创建其它线程之前设定亲缘性,那么它所设定的亲缘性将被继承,因为这时所有线程的亲缘性相同(假设之后没有任何线程私自设置亲缘性),我们就可以认为前面设置的是进程亲缘性,而不管它所调用的函数是sched_set

8、affinity还是pthread_setaffnity_np。 下面创建两个并发线程分别绑定在CPU0和CPU1上。#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include int x1;int x2; double waste_time(long n) double res = 0; long i = 0; while (i n * 500000) i+; res += sqrt(i); return res;void* proc

9、1(void*arg) cpu_set_t mask ; CPU_ZERO(&mask); CPU_SET(0,&mask); int ret = 0; ret = pthread_setaffinity_np(pthread_self(),sizeof(mask),(const cpu_set_t*)&mask ); if(ret 900000000) break; x1+; waste_time(1); ret =pthread_getaffinity_np(pthread_self(),sizeof(mask),(const cpu_set_t*)&mask ); if(ret 0) p

10、rintf(pthread_getaffinity_np err n); return ; int j; for( j = 0;j CPU_SETSIZE;j+) if(CPU_ISSET(j,&mask) printf( thread%d bind cpu%dn,pthread_self(),j); void* proc2(void* arg) cpu_set_t mask ; CPU_ZERO(&mask); CPU_SET(2,&mask); int ret = 0; ret =pthread_setaffinity_np(pthread_self(),sizeof(mask),(con

11、st cpu_set_t*)&mask ); if(ret 900000000) break; x2+; waste_time(1); ret = pthread_getaffinity_np(pthread_self(),sizeof(mask),(const cpu_set_t*)&mask ); if(ret 0) printf(pthread_getaffinity_np err n); return ; int j; for( j = 0;j CPU_SETSIZE;j+) if(CPU_ISSET(j,&mask) printf( thread%d bind cpu%dn,pthr

12、ead_self(),j); void main() int ret; pthread_t t1,t2; struct timeval time1,time2; ret = gettimeofday(&time1,NULL); ret = pthread_create(&t1,NULL,proc1,NULL); ret = pthread_create(&t2,NULL,proc2,NULL); pthread_join(t1,NULL); pthread_join(t2,NULL); ret = gettimeofday(&time2,NULL); printf(time spend:%ds %dms n,time2.tv_sec - time1.tv_sec,(time2.tv_usec - time1.tv_usec)/1000);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号