RTAI习题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

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.SemaphoreAPIInkernelmodeyoucanusevoidrt_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

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功