Sunteți pe pagina 1din 8

InvestigatingThreads

LearningOutcomes
Attheendofthislabyoushouldbeableto:
LO1. Writeasourcecodethatcreatesthreads
LO2. Usethesimulatortocompileandrunthecode
LO3. Displaythelistofprocesses/threadsandthetreeofprocessesshowingthe
parent/childprocessrelationship
LO4. Demonstratethatthreadssharetheirparentsdataareas
LO5. Modifythesourcecodetocreateaversionnotusingthreadstobeableto
comparethetwoversions
LO6. Simulateamultithreadedserverprogram

Whatarethreads?
ThreadsaresimilartoprocessesandtheytoogetscheduledbytheOS.However,theydo
notnormallyexistinisolation.Allthreadshaveparentprocessesorotherparentthreads.
Threadsareusuallyreferredtoaslightweightprocesses(lwp).Thisisbecausethread
creationandmanagementisnotasdemandingandtimeconsumingasprocesses.Also,
threadsnormallysharetheirparents(andgrandparents)globaldataspacesandother
resources.

TutorialExercises
Inthistutorial,youllinvestigatethethreads(orlightweightprocesses).Todothiswelluse
theCPU/OSsimulator.Youalsoneedtousetheteachingcompilerthatispartofthe
simulator.Pleasenotethatthemethodofcreatingthreads,asshownbelow,isspecificto
theteachingcompilerandthesimulator;otherlanguagessuchasJavaandC++use
differentmethods.
LO1. Writeasourcecodethatcreatesthreads.
StarttheCPU/OSsimulator.Inthecompilerwindowenterthefollowingsourcecode:
program ThreadTest1
sub thread1 as thread
writeln("In thread1")
while true
wend
end sub
sub thread2 as thread
call thread1
1

writeln("In thread2")
while true
wend
end sub
call thread2
writeln("In main")
do
loop
end

Notes:
1. Theasthreadconstructmarksthesubroutineexecutableasathread.
2. YoumaywishtosavetheabovecodeinafileorpasteitinNotebook.

Brieflyexplainwhattheabovecodeisdoing:

Listtheorderinwhichyouexpectthetexttobedisplayedbythewritelnstatements:

LO2. Usethesimulatortocompileandrunthecode.
LO3. Displaythelistofprocesses/threadsandthetreeofprocessesshowingthe
parent/childprocessrelationship.
a) Compiletheabovesourceandloadthegeneratedcodeinmemory.
b) MaketheconsolewindowvisiblebyclickingontheINPUT/OUTPUTbutton.Also
makesuretheconsolewindowstaysontopbycheckingtheStayontopcheck
box.
c) Now,gototheOSsimulatorwindow(usetheOSbuttonintheCPUsimulator
window)andcreateasingleprocessofprogramThreadTest1intheprogramlist
view.ForthisusetheCREATENEWPROCESSbutton.
d) MakesuretheschedulingpolicyselectedisRoundRobinandthatthesimulation
speedissetatmaximum.
e) HittheSTARTbuttonandatthesametimeobservethedisplaysontheconsole
window.


Brieflyexplainyourobservations:

Howmanyprocessesarecreated?

Identify,byname,whichisaprocessandwhichisathread:

f) Now,clickontheViewstabandclickontheVIEWPROCESSLISTbutton.
Observethecontentsofthewindownowdisplaying.

Brieflyexplainyourobservations:

WhatdoyouthinkthePPIDfieldsignifies?

IsthereequivalentinformationinMSWindows?

g) IntheProcessListwindowhitthePROCESSTREEbutton.Observethecontents
ofthewindownowdisplaying.

Brieflyexplainyourobservations:

Howaretheparent/childprocessrelationshipsrepresented?

Identifytheparentandthechildrenprocesses:
IsthereequivalentinformationinMSWindows?
h) StoptherunningprocessesbyrepeatedlyusingtheKILLbuttonintheOS
simulatorwindow.

LO4. Demonstratethatthreadssharetheirparentsdataareas.
Wenowneedtomodifytheaboveprogramstatementsforthenextsetofexercises.You
candothisintwoways:1)Modifytheexistingsource,2)Copythecodeandpasteitintoa
neweditorwindow(youcanusetheNEWbuttonforthisinthecompilerwindow).The

requiredmodificationsareinboldandunderlined.Alsomakesurethatthismodified
programhasadifferentprogramnameasshownbelow.
program ThreadTest2
var s1 string(6)
var s2 string(6)
sub thread1 as thread
s1 = "hello1"
writeln("In thread1")
while true
wend
end sub
sub thread2 as thread
call thread1
s2 = "hello2"
writeln("In thread2")
while true
wend
end sub
call thread2
writeln("In main")
wait
writeln(s1)
writeln(s2)
end

Notes:
1. Thewaitstatementallowstheparentprocesstowaitforitschildrentoterminate
beforeitcontinues.
Brieflyexplainwhateffectthemodificationswillhave:

a) Compiletheabovesourceandloadthegeneratedcodeinmemory.
b) ClickontheSYMBOLTABLEbuttoninthecompilerwindow.Inthedisplayed
window,observetheinformationonvariabless1ands2.
Makeanotebelowofthedatamemoryaddressesforvariabless1ands2:

c) MaketheconsolewindowvisiblebyclickingontheINPUT/OUTPUTbutton.
AlsomakesuretheconsolewindowstaysontopbycheckingtheStayontop
checkbox.
d) Now,gototheOSsimulatorwindowandcreateasingleprocessofprogram
ThreadTest2intheprogramlistview.ForthisusetheCREATENEWPROCESS
button.
e) MakesuretheschedulingpolicyselectedisRoundRobinandthatthesimulation
speedissetatmaximum.
f) HittheSTARTbuttonandafterallthedisplaysontheconsolewindowaredone
usetheSUSPENDbuttontotemporarilysuspendtheprocesses.Identifyand
selectthegrandparentprocess.
g) Now,clickontheSHOWMEMORYbutton.Thisactionwilldisplaythedataarea
oftheselectedprocess.Observethecontentsofthememoryareasofvariables
s1ands2(usetheaddressvaluesyounoteddownin(b)above).
h) Displaythememoryareasofthechildprocessesandagainobservethecontents
ofthememoryareasofvariabless1ands2(notethateachstringvariablesdata
startswiththefirstbytesetto03).

Brieflyexplainwhatyourgeneralobservationsareandsuggestwhatthesignificanceofyour
observationsis:

i) ClickontheRESUMEbuttonandstoptherunningprocessesbyrepeatedlyusing
theKILLbuttonintheOSsimulatorwindow.


LO5. Modifythesourcecodetocreateaversionnotusingthreadstobeabletocompare
thetwoversions.
a) ModifythesourcecodeforThreadTes1byremovingthetwoinstancesofas
threadfromthetwosubroutinedeclarations.Youmaywishtocallthismodified
sourcecodeThreadTest3.
b) Compilethecodeandloadinmemory.
c) CreateaprocessofitandrunintheOSsimulator.
d) Observethedisplayintheconsolewindow.

Brieflydescribewhatyouobserveandexplainhowandwhythisdiffersfromtheresultsof
thepreviousprocesses:

e) StoptherunningprocessbyusingtheKILLbuttonintheOSsimulatorwindow.

LO6. Simulateamultithreadedserverprogram
Inthecompilerwindowenterthefollowingsourcecode:
program ServerTest
var p integer
sub ServiceThread as thread
writeln("Started service")
for i = 1 to 30
next
writeln("Finished service")
end sub
sub ServerThread as thread
while true
read(nowait, p)
select p
case '1'
call ServiceThread
case 'q'
break
case else
6

end select
wend
end sub
call ServerThread
wait
end

a. Compiletheabovesource,loadthegeneratedcodeinmemoryandrunit.
b. MakeanotebelowofwhatyouobserveontheOSsimulatorwindow:

c. Ontheconsolewindowtype1fourtimesoneaftertheotherandobserve
thedisplaysontheconsole.
d. Whenallthethreadsfinishthenmakeanoteofthedisplaysonthe
console.

e. Typeqontheconsoletostoptheserverprogram.
f. Now,modifytheabovecodebyremovingtheasthreadconstructfrom
thesubroutineServiceThread.
g. Compiletheabovesource,loadthegeneratedcodeinmemoryandrunit.
h. Ontheconsolewindowtype1fourtimesoneaftertheotherandobserve
thedisplaysontheconsole.
i.

Whenallthethreadsfinishthenmakeanoteofthedisplaysonthe
console.

j.

Typeqontheconsoletostoptheserverprogram.

k. Comparethedisplaysin(l)withthosein(q)above.Dotheydiffer?Ifthey
dothenexplainbelowwhytheydiffer?

S-ar putea să vă placă și