InvestigatingSynchronisation
Introduction
Attheendofthislabyoushouldbeableto:
1. Showthatwritingtounprotectedsharedglobalmemoryregioncanhave
undesirablesideeffectswhenaccessedbythreadsatthesametime.
2. Understandsharedglobalmemoryprotectionusingsynchronisedthreads.
3. Explainhowcriticalregionsofcodecanprotectsharedglobalmemoryareas.
4. Showthatmemoryareaslocaltothreadsareunaffectedbyotherthreads.
ProcessorandOSSimulators
Thecomputerarchitecturetutorialsaresupportedbysimulators,whichare
createdtounderpintheoreticalconceptsnormallycoveredduringthelectures.
Thesimulatorsprovidevisualandanimatedrepresentationofmechanisms
involvedandenablethestudentstoobservethehiddeninnerworkingsof
systems,[Link]
advantageofusingsimulatorsisthattheyallowthestudentstoexperimentand
exploredifferenttechnologicalaspectsofsystemswithouthavingtoinstalland
configuretherealsystems.
BasicTheory
Concurrentprocessesaccessingglobalsharedresourcesatthesametimecan
[Link]
hardwareandoperatingsystemcanprovidesupportforimplementingcritical
regionsofcodewhengloballyaccessibleresourcesaresharedbyseveral
concurrentlyexecutingthreads.
Lab Exercises - Investigate and Explore
[Link]
[Link],youneedtouse
[Link],openthecompiler
windowbyselectingtheCOMPILERbuttoninthecurrentwindow.
1. Inthecompilerwindow,enterthefollowingsourcecodeinthecompilersource
editorarea(underPROGRAMSOURCEframetitle).Makesureyourprogramis
exactlythesameastheonebelow(besttousecopyandpasteforthis).
program CriticalRegion1
var g integer
sub thread1 as thread
writeln("In thread1")
g = 0
for n = 1 to 20
g = g + 1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread
writeln("In thread2")
g = 0
for n = 1 to 12
g = g + 1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
[Link]
[Link]
theglobalvariablegintwoseparateloops.
2
Workoutwhattwovaluesofgyouwouldexpecttobedisplayedontheconsole
whenthetwothreadsfinish?
Compilingandloadingtheabovecode:
i) CompiletheabovecodeusingtheCOMPILEbutton.
ii) LoadtheCPUinstructionsinmemoryusingtheLOADINMEMORYbutton.
iii) DisplaytheconsoleusingtheINPUT/OUTPUTbuttoninCPUsimulator.
iv) OntheconsolewindowchecktheStayontopcheckbox.
Runningtheabovecode:
i) EntertheOSsimulatorusingtheOS0buttoninCPUsimulator.
ii) Youshouldseeanentry,titledCriticalRegion1,inthePROGRAMLISTview.
iii) CreateaninstanceofthisprogramusingtheNEWPROCESSbutton.
iv) SelectRoundRobinoptionintheSCHEDULER/Policiesview.
v) Select10ticksfromthedropdownlistinRRTimeSliceframe.
vi) Makesuretheconsolewindowisdisplaying(seeabove).
vii) MovetheSpeedslidertothefastestposition.
viii) StarttheschedulerusingtheSTARTbutton.
Now,followtheinstructionsbelowwithoutanydeviations:
2. Whentheprogramstopsrunning,[Link]
thesevalueswhatyouwereexpecting?Explainifthereareanydiscrepancies.
3. ChangeRRTimeSliceintheOSsimulatorwindowto5ticksandrepeattheabove
[Link],[Link]
thevaluesin(2)above?Ifso,explainwhy.
4. [Link].
RenametheprogramCriticalRegion2.
program CriticalRegion2
var g integer
sub thread1 as thread synchronise
writeln("In thread1")
g = 0
for n = 1 to 20
g = g + 1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread synchronise
writeln("In thread2")
g = 0
for n = 1 to 12
g = g + 1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
NOTE:Thesynchronisekeywordmakessurethethread1andthread2codeare
executedmutuallyexclusively([Link]).
5. [Link],runitandcarefully
[Link]
theresultsdifferentthanthosein(2)and(3)above?Ifso,why?
6. [Link]
[Link].RenameitCriticalRegion3.
program CriticalRegion3
var g integer
sub thread1 as thread
writeln("In thread1")
enter
g = 0
for n = 1 to 20
g = g + 1
next
writeln("thread1 g = ", g)
leave
writeln("Exiting thread1")
end sub
sub thread2 as thread
writeln("In thread2")
enter
g = 0
for n = 1 to 12
g = g + 1
next
writeln("thread2 g = ", g)
leave
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
NOTE:Theenterandleavekeywordpairprotecttheprogramcodebetween
[Link]
theCPUwithanyotherthread.
7. LocatetheCPUassemblyinstructionsgeneratedfortheenterandleavekeywordsin
[Link]
[Link].
Makeanoteofthisinstructionhere:
8. [Link],[Link]
ofthetwovaluesofvariableg.
9. [Link]
[Link],[Link]
CriticalRegion4.
program CriticalRegion4
sub thread1 as thread
var g integer
writeln("In thread1")
g = 0
for n = 1 to 20
g = g + 1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread
var g integer
writeln("In thread2")
g = 0
for n = 1 to 12
g = g + 1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
10. [Link],[Link]
[Link]
(1),(4)and(6)above?
11. Sowhathavewedonesofar?Tohelpunderstandtheorybettertrytoanswerthe
[Link],soitisimportantthat
[Link],youdontneedtocompletethispart
duringthetutorialsession.
a) Brieflyexplainthemainpurposeofthistutorialasyouunderstandit.
b) Whyhavewechosentodisplaythesameglobalvariableginboththreads?
c) Whatpopularhighlevellanguageusesthekeywordsynchronise(orsimilar)for
thesamepurposeasthecodein(4)?
d) [Link]
[Link].
e) SomecomputerarchitectureshaveatestandsetCPUinstructionfor
[Link]
separatesheet.
f) Intheabsenceofanyhelpfromhardwareandoperatingsystem,howwouldyou
protectacriticalregioninyourcode?Suggestawayofdoingitandstatehowit
woulddifferfromtheabovemethods(hint:busywait).