多核处理器LINUX的进程绑定处理器核运行Word文件下载.docx
- 文档编号:17780819
- 上传时间:2022-12-10
- 格式:DOCX
- 页数:15
- 大小:980.72KB
多核处理器LINUX的进程绑定处理器核运行Word文件下载.docx
《多核处理器LINUX的进程绑定处理器核运行Word文件下载.docx》由会员分享,可在线阅读,更多相关《多核处理器LINUX的进程绑定处理器核运行Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
voidCPU_CLR(intcpu,cpu_set_t*set)
这个宏将cpu从CPU集set中删除。
intCPU_ISSET(intcpu,constcpu_set_t*set)
如果cpu是CPU集set的一员,这个宏就返回一个非零值(true),否则就返回零(false)。
Example:
我是在一个虚拟机上运行的程序,机器CPU是双核的,我设置虚拟机模拟四核。
在linux上执行top指令看结果,点击“1”查看每个CPU核的运行情况。
/*Shorttestprogramtotestsched_setaffinity
*(whichsetstheaffinityofprocessestoprocessors).
*Compile:
gccsched_setaffinity_test.c
*-osched_setaffinity_test.o-lm
*Usage:
./sched_setaffinity_test.o
*
*Opena"
top"
-windowatthesametimeandseeallthework
*beingdoneonCPU0firstandafterashortwaitonCPU1,2,3.
*Repeatwithdifferentnumberstomakesure,itisnota
*coincidence.
*/
#include<
stdio.h>
math.h>
sched.h>
doublewaste_time(longn)
{
doubleres=0;
longi=0;
while(i<
n*200000){
i++;
res+=sqrt(i);
}
returnres;
}
intmain(intargc,char**argv)
unsignedlongmask=1;
/*二进制1,processor0*/
/*bindprocesstoprocessor0*/
if(sched_setaffinity(0,sizeof(mask),&
mask)<
0){
perror("
sched_setaffinity"
);
/*wastesometimesotheworkisvisiblewith"
*/
printf("
result:
%f\n"
waste_time(2000));
mask=2;
/*二进制10,processswitchestoprocessor1now*/
/*wastesomemoretimetoseetheprocessorswitch*/
mask=4;
/*二进制100,processor2*/
/*bindprocesstoprocessor2*/
mask=8;
/*二进制1000,processswitchestoprocessor3now*/
}
2、运行结果
图2-1:
图中显示编译及执行一个使用“CPU亲和力”的程序。
图2-2可以看见CPU0已在执行程序(CPU达到100.0%?
有必要吗),第一段程序的结果还未出来。
图2-3可以看见CPU1已在执行程序,第一段程序的结果已经显示(图片中显示了上一次执行的结果,因为截图比较慢,执行了3次^-^),CPU1执行的结果还未显示出来。
图2-4:
可以看见CPU2已在执行程序,第二段程序(CPU1执行)的结果已经显示,CPU2执行的程序结果还未出来。
图2-5:
:
CPU3再执行程序。
图2-6:
结果已经显示,CPU恢复到执行之前的状态
二、多进程(在两个核上各自运行一个进程)
1、代码如下
/*Shorttestprogramtotestparallel_setaffinity_test.c
gccparallel_setaffinity_test.c
*-oparallel_setaffinity_test.o-lm
./parallel_setaffinity_test.o
*beingdoneonCPU0firstandafterashortwaitonCPU1.
#include<
stdlib.h>
sys/types.h>
sys/sysinfo.h>
unistd.h>
string.h>
#include<
doublewaste_time(longn)
{
doubleres=0;
longi=0;
while(i<
i++;
res+=sqrt(i);
}
returnres;
intmain()
charbuf[100];
pid_tcld_pid;
intfd;
intstatus;
unsignedlongmask=0;
strcpy(buf,"
Thisisparentprocesswrite!
\n"
if((cld_pid=fork())==0)
{
strcpy(buf,"
Thisischildprocesswrite!
printf("
Thisischildprocess!
MyPID(child)is%d\n"
getpid());
getppid());
mask=1;
/*bindprocesstoprocessor0*/
if(sched_setaffinity(0,sizeof(mask),&
perror("
}
/*wastesometimesotheworkisvisiblewith"
printf("
%f.\n"
write(fd,buf,strlen(buf));
close(fd);
exit(0);
else
Thisisparentprocess!
MyPID(parent)is%d.\n"
MychildPIDis%d.\n"
cld_pid);
mask=2;
/*wastesomemoretimetoseetheprocessorswitch*/
wait(&
status);
return0;
图3-1:
图中显示编译及执行一个使用“CPU亲和力”的多进程程序,双核(虚拟四核太慢)。
图3-2:
,父子进程并发执行,父进程在CPU1上运行,子进程运行在CPU0,可以看出两个处理器目前都在运行程序。
图3-3:
结果已经显示,CPU0和CPU1都恢复到执行之前的状态
附录
1、Processoraffinity
isamodificationofthenativecentralqueue
schedulingalgorithm
ina
symmetricmultiprocessing
operatingsystem.Eachtask(beitprocessorthread)inthequeuehasatagindicatingitspreferred/kin
processor.Atallocationtime,eachtaskisallocatedtoitskinprocessorinpreferencetoothers.
Processoraffinitytakesadvantageofthefactthatsomeremnantsofaprocessmayremaininoneprocessor'
sstate(inparticular,inits
cache)fromthelasttimetheprocessran.Schedulingittorunonthesameprocessorthenexttimecouldresultintheprocess'
srunningmoreefficientlybyreducingperformance-degradingsituationssuchascachemisses.Apracticalexamplemightberunningmultipleinstancesofanapplicationwhichdoesnotusemultiplethreads,suchassomegraphics-renderingsoftware.Overallsystemefficiencyincreases.
Schedulingalgorithmimplementationsvaryinadherencetoprocessoraffinity.Undercertaincircumstancessomeimplementationswillallowatasktochangetoanotherprocessorifthisisdeemedtobemostefficient.Thismaybethecaseiftwoprocessor-intensivetasks(A&
B)haveaffinitytooneprocessorwhileanotherprocessorliesunused.ManyalgorithmswouldshifttaskBtothesecondprocessorinordertomaximizeprocessoruse.TaskBwouldthenacquireaffinitywiththesecondprocessorwhiletaskAwouldcontinuetohaveaffinitywiththeoriginalprocessor.
Processoraffinitycaneffectivelyreducecacheproblemsbutitdoesnotcurbthepersistent
load-balancing
problem.[1]
Further,processoraffinitybecomesmorecomplicatedinsystemswithnon-uniformarchitectures.Forexample,asystemwithtwodual-core
hyper-threaded
CPUspresentsachallengetoaschedulingalgorithm.ThereiscompleteaffinitybetweentwovirtualCPUsimplementedonthesamecoreviahyper-threading,partialaffinitybetweentwocoresonthesamephysicalchip(asthecoressharesome,butnotall,cache),andnoaffinitybetweenseparatephysicalchips.
Asotherresourcesarealsoshared,processoraffinityalonecannotbeusedasthebasisforCPUdispatching.Ifaprocesshasrecentlyrunononevirtualhyper-threadedCPUinagivencore,andthatvirtualCPUiscurrentlybusybutitspartnerisnot,cacheaffinitywouldsuggestthattheprocessshouldbedispatchedtotheidlepartner.However,thetwovirtualCPUscompeteforessentiallyallcomputing,cache,andmemoryresources.ItwouldtypicallybemoreefficientinthiscasetodispatchtheprocesstoadifferentcoreorCPUifoneisavailable.Thiswouldlikelyincurapenaltywhenprocessrepopulatesthecache,butoverallperformancewouldlikelybehigherastheprocesswouldnothavetocompeteforresourceswithintheCPU.
On
Linux
theCPUaffinityofaprocessmightbealteredwiththetaskset
(1)program.[2]
SGI
systems,
dplace
bindsaprocesstoasetofCPUs.[3]NetBSD
5.0,
FreeBSD
7.2andlaterversionscanusepthread_setaffinity_npandpthread_getaffinity_np.[4]
In
NetBSD,thepsrsetutility[5]
tosetathread'
saffinitytoacertainCPUset.In
FreeBSD,cpuset[6]
utilityisusedtocreateCPUsetsandtoassignprocessestothesesets.On
WindowsNT,threadandprocessCPUaffinitiescanbesetseparatelybyusingSetThreadAffinityMask[7]
andSetProcessAffinityMask[8]
APIcallsorviatheTaskManagerinterface(forprocessaffinityonly).
MacOSX
exposesan
affinityAPI
thatprovideshintstothekernelhowtoschedulethreadsaccordingtoaffinitysets.
2、sched_setaffinity,sched_getaffinity-setandgetaprocess'
sCPUaffinitymask
SYNOPSIS
DESCRIPTION
sched_setaffinitysetstheCPUaffinitymaskoftheprocessdenotedbypid.Ifpidiszero,thenthecurrentprocessisused.
Theaffinitymaskisrepresentedbythebitmaskstoredinmask.Theleastsignificantbitcorrespondstothefirstlogicalprocessornumberonthesystem,whilethemostsignificantbitcorrespondstothelastlogicalprocessornumberonthesystem.AsetbitcorrespondstoalegallyschedulableCPUwhileanunsetbitcorrespondstoanillegallyschedulableCPU.Inotherwords,aprocessisboundtoandwillonlyrunonprocessorswhosecorrespondingbitisset.Usually,allbitsinthemaskareset.
Theargumentlenisthelengthofthedatapointedtobymask.Normallythisisthesizeofawordonthesystem.ForcompatibilitywithfutureversionsoftheLinuxkernel,sincethissizecanchange,thebitmasksuppliedmustbeatleastaslargeastheaffinitymaskstoredinthekernel.
Thefunction
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多核 处理器 LINUX 进程 绑定 运行