Linux下进程绑定多CPU运行Word文件下载.docx
《Linux下进程绑定多CPU运行Word文件下载.docx》由会员分享,可在线阅读,更多相关《Linux下进程绑定多CPU运行Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
![Linux下进程绑定多CPU运行Word文件下载.docx](https://file1.bdocx.com/fileroot1/2022-10/19/6e3202ad-a998-4cae-b036-d72e4101aaaa/6e3202ad-a998-4cae-b036-d72e4101aaaa1.gif)
0x00000004(CPU2)
...
如果想设置进程号(PID)为12212的进程到CPU0上的话:
#taskset0x00000001-p12212
或者关掉任务(MySQL),并用taskset将它启动:
#taskset-c1,2,3/etc/init.d/mysqlstart
对于其他进程,也可如此处理(nginx除外,详见下文)。
之后用top查看CPU的使用情况。
二、配置nginx绑定CPU
刚才说nginx除外,是因为nginx提供了更精确的控制。
在conf/nginx.conf中,有如下一行:
worker_processes1;
这是用来配置nginx启动几个工作进程的,默认为1。
而nginx还支持一个名为worker_cpu_affinity的配置项,也就是说,nginx可以为每个工作进程绑定CPU。
我做了如下配置:
worker_processes3;
worker_cpu_affinity001001001000;
这里001001001000是掩码,分别代表第2、3、4颗cpu核心。
重启nginx后,3个工作进程就可以各自用各自的CPU了。
三、代码中绑定
1.如果自己写代码,要把进程绑定到CPU,该怎么做?
可以用sched_setaffinity函数。
在Linux上,这会触发一次系统调用。
intsched_setaffinity(pid_tpid,unsignedintlen,unsignedlong*mask);
sched_setaffinity的第一个参数是pid(进程ID),设置进程为pid的这个进程,让它运行在mask所设定的CPU上。
如果pid的值为0,则表示指定的是当前进程,使当前进程运行在mask所设定的那些CPU上;
第二个参数cpusetsize是mask所指定的数的长度。
通常设定为sizeof(cpu_set_t);
如果当前pid所指定的CPU此时没有运行在mask所指定的任意一个CPU上,则该指定的进程会从其它CPU上迁移到mask的指定的一个CPU上运行。
intsched_getaffinity(pid_tpid,unsignedintlen,unsignedlong*mask);
该函数获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中,即获得指定pid当前可以运行在哪些CPU上。
同样,如果pid的值为0.也表示的是当前进程。
Example:
1./*Shorttestprogramtotestsched_setaffinity
2.*(whichsetstheaffinityofprocessestoprocessors).
3.*Compile:
gccsched_setaffinity_test.c
4.*-osched_setaffinity_test-lm
5.*Usage:
./sched_setaffinity_test
6.*
7.*Opena"
top"
-windowatthesametimeandseeallthework
8.*beingdoneonCPU0firstandafterashortwaitonCPU1.
9.*Repeatwithdifferentnumberstomakesure,itisnota
10.*coincidence.
11.*/
12.
13.#include<
stdio.h>
14.#include<
math.h>
15.#include<
sched.h>
16.
17.doublewaste_time(longn)
18.{
19.doubleres=0;
20.longi=0;
21.while(i<
n*200000){
22.i++;
23.res+=sqrt(i);
24.}
25.returnres;
26.}
27.
28.intmain(intargc,char**argv)
29.{
30.unsignedlongmask=1;
/*processor0*/
31.
32./*bindprocesstoprocessor0*/
33.if(sched_setaffinity(0,sizeof(mask),&
mask)<
0){
34.perror("
sched_setaffinity"
);
35.}
36.
37./*wastesometimesotheworkisvisiblewith"
*/
38.printf("
result:
%f\n"
waste_time(2000));
39.
40.mask=2;
/*processswitchestoprocessor1now*/
41.if(sched_setaffinity(0,sizeof(mask),&
42.perror("
43.}
44.
45./*wastesomemoretimetoseetheprocessorswitch*/
46.printf("
47.}
pthread_setaffinity_npexample:
1./*Shorttestprogramtotestthepthread_setaffinity_np
2.*(whichsetstheaffinityofthreadstoprocessors).
gccpthread_setaffinity_np_test.c
4.*-opthread_setaffinity_np_test-lm-lpthread
./pthread_setaffinity_test
pthread.h>
21.while(i<
23.res+=sqrt(i);
28.void*thread_func(void*param)
33.if(pthread_setaffinity_np(pthread_self(),sizeof(mask),
34.&
35.perror("
pthread_setaffinity_np"
36.}
37.
38./*wastesometimesotheworkisvisiblewith"
39.printf("
waste_time(2000));
40.
41.mask=2;
42.if(pthread_setaffinity_np(pthread_self(),sizeof(mask),
43.&
44.perror("
45.}
46.
47./*wastesomemoretimetoseetheprocessorswitch*/
48.printf("
49.}
50.
51.intmain(intargc,char*argv[])
52.{
53.pthread_tmy_thread;
54.
55.if(pthread_create(&
my_thread,NULL,thread_func,
56.NULL)!
=0){
57.perror("
pthread_create"
58.}
59.pthread_exit(NULL);
60.}
voidCPU_ZERO(cpu_set_t*set)
这个宏对CPU集set进行初始化,将其设置为空集。
voidCPU_SET(intcpu,cpu_set_t*set)
这个宏将cpu加入CPU集set中。
voidCPU_CLR(intcpu,cpu_set_t*set)
这个宏将cpu从CPU集set中删除。
intCPU_ISSET(intcpu,constcpu_set_t*set)
如果cpu是CPU集set的一员,这个宏就返回一个非零值(true),否则就返回零(false)。
#include<
stdlib.h>
#include