Basic Concepts of RTOS

Embed Size (px)

Citation preview

  • 7/27/2019 Basic Concepts of RTOS

    1/6

    Basic concepts of real-time operating systems

    by David Kalinsky (Nov. 18, 2003)

    The fundamentals

    To most people, embedded systems are not reconi!able as comp"ters. #nstead, t$ey are $idden inside everydayob%ects t$at s"rro"nd "s and $elp "s in o"r lives. &mbedded systems typically do not inter'ace it$ t$e o"tsideorld t$ro"$ 'amiliar personal comp"ter inter'ace devices s"c$ as a mo"se, keyboard and rap$ic "ser inter'ace.#nstead, t$ey inter'ace it$ t$e o"tside orld t$ro"$ "n"s"al inter'aces s"c$ as sensors, act"ators and speciali!edcomm"nication links.

    eal*time and embedded systems operate in constrained environments in $ic$ comp"ter memory and processinpoer are limited. T$ey o'ten need to provide t$eir services it$in strict time deadlines to t$eir "sers and to t$es"rro"ndin orld. #t is t$ese memory, speed and timin constraints t$at dictate t$e "se o' real*time operatinsystems in embedded so'tare.

    Basic kernel services

    #n t$e disc"ssion belo, e ill 'oc"s on t$e +kernel+ t$e part o' an operatin system t$at provides t$e most basicservices to application so'tare r"nnin on a processor.

    T$e +kernel+ o' a real*time operatin system (+T-+) provides an +abstraction layer+ t$at $ides 'rom applicationso'tare t$e $ardare details o' t$e processor (or set o' processors) "pon $ic$ t$e application so'tare ill r"n.T$is is s$on in /i"re1.

    Figure 1: An RTOS ernel provides an A!straction "ayer !et#een Application Soft#are and $m!edded%ard#are

    #n providin t$is +abstraction layer+ t$e T- kernel s"pplies 'ive main cateories o' basic services to applicationso'tare, as seen in /i"re 2.

    Figure &: Basic Services 'rovided !y a Real-Time Operating System ernel

  • 7/27/2019 Basic Concepts of RTOS

    2/6

    T$e most basic cateory o' kernel services, at t$e very center o' /i"re 2, is Task anaement. T$is set o' servicesallos application so'tare developers to desin t$eir so'tare as a n"mber o' separate +c$"nks+ o' so'tare **eac$ $andlin a distinct topic, a distinct oal, and per$aps its on real*time deadline. &ac$ separate +c$"nk+ o'so'tare is called a +task.+ ervices in t$is cateory incl"de t$e ability to la"nc$ tasks and assin priorities to t$em.T$e main T- service in t$is cateory is t$e sc$ed"lin o' tasks as t$e embedded system is in operation. T$eTask c$ed"ler controls t$e eec"tion o' application so'tare tasks, and can make t$em r"n in a very timely andresponsive 'as$ion. ater, e ill see t$e details o' $o t$is is done.4

    T$e second cateory o' kernel services, s$on at t$e top o' /i"re 2, is #ntertask 5omm"nication andync$roni!ation. T$ese services make it possible 'or tasks to pass in'ormation 'rom one to anot$er, it$o"t danero' t$at in'ormation ever bein damaed. T$ey also make it possible 'or tasks to coordinate, so t$at t$ey canprod"ctively cooperate it$ one anot$er. 6it$o"t t$e $elp o' t$ese T- services, tasks mi$t ell comm"nicatecorr"pted in'ormation or ot$erise inter'ere it$ eac$ ot$er.

    ince many embedded systems $ave strinent timin re7"irements, most T- kernels also provide some basicTimer services, s"c$ as task delays and time*o"ts. T$ese are s$on on t$e ri$t side o' /i"re 2.

    any (b"t not all) T- kernels provide Dynamic emory llocation services. T$is cateory o' services allostasks to +borro+ c$"nks o' memory 'or temporary "se in application so'tare. -'ten t$ese c$"nks o' memoryare t$en passed 'rom task to task, as a means o' 7"ickly comm"nicatin lare amo"nts o' data beteen tasks.ome very small T- kernels t$at are intended 'or ti$tly memory*limited environments, do not o''er Dynamicemory llocation services.

    any (b"t not all) T- kernels also provide a +Device #9- "pervisor+ cateory o' services. T$ese services, i'available, provide a "ni'orm 'rameork 'or orani!in and accessin t$e many $ardare device drivers t$at aretypical o' an embedded system. /or more in'ormation on t$is, please visit: t$e device drivers paeat t$e Kalinskyssociates 6ebsite4

    #n addition to kernel services, many T-s o''er a n"mber o' optional add*on operatin system components 'ors"c$ $i$*level services as 'ile system orani!ation, netork comm"nication, netork manaement, databasemanaement, "ser*inter'ace rap$ics, etc. lt$o"$ many o' t$ese add*on components are m"c$ larer and m"c$more comple t$an t$e T- kernel, t$ey rely on t$e presence o' t$e T- kernel and take advantae o' its basicservices. &ac$ o' t$ese add*on components is incl"ded in an embedded system only i' its services are needed 'orimplementin t$e embedded application, in order to keep proram memory cons"mption to a minim"m.

    #n t$is paper, e ill 'oc"s on t$e basic T- kernel services 'or task manaement, intertask comm"nication andsync$roni!ation, and dynamic memory allocation.

    RTOSs vs( general-purpose operating systems

    any non*real*time operatin systems also provide similar kernel services. T$e key di''erence beteen eneral*comp"tin operatin systems and real*time operatin systems is t$e need 'or + deterministic + timin be$avior int$e real*time operatin systems. /ormally, +deterministic+ timin means t$at operatin system services cons"meonly knon and epected amo"nts o' time. #n t$eory, t$ese service times co"ld be epressed as mat$ematical'orm"las. T$ese 'orm"las m"st be strictly alebraic and not incl"de any random timin components. andomelements in service times co"ld ca"se random delays in application so'tare and co"ld t$en make t$e applicationrandomly miss real*time deadlines a scenario clearly "nacceptable 'or a real*time embedded system.

    ;eneral*comp"tin non*real*time operatin systems are o'ten 7"ite non*deterministic. T$eir services can in%ectrandom delays into application so'tare and t$"s ca"se slo responsiveness o' an application at "nepected times.#' yo" ask t$e developer o' a non*real*time operatin system 'or t$e alebraic 'orm"la describin t$e timin be$avioro' one o' its services (s"c$ as sendin a messae 'rom task to task), yo" ill invariably not et an alebraic 'orm"la.#nstead t$e developer o' t$e non*real*time operatin system (s"c$ as 6indos,

  • 7/27/2019 Basic Concepts of RTOS

    3/6

    responsiveness. =ery 7"ick responsiveness is made possible by t$e +preemptive+ nat"re o' t$e task sc$ed"lin.+>reemptive+ means t$at t$e sc$ed"ler is alloed to stop any task at any point in its eec"tion, i' it determines t$atanot$er task needs to r"n immediately.

    T$e basic r"le t$at overns priority*based preemptive sc$ed"lin is t$at at every moment in time, +T$e ?i$est>riority Task t$at is eady to "n, ill be t$e Task t$at "st be "nnin.+ #n ot$er ords, i' bot$ a lo*priority taskand a $i$er*priority task are ready to r"n, t$e sc$ed"ler ill allo t$e $i$er*priority task to r"n 'irst. T$e lo*priority task ill only et to r"n a'ter t$e $i$er*priority task $as 'inis$ed it$ its c"rrent ork.

    6$at i' a lo*priority task $as already be"n to r"n, and t$en a $i$er*priority task becomes ready@ T$is mi$tocc"r beca"se o' an eternal orld trier s"c$ as a sitc$ closin. priority*based preemptive sc$ed"ler illbe$ave as 'ollos: #t ill allo t$e lo*priority task to complete t$e c"rrent assembly*lan"ae instr"ction t$at it iseec"tin. A"t it onBt allo it to complete an entire line o' $i$*level lan"ae codeC nor ill it allo it to contin"er"nnin "ntil t$e net clock tick.4 #t ill t$en immediately stop t$e eec"tion o' t$e lo*priority task, and allo t$e$i$er*priority task to r"n. 'ter t$e $i$er*priority task $as 'inis$ed its c"rrent ork, t$e lo*priority task ill bealloed to contin"e r"nnin. T$is is s$on in /i"re 3, $ere t$e $i$er*priority task is called +id*>riority Task.+

    -' co"rse, $ile t$e mid*priority task is r"nnin, an even $i$er*priority task mi$t become ready. T$is isrepresented in /i"re 3 by +Trier2+ ca"sin t$e +?i$*>riority Task+ to become ready. #n t$at case, t$e r"nnintask (+id*>riority Task+) o"ld be preempted to allo t$e $i$*priority task to r"n. 6$en t$e $i$*priority task $as'inis$ed its c"rrent ork, t$e mid*priority task o"ld be alloed to contin"e. nd a'ter bot$ t$e $i$*priority task andt$e mid*priority task complete t$eir ork, t$e lo*priority task o"ld be alloed to contin"e r"nnin. T$is sit"ationmi$t be called +nested preemption.+

    Figure ): Timeline for 'riority-!ased 'reemptive Scheduling $*amples

    &ac$ time t$e priority*based preemptive sc$ed"ler is alerted by an eternal orld trier (s"c$ as a sitc$ closin)or a so'tare trier (s"c$ as a messae arrival), it m"st o t$ro"$ t$e 'olloin E steps:

    Determine $et$er t$e c"rrently r"nnin task s$o"ld contin"e to r"n. #' not F

    Determine $ic$ task s$o"ld r"n net.

    ave t$e environment o' t$e task t$at as stopped (so it can contin"e later).

    et "p t$e r"nnin environment o' t$e task t$at ill r"n net.

    llo t$is task to r"n.

    T$ese E steps toet$er are called +task sitc$in.+

    Fi*ed-time task s#itching

    T$e time it takes to do task sitc$in is o' interest $en eval"atin an operatin system. simple eneral*comp"tin (non*preemptive) operatin system mi$t do task sitc$in only at timer tick times, $ic$ mi$t 'oreample be ten milliseconds apart. T$en i' t$e need 'or a task sitc$ arises any$ere it$in a 10*millisecondtime'rame, t$e act"al task sitc$ o"ld occ"r only at t$e end o' t$e c"rrent 10*millisecond period. "c$ a delayo"ld be "nacceptable in most real*time embedded systems.

    #n more sop$isticated preemptive task sc$ed"lers, t$e sc$ed"ler may need to searc$ t$ro"$ arrays o' tasks todetermine $ic$ task s$o"ld be made to r"n net. #' t$ere are more tasks to searc$ t$ro"$, t$e searc$ ill takeloner. "c$ searc$es are o'ten done by eneral*comp"tin operatin systems, t$"s makin t$em non*deterministic. eal*time operatin systems, on t$e ot$er $and, avoid s"c$ searc$es by "sin incrementally "pdatedtables t$at allo t$e task sc$ed"ler to identi'y t$e task t$at s$o"ld r"n net in a rapid 'ied*time 'as$ion.

    T$ese to types o' timin be$avior 'or task sitc$in can be seen in /i"re G.

  • 7/27/2019 Basic Concepts of RTOS

    4/6

    Figure +: Task S#itching Timing

    #n t$is 'i"re, e see t$at 'or a eneral*comp"tin (non*real*time) operatin system, t$e task sitc$in timeenerally rises as a so'tare system incl"des more tasks t$at can be sc$ed"led. ?oever, t$e act"al time 'or a tasksitc$ is not t$e time s$on by t$e das$ed red line. #nstead, in any iven task sitc$ instance, it mi$t be ellabove or ell belo t$e time s$on by t$e das$ed red line. T$e s$aded reions s"rro"ndin t$e das$ed red linesimply s$o t$e likeli$ood o' t$e act"al task sitc$ time bein t$at 'ar above or belo t$e das$ed red line.

    -n t$e ot$er $and, t$e $ori!ontal solid reen line s$os t$e task sitc$in time c$aracteristic o' a real*timeoperatin system. #t is constant, independent o' any load 'actor s"c$ as t$e n"mber o' tasks in a so'tare system.

    >lease note t$at in some instances, s"c$ as t$e le'tmost area o' t$e rap$, t$e task sitc$in time mi$t in specialcases be 7"icker 'or a eneral*comp"tin non*real*time operatin system, t$an 'or a real*time operatin system.

    T$is does not detract 'rom t$e appropriateness o' a real*time operatin system 'or real*time embedded applications./or, in 'act, t$e term +real*time+ does not mean +as 'ast as possible+ b"t rat$er +real*time+ demands consistent,repeatable, knon timin per'ormance. lt$o"$ a non*real*time operatin system mi$t do some 'aster tasksitc$in 'or small n"mbers o' tasks, it mi$t e7"ally ell introd"ce a lon time delay t$e net time it does t$e sametask sitc$. T$e strent$ o' a real*time operatin system is in its knon, repeatable timin per'ormance, $ic$ isalso typically 'aster t$an t$at o' a non*deterministic task sc$ed"ler in sit"ations o' lare n"mbers o' tasks in aso'tare system. ost o'ten, t$e real*time operatin system ill e$ibit task*sitc$in times m"c$ 'aster t$an itsnon*real*time competitor $en t$e n"mber o' tasks ros above E or 10.

    ,ntertask communication and synchroniation

    ost operatin systems, incl"din T-s, o''er a variety o' mec$anisms 'or comm"nication and sync$roni!ationbeteen tasks. T$ese mec$anisms are necessary in a preemptive environment o' many tasks, beca"se it$o"t

    t$em t$e tasks mi$t ell comm"nicate corr"pted in'ormation or ot$erise inter'ere it$ eac$ ot$er.

    /or instance, a task mi$t be preempted $en it is in t$e middle o' "pdatin a table o' data. #' a second task t$atpreempts it reads 'rom t$at table, it ill read a combination o' some areas o' nely*"pdated data pl"s some areas o'data t$at $ave not yet been "pdated. Ne Horkers o"ld call t$is a +mis$*mas$.+4 T$ese "pdated and old dataareas toet$er may be incorrect in combination, or may not even make sense. n eample is a data tablecontainin temperat"re meas"rements t$at beins it$ t$e contents +10 5.+ task beins "pdatin t$is table it$t$e ne val"e +II /+, ritin into t$e table c$aracter*by*c$aracter. #' t$at task is preempted in t$e middle o' t$e"pdate, a second task t$at preempts it co"ld possibly read a val"e like +I0 5+ or +II 5.+ or +II /+, dependin onprecisely $en t$e preemption took place. T$e partially "pdated val"es are clearly incorrect, and are ca"sed bydelicate timin coincidences t$at are very $ard to deb" or reprod"ce consistently.

    n T-Js mec$anisms 'or comm"nication and sync$roni!ation beteen tasks are provided to avoid t$ese kinds o'

    errors. ost T-s provide several mec$anisms, it$ eac$ mec$anism optimi!ed 'or reliably passin a di''erentkind o' in'ormation 'rom task to task.

    >robably t$e most pop"lar kind o' comm"nication beteen tasks in embedded systems is t$e passin o' data 'romone task to anot$er. ost T-s o''er a messae passin mec$anism 'or doin t$is, as seen in /i"re E. &ac$messae can contain an array or b"''er o' data.

  • 7/27/2019 Basic Concepts of RTOS

    5/6

    Figure .: ,ntertask /essage 0ommunication

    #' messaes can be sent more 7"ickly t$an t$ey can be $andled, t$e T- ill provide messae 7"e"es 'or $oldint$e messaes "ntil t$ey can be processed. T$is is s$on in /i"re.

    not$er kind o' comm"nication beteen tasks in embedded systems is t$e passin o' $at mi$t be called+sync$roni!ation in'ormation+ 'rom one task to anot$er. +ync$roni!ation in'ormation+ is like a command, $eresome commands co"ld be positive, and some neative. /or eample, a neative command to a task o"ld besomet$in like +>lease donBt print ri$t no, beca"se my task is "sin t$e printer.+ -r more enerally, +# ant to lockt$e . . . 'or my on "se only.+ positive command o"ld be somet$in like +#Bve detected a cardiac emerency, and# ant yo" to $elp me $andle it.+ -r more enerally, +>lease %oin me in $andlin . . .+

    ost T-s o''er a semap$ore or m"te mec$anism 'or $andlin neative sync$roni!ation (sometimes called+m"t"al ecl"sion+). T$ese mec$anisms allo tasks to lock certain embedded system reso"rces 'or t$eir "se only,and s"bse7"ently to "nlock t$e reso"rce $en t$eyBre done.

    /or positive sync$roni!ation, di''erent T-s o''er di''erent mec$anisms. ome T-s o''er event*'las, $ileot$ers o''er sinals. nd yet ot$ers rely on messae passin 'or positive sync$roni!ation as ell as data passind"ties.

    eterminism and high-speed message passing

    #ntertask messae comm"nication is anot$er area $ere di''erent operatin systems s$o di''erent timinc$aracteristics. ost operatin systems act"ally copy messaes tice as t$ey trans'er t$em 'rom task to task via amessae 7"e"e. ee /i"re . T$e 'irst copyin is 'rom t$e messae*sender task to an operatin system*oned+secret+ area o' memory (implementin t$e +messae 7"e"e+)C and t$e second copyin is 'rom t$e operatinsystemBs +secret+ area to t$e messae*receiver task. 5learly t$is is non*deterministic in its timin, as t$esecopyin activities take loner as messae lent$ increases.

    Figure 2: /essage Transfer via /essage 3ueue

    n approac$ t$at avoids t$is non*determinism and also accelerates per'ormance, is to $ave t$e operatin systemcopy a pointer to t$e messae and deliver t$at pointer to t$e messae*receiver task it$o"t movin t$e messaecontents at all. #n order to avoid access collisions, t$e operatin system t$en needs to o back to t$e messae*sender task and obliterate its copy o' t$e pointer to t$e messae. /or lare messaes, t$is eliminates t$e need 'orlent$y copyin and eliminates non*determinism.

    ynamic memory allocation

    Determinism o' service times is also an iss"e in t$e area o' dynamic allocation o' memory. any eneral*comp"tin non*real*time operatin systems o''er memory allocation services 'rom $at is termed a +?eap.+ T$e'amo"s +malloc+ and +'ree+ services knon to 5*lan"ae prorammers ork 'rom a $eap. Tasks can temporarilyborro some memory 'rom t$e operatin systemBs $eap by callin +malloc+, and speci'yin t$e si!e o' memoryb"''er needed. 6$en t$is task (or anot$er task) is 'inis$ed it$ t$is memory b"''er it can ret"rn t$e b"''er to t$eoperatin system by callin +'ree.+ T$e operatin system ill t$en ret"rn t$e b"''er to t$e $eap, $ere its memorymi$t be "sed aain, per$aps as part o' a larer b"''er. -r per$aps it may in t$e '"t"re be broken into severalsmaller b"''ers.

    ?eaps s"''er 'rom a p$enomenon called +&ternal emory /ramentation+ t$at may ca"se t$e $eap services toderade. T$is 'ramentation is ca"sed by t$e 'act t$at $en a b"''er is ret"rned to t$e $eap, it may in t$e '"t"re bebroken into smaller b"''ers $en +malloc+ re7"ests 'or smaller b"''er si!es occ"r. 'ter a $eap "nderoes manycycles o' +malloc+s and +'ree+s, small slivers o' memory may appear beteen memory b"''ers t$at are bein "sed

    by tasks. T$ese slivers are so small t$at t$ey are "seless to tasks. A"t t$ey are trapped beteen b"''ers t$at arebein "sed by tasks, so t$ey canBt be coa"lated (+l"ed+) toet$er into bier, "se'"l b"''er si!es. -ver time, a$eap ill $ave more and more o' t$ese slivers. T$is ill event"ally res"lt in sit"ations $ere tasks ill ask 'ormemory b"''ers (+malloc+) o' a certain si!e, and t$ey ill be re'"sed by t$e operatin system *** even t$o"$ t$eoperatin system $as eno"$ available memory in its $eap. T$e problem: T$at memory is scattered in small sliversdistrib"ted in vario"s separate parts o' t$e $eap. #n operatin system terminoloy, t$e slivers are called +'raments+,

  • 7/27/2019 Basic Concepts of RTOS

    6/6

    and t$is problem is called +eternal memory 'ramentation.+

    T$is 'ramentation problem can be solved by so*called +arbae collection+ (de'ramentation) so'tare.ools+ memoryallocation mec$anism allos application so'tare to allocate c$"nks o' memory o' per$aps G or 8 di''erent b"''ersi!es per pool. >ools totally avoid eternal memory 'ramentation, by not permittin a b"''er t$at is ret"rned to t$epool to be broken into smaller b"''ers in t$e '"t"re. #nstead, $en a b"''er is ret"rned t$e pool, it is p"t onto a +'reeb"''er list+ o' b"''ers o' its on si!e t$at are available 'or '"t"re re*"se at t$eir oriinal b"''er si!e. T$is is s$on in/i"re L.

    Figure 4: A /emory 'ool5s Free Buffer "ists

    emory is allocated and de*allocated 'rom a pool it$ deterministic, o'ten constant, timin.

    Summary

    eal*time and embedded systems are "sed in many applications s"c$ as airborne comp"ters, medical instr"mentsand comm"nication systems. &mbedded systems are c$aracteri!ed by limited processor memory, limitedprocessin poer, and "n"s"al inter'aces to t$e o"tside orld. eal*time re7"irements impose strinent timedeadlines 'or deliverin t$e res"lts o' embedded processin.

    T- kernels $ide 'rom application so'tare t$e lo*level details o' system $ardare, and at t$e same time provideseveral cateories o' services to application so'tare. T$ese incl"de: task manaement it$ priority*basedpreemptive sc$ed"lin, reliable intertask comm"nication and sync$roni!ation, non*'ramentin dynamic memoryallocation, and basic timer services.

    T$e iss"e o' timin determinism is important in di''erentiatin eneral*comp"tin operatin systems 'rom real*time

    operatin systems. T$is iss"e crops "p in many parts o' operatin system kernels, s"c$ as task sc$ed"lers,dynamic memory allocation and intertask messae comm"nication. 6$ile eneral*comp"tin operatin systemso'ten o''er non*deterministic services in t$ese areas, '"lly deterministic sol"tions are needed 'or real*time andembedded systems. n"mber o' real*time operatin systems implement t$ese sol"tions in t$eir compact $i$*per'ormance kernels.