4.
1Ov
erv
iew
A thread isabasi cuni tofCPUut il
i
zati
on,consi
st i
ngofaprogram
count er,ast ack,andasetofr egister
s,(andat hreadI
D.)
Traditional (heavy weight)processeshav easinglethr
eadofcont rol
-Ther eisonepr ogr am counter,andonesequenceofi nst
ructi
ons
thatcanbecar ri
edoutatanygi v enti
me.
Asshowni nFi gure4.1,mul ti
-t
hreadedapplicat
ionshavemul t
ipl
e
threadswi thinasi ngl
epr ocess,eachhav i
ngtheirownprogram
count er,stackandsetofr egi
sters,butshari
ngcommoncode, data,
andcer t
ainst ructuressuchasopenf i
l
es.
Fi
gur
e4.
1-Si
ngl
e-t
hreadedandmul
ti
thr
eadedpr
ocesses
4.
1.1Mot
ivat
ion
Threadsarev eryusefulinmoder nprogrammi ngwhenev era
processhasmul ti
pletaskstoperformindependent l
yofthe
others.
Thisispart
icularlyt
ruewhenoneoft het asksmaybl ock,andit
i
sdesi r
edtoal l
owt heot hertaskstoproceedwi thoutblocking.
Forexamplei nawor dprocessor,abackgroundt hr
eadmay
checkspell
ingandgr ammarwhi l
eaf or
egr oundt hr
ead
processesuseri nput(keystrokes),whil
ey etat hir
dthread
l
oadsi magesf rom thehar ddri
ve,andaf ourthdoesper iodic
automat icbackupsoft hefi
lebeingedited.
Anot herexampleisawebser ver-Multi
plethreadsal l
owfor
mul t
iplerequeststobesatisfi
edsimul t
aneously,without
havingt oservi
cerequestssequent i
all
yortoforkof fseparat
e
processesf oreveryincomingrequest.(Thelatteri
showt hi
s
sortoft hi
ngwasdonebef oretheconceptoft hreadswas
[Link] dli
stenatapor t,
forkoffachi l
dfor
everyincomi ngrequesttobepr ocessed,andthengobackt o
l
isteningtothepor t.)
Fi
gur
e4.
2-Mul
ti
thr
eadedser
verar
chi
tect
ure
4.
1.2Benef
it
s
Ther
earef ourmaj orcat egor iesofbenef i
tst omul t
i-
threading:
[Link] veness-Onet hreadmaypr ov ider apidresponse
whi l
eot herthreadsar ebl ockedorsl oweddowndoi ng
int
ensi vecalculat ions.
[Link] ceshar ing-Bydef aultthr eadsshar ecommon
code, data,andot herr esources, whichal l
owsmul ti
ple
taskst obeper for medsi mul taneousl yi nasi ngleaddr ess
space.
[Link]-Cr eat ingandmanagi ngt hreads(andcont ext
swi t
chesbet weent hem )i smuchf ast ert hanper formi ng
thesamet asksf orpr ocesses.
[Link]
ty,i
.[Link] ilizationofmul tiprocessorar chitectures
-Asi nglethreadedpr ocesscanonl yr unononeCPU, no
mat terhowmanymaybeav ail
abl e,wher east he
execut i
onofamul ti
-t
hr eadedappl i
cationmaybespl i
t
amongstav ailabl epr ocessor s.(Not et hatsi ngle
threadedpr ocessescanst i
llbenef i
tf r
om mul ti
-
processorar chi tect ureswhent her earemul ti
ple
processescont endi ngf ortheCPU, i
.[Link] heload
averagei sabov esomecer taint hreshol d.)
4.
3Mul
ti
thr
eadi
ngModel
s
Ther earet wot ypesoft hreadstobemanagedi namoder nsystem:User
threadsandker nel t
hreads.
Usert hreadsar esuppor tedabov et heker nel
,[Link]
aret hethreadst hatappl i
cati
onpr ogrammer swoul dputintot hei
rprograms.
Ker nelt
hr eadsar esuppor tedwit
hint heker neloftheOSi tself
.Allmoder n
OSessuppor tker nell
evel thr
eads, all
owi ngtheker nelt
oper f
or m mult
iple
simul t
aneoust asksand/ ortoservicemul ti
plekernelsystem call
s
simul t
aneousl y.
Inaspeci fi
cimpl ement ation,t
heusert hreadsmustbemappedt okernel
threads, usingoneoft hef oll
owingst rategies.
4.
3.1Many
-To-
OneModel
Inthemany -t
o-onemodel ,
manyuser -
lev elthreadsar eallmappedont o
asingleker nelthread.
Threadmanagementi shandl edbyt hethr eadl ibr
aryinuserspace,
whichi sv er
yef fi
cient.
Howev er,i
fabl ockingsy stem call
ismade, t hent heentir
epr ocess
blocks, eveniftheot herusert hr
eadswoul dot herwisebeabl eto
continue.
Becauseasi ngleker nel threadcanoper ateonl yonasi ngleCPU, the
many -to-onemodel doesnotal l
owindividual processest obespl i
t
acrossmul t
ipleCPUs.
Greent hreadsf orSol arisandGNUPor tableThr eadsimpl ementt he
many -to-onemodel int hepast ,butfewsy stemscont inuetodoso
today.
Fi
gur
e4.
5-Many
-to-
onemodel
4.
3.2One-
To-
OneModel
Theone- t
o-onemodel cr
eatesaseparatekernelthr
eadtohandleeach
userthr ead.
One- t
o-onemodel overcomestheproblemsl i
stedaboveinvol
vi
ng
blockingsy stem call
sandt hesplit
ti
ngofpr ocessesacrossmulti
ple
CPUs.
Howev ertheov erheadofmanagi ngtheone- t
o-onemodel ismore
signif
icant,involvi
ngmor eover
headandsl owingdownt hesystem.
Mosti mpl ement ati
onsoft hismodelplacealimitonhowmanyt hr
eads
canbecr eated.
LinuxandWi ndowsf rom 95toXPi mplementtheone-to-onemodelfor
threads.
Fi
gur
e4.
6-One-
to-
onemodel
4.
3.3Many
-To-
ManyModel
Themany -t
o-manymodel mult
ipl
exesanynumberofusert hr
eadsonto
anequal orsmal l
ernumberofkernelthreads,combiningthebest
featuresoftheone-to-
oneandmany -
to-onemodel s.
User shavenor est
ri
cti
onsonthenumberoft hreadscreated.
Blockingkernelsyst
em cal
lsdonotbl ocktheent i
reprocess.
Processescanbespl i
tacr
ossmul t
ipl
epr ocessors.
Indivi
dualprocessesmaybeallocatedv ari
abl
enumber sofkernel
threads,dependingonthenumberofCPUspr esentandotherfact
ors.
Fi
gur
e4.
7-Many
-to-
manymodel
Onepopularvar
iat
ionoft
hemany -
to-
manymodelisthetwo-t
iermodel
,
whichall
owseit
hermany-t
o-manyorone-
to-
oneoperat
ion.
I
RIX,HP-UX,andTru64UNIXusethetwo-
ti
ermodel
,asdidSolari
spri
or
t
oSol ar
is9.
Fi
gur
e4.
8-Two-
lev
elmodel