RealtimeLinux(RTAI)-RadboudUniversityNijmegenExercise#3SemaphoresIntroductionSemaphorespermitmultitaskingapplicationstocoordinatetheiractivities.Themostobviouswayfortaskstocommunicateisviavariousshareddatastructures.BecausealltasksinRTAIexistinasinglelinearaddressspace,sharingdatastructuresbetweentasksistrivial.Globalvariables,linearbuffers,ringbuffers,linklists,andpointerscanbereferenceddirectlybycoderunninginadifferentcontext.However,whilesharedaddressspacesimplifiestheexchangeofdata,interlockingaccesstomemoryiscrucialtoavoidcontention.Manymethodsexistforobtainingexclusiveaccesstoresources,andoneofthemissemaphores.ObjectivesThefollowingaretheprimaryobjectivesofthisexercise:TodemonstratetheuseofRTAIsemaphores.DescriptionRTAIsemaphoresprovidefastintertaskcommunicationinRTAI.Semaphoresaretheprimarymeansforaddressingtherequirementsofbothmutualexclusionandtasksynchronization.Ingeneralwecansay:Semaphoresaredatastructureswithacountandtwoassociatedoperations,giveandtake.A'give'(signal)operationincrementsthecountandreturnsimmediately.A'take'operationdecrementsthecountandreturnsimmediately,unlessthecountisalreadyzero.Inthiscasetheoperationblocksuntilanotherprocessgivesthesemaphore.Semaphoreshavethesemanticstodeterminewhichofthepossiblymanyblockedprocessesattemptingtotakeasemaphorewillawakenfirstwhenitisgiven,forexamplefirst-infirst-out(FIFO)orpriority-based(PRIO).Therearethreetypesofsemaphores,optimizedtoaddressdifferentclassesofproblems:BinaryThefastest,mostgeneralpurposesemaphorewhichisoptimizedforsynchronization.Abinarysemaphoreisasemaphorewithamaximumcountof1.Binarysemaphoresareusefultoenforcemutualexclusion:onlyoneprocesscanhavethesemaphoreatanypointoftime,andthenothertakerswillblockuntiltheholderreturnsit.Shareddatawillremainconsistent,sincethereisnopossibilityofbeinginterruptedduringaccess.ResourceAspecialbinarysemaphoreoptimizedforproblemsinherentinmutualexclusion:priorityinheritanceandrecursion.Resourcesemaphoresarespecialbinarysemaphoressuitableformanagingresources.Thetaskthatacquiresaresourcesemaphorebecomesitsowner,alsocalledresourceowner,sinceitistheonlyonecapableofmanipulatingtheresourcethesemaphoreisprotecting.Theownerhasitspriorityincreasedtothatofanytaskblockingonawaittothesemaphore.Suchafeature,calledpriorityinheritance,ensuresthatahighprioritytaskisneverslavedtoalowerpriorityone,thusallowingtoavoidanydeadlockduetopriorityinversion.NotethatresourcesemaphoreswillenforceapriorityqueuingpolicyandcanneveruseFIFOqueuingpolicy.CountingAcountingsemaphoreissimilartothebinarysemaphore,itbutkeepstrackofthenumberoftimesthesemaphoreisgiven.Optimizedforguardingmultipleinstancesofaresource.SemaphoreAPIInkernelmodeyoucanusevoidrt_typed_sem_init(SEM*sem,intvalue,inttype)toinitializeaspecificallytyped(counting,binary,resource)semaphore.Typeisthesemaphoretypeandqueuingpolicy:osemaphorekind:CNT_SEMforcountingsemaphores,BIN_SEMforbinarysemaphores,RES_SEMforresourcesemaphores.oqueuingpolicy:FIFO_Q,PRIO_Qforafifoandpriorityqueuingrespectively.voidrt_sem_init(SEM*sem,intvalue)toinitializeacountingsemaphore.Inbothuserandkernelspace:intrt_sem_delete(SEM*sem)Deleteasemaphore.intrt_sem_signal(SEM*sem)Signaling(giving)asemaphore.intrt_sem_broadcast(SEM*sem)Signalingasemaphore,unblocksalltaskswaitingonit.intrt_sem_wait(SEM*sem)Takeasemaphore.intrt_sem_wait_if(SEM*sem)Takeasemaphore,onlyifthecallingtaskisnotblocked.intrt_sem_wait_until(SEM*sem,RTIMEtime)Waitasemaphorewithtimeout.intrt_sem_wait_timed(SEM*sem,RTIMEdelay)Waitasemaphorewithtimeout.Example:BinarySemaphoreAbinarysemaphorecanbedeclaredasfollows:staticSEMsemBinary;rt_typed_sem_init(&semBinary,1,BIN_SEM|FIFO_Q);Itistakenbythefollowingstatement:rt_sem_wait(&semBinary);Abinarysemaphorecanbeviewedasaflagthatisavailableorunavailable.Whenatasktakesabinarysemaphore,usingrt_sem_wait(),theoutcomedependsonwhetherthesemaphoreisavailableorunavailableatthetimeofthecall.Ifthesemaphoreisavailable,thenthesemaphorebecomesunavailableandthenthetaskcontinuesexecutingimmediately.Ifthesemaphoreisunavailable,thetaskisputinaqueueofblockedtasksandentersastateofpendingontheavailabilityofthesemaphore.Whenataskgivesabinarysemaphore,usingrt_sem_signal(),theoutcomedependsonwhetherthesemaphoreisavailableorunavailableatthetimeofthecall.Ifthesemaphoreisalreadyavailable,givingthesemaphorehasnoeffectatall.Ifthesemaphoreisunavailableandnotaskiswaitingtotakeit,thenthesemaphorebecomesavailable.Ifthesemaphoreisunavailableandoneormoretasksarependingonitsavailability,thenthefirsttaskinthequeueofpendingtasksisunblocked,andthesemaphoreisleftunavailable.Example:ChangingaglobalvariablebytwotasksIntheexamplebelow,twotasks(taskOneandtaskTwo),arecompetingtoupdatethevalueofaglobalvariable,calledglobal.Theobjectiveoftheprogramistotogglethevalueoftheglobalvariable(1sand0s).taskOneincrementsthevalueofglobalandtaskTwodecrementsthevalue.-------------------------------------------------------------------------------------/*fileglobal.cupdateglobalvariablebytwotasks*//*includes*/#includelinux/kernel.h/*decl