RealtimeLinux(RTAI)-RadboudUniversityNijmegenExercise#2Multi-TaskingIntroductionModernreal-timesystemsarebasedonthecomplementaryconceptsofmultitaskingandintertaskcommunications.Amultitaskingenvironmentallowsreal-timeapplicationstobeconstructedasasetofindependenttasks,eachwithaseparatethreadofexecutionanditsownsetofsystemresources.Theintertaskcommunicationfacilitiesallowthesetaskstosynchronizeandcoordinatetheiractivities.TheRTAImultitaskingscheduler,usesinterrupt-driven,priority-basedtaskscheduling.Itfeaturesfastcontextswitchtimeandlowinterruptlatency.ObjectivesThefollowingaretheprimaryobjectivesofthisexercise:ToteachthestudenthowtoinitiatemultipleprocessesusingRTAItaskingroutines.DescriptionMultitaskingcreatestheappearanceofmanythreadsofexecutionrunningconcurrentlywhen,infact,thekernelinterleavestheirexecutiononthebasisofaschedulingalgorithm.Eachapparentlyindependentprogramiscalledatask.Eachtaskhasitsowncontext,whichistheCPUenvironmentandsystemresourcesthatthetaskseeseachtimeitisscheduledtorunbythekernel.Onacontextswitch,atask'scontextissavedintheTaskControlBlock(TCB).Atask'scontextincludes:athreadofexecution,thatis,thetask'sprogramcountertheCPUregistersandfloating-pointregistersifnecessaryastackofdynamicvariablesandreturnaddressesoffunctioncallsI/Oassignmentsforstandardinput,output,erroradelaytimeratimeslicetimerkernelcontrolstructuressignalhandlersdebuggingandperformancemonitoringvaluesSchedulinginRTAILinuxThereal-timeschedulerisstartedtogetherwiththereal-timetimerbythecommandstart_rt_timer.InRTAILinux,thereal-timeschedulerisdrivenbytimerinterruptsfromthePC's8254ProgrammableIntervalTimer(PIT)chip,whichcanbeprogrammedtogeneratetiminginterruptsatcertainmoments.(ThischipisalsousedbythenormalLinuxscheduler.)Theresolutionisbasedonfrequencyofthe8254chipfrequency(1,193,180Hz),i.e.,thereistickofthe8254chipevery838nanoseconds.InadditionwecanusetheTimeStampClock(TSC)ofthePC,basedonaninternalcrystalwhichdrivesthePC.EachseparateCPUhasitsownreal-timetimerandeachcanruninperiodicorone-shotmodeindependentofalltheotherCPU's.Internalcountunitsmeasuretheamountofelapsedtime;howthisisdonedependsonthemode.ThetypeoftheseinternalcountunitsisRTIME.Inpureperiodicmode:oTheinternalcountunitsaretheticksofthe8254chip.oTheparameterofthestart_rt_timerisusedtospecifyaso-calledbaseperiodintermsofinternalcountunits(i.e.,8254ticks).The8254PITisprogrammedtoexpirerepeatedlyafterthespecifiedamountofinternalcounts,providinginterruptswiththefrequencyofthebaseperiod.Alltaskswillrunatamultipleofthisbaseperiod.oThefunctionrt_get_time()returnsthenumbersofinternalcountunits(i.e.,8254ticks)passedsincestart_rt_timerwascalled.oTheadvantageisthattimerreprogrammingisavoided,whichsavesabout3microseconds.oThedisadvantageisthatalltasksareforcedtorunatmultiplesofthebaseperiod.Inone-shotmode:oTheinternalcountunitsusedequaltheTSCcount.oThe8254PITisreprogrammedattheendofeachtask,suchthatitexpiresexactlywhenthenexttaskisscheduledtorun.oFunctionrt_get_time()againreturnsthenumbersofinternalcountunits,nowthiscorrespondstotheTSCcount.oTheadvantageisthatthetimingoftaskscanvarydynamicallyandnewtaskscanbeaddedwithouthavingtobotheraboutanybaseperiod.oThedisadvantageisrepeatedtimerreprogrammingoverhead.NoticethatthetimerwillalwaysrunonafrequencylowerthantheTSCoftheCPU,buttheTSCfrequencyisusuallymuchhigherthanthatofthe8254chip(300timesfora300MHzCPU).StartingtheTimerandRealtimeSchedulerThefirstthingwealwayshavetodoatthestartofareal-timeprogramistostartthereal-timetimerandtheschedulerusingthestart_rt_timercommand.Westartthetimertoexpireinpureperiodicmodebycallingthefunctionsvoidrt_set_periodic_mode(void);RTIMEstart_rt_timer(RTIMEperiod);Thevaluepassedto'start_rt_timer()'inperiodicmodeisthe,abovementioned,desiredbaseperiod,specifiedininternalcounts(i.e.8354ticks).ThereturnvalueoftypeRTIMEyieldstherealizednumberofcounts.Westartthetimertoexpireinone-shotmodebycallingthefunctionsvoidrt_set_oneshot_mode(void);RTIMEstart_rt_timer(RTIMEperiod);Theargumentforstart_rt_timerischosenrandomlybecausewedon'thaveaperiod,anditwillthereforebeignoredbyrt_start_timer.Sowecanforinstanceusestart_rt_timer(1).ThefunctionRTIMEnano2counts(intnanoseconds);canbeusedtoconverttimeinnanosecondstotheseRTIMEinternalcountunits.Inperiodicmodethisisquantizedtotheresolutionofthe8254chipfrequency(1,193,180Hz).TheTaskFunctionEachtaskisassociatedwithafunctionthatiscalledwhenthetaskisscheduledtorun.ThisfunctionisausualCfunctionrunninginperiodmodethattypicallyreadssomeinputs,computessomeoutputsandwaitsforthenextcycle.Suchataskfunctionshouldenteranendlessloop,inwhichisdoesitswork,andthencallsvoidrt_task_wait_period(void);towaitfortheitsnextscheduledcycle.Typicalcodelookslikethis:voidtask_function(intarg){while(1){/*Doyourthinghere*/rt_task_wait_period();}return;}SettingUptheTaskStructureAnRT_TASKdatastructureisusedtoholdalltheinformationaboutatask:othetaskfunction,oanyinitialargumentpassedtoit,othesizeofthestackallocatedforitsvariables,oitspriority,owhetherornotitusesfloating-pointmath,oandasigna