You are on page 1of 8

Take A Hike

A Trek Through the Contiki Operating System


Hossam Ashtawy, Troy Brown, Xiaojun Wang, Yuan Zhang
Department o Computer S!ien!e an" #ngineering $i!higan State %ni&ersity #ast 'ansing, $( )**+) ,ashtawy, t-rown./, wang0i)), 1hangy2+34msu5e"u
AbstractThis paper examines the Contiki Operating System and provides in-depth coverage of the core concepts required to understand how it behaves. This understanding can be used to increase an app ication deve oper!s know edge of the under ying system. The paper can a so be used to he p a kerne deve oper port the operating system to another p atform or to deve op additiona device drivers for an existing p atform. "o matter how the end user expects to uti i#e Contiki$ the tutoria provides a base eve of understanding$ which can be everaged to enhance the deve opment experience.

(n a""ition to an app6i!ation an" kerne6 sharing the same memory spa!e, an" there ore a!!ess to ea!h other<s resour!es, an app6i!ation a6so e0ists in the same memory spa!e as another app6i!ation5 There ore, app6i!ations !an a6so !orrupt ea!h other in a""ition to the kerne65 #&en though app6i!ations an" the kerne6 share a memory spa!e an" ha&e a!!ess to ea!h other<s g6o-a6 "ata, this is not a6ways !onsi"ere" a -a" "esign5 As has a6rea"y -een in"i!ate", resour!es are s!ar!e on these systems= howe&er ;A$ is not the on6y resour!e that is s!ar!e5 >orma66y, the pro!essors ?or mi!ro7!ontro66ers@ that are in these systems ha&e just enough pro!essing power to per orm the task at han"5 The pro!essors are norma66y se6e!te" -ase" on a pri!e8per orman!e tra"eo 5 The more that !an -e "one with 6ess pro!essing power, the 6ess the en" pro"u!t wi66 u6timate6y !ost5 The a!t that the app6i!ations an" kerne6 share memory spa!e makes it &ery e i!ient or them to share in ormation through g6o-a6 &aria-6es5 A6though this !auses the operating system an" app6i!ations to -e tight6y integrate", it a66ows or more e i!ient usage o the a&ai6a-6e memory5 This is the approa!h taken -y the "esigners o Contiki= in!rease the !oup6ing to a66ow or more e i!ient usage o system resour!es5 Due to this e i!ient usage o resour!es, typi!a6 !on igurations o the Contiki operating system !an it within +: o ;A$ an" )A: o ;O$5 + ,)STO(The origins o Contiki 6ay within the "ay "reaming o its !reator, A"am Dunke6s9 who works or the Swe"ish (nstitute o Computer S!ien!e+5 During his morning !ommute on the train in +AA+, he ha" the i"ea o an em-e""e" we- -rowser5 Sin!e he ha" pre&ious6y "e&e6ope" an em-e""e" TCB8(B networking sta!k an" we- ser&er, the thought o a we- -rowser was not that ar et!he"5 %sing a Commo"ore /) as a means to "isp6ay the -rowse" we- using a primiti&e te0t -ase" graphi!a6 inter a!e ?i5e5, Contiki Too6kit@ an" an #thernet !ar", a6so "e&e6ope" -y Dunke6s., the we- -rowser was -orn5 This su!!ess u6 "e&e6opment 6e" Dunke6s to urther e0p6oit the seeming6y &ast amounts o remaining resour!es in the ma!hine -y "e&e6oping a""itiona6 app6i!ations5 %6timate6y, this 6e" to the "esire to run mu6tip6e app6i!ations on the CB% at the same time, thus Contiki was -orn as an attempt to u6 i66 httpC88www5si!s5se8Da"am8 + httpC88www5si!s5se8 . httpC88"unke6s5!om8a"am8t e8
9

% O&'(&)'* The Contiki Operating System a66s within the !ategory o #&ent7Dri&en systems5 This means that the app6i!ations e0e!uting on the OS are triggere" -y e&ents5 An app6i!ation !onsists o a pro!ess, whi!h is a6so !onsi"ere" an e&ent han"6er5 Any time an e&ent is "estine" or the pro!ess, the pro!ess is s!he"u6e" an" the e&ent is "e6i&ere" to the pro!ess in the orm o a!ti&ating its e&ent han"6er5 The e&ent han"6er parameters a66ow or the e&ent, a6ong with any "ata, to -e passe" to the han"6er so that it !an -e e0amine" an" a!te" upon5 %n6ike tra"itiona6 persona6 !omputer operating systems, the kerne6 o Contiki is not prote!te" rom app6i!ations5 This means that an app6i!ation a!!ess an"8or !orrupt g6o-a6 "ata, e&en "ata -e6onging to the un"er6ying operating system5 There are a num-er o reasons or this type o !on iguration5 The most prominent reason is the a!t that Contiki is suita-6e or "eep6y em-e""e" systems, where memory management har"ware, su!h as an $$%, is not a&ai6a-6e on the p6at orm5 This is !ommon or mi!ro7!ontro66ers as we66 as 6ow7en" pro!essors5 A urther reason or the kerne6 an" app6i!ations sharing the same memory spa!e is "ue to memory an" pro!essing !onstraints5 A66owing or a more tight6y !oup6e" operating system an" app6i!ation a66ows or more e i!ient use o memory5 (t is not unusua6 or a typi!a6 em-e""e" system to ha&e 6ess than 9: o ;A$ a&ai6a-6e to the system5 (n or"er or an operating system to -e use u6 on these types o systems, it must not !onsume a66 o the resour!es o the p6at orm5 There ore, one o the "ri&ing or!es -ehin" the Contiki operating system is to minimi1e the amount o resour!es !onsume"5

this "esire, a66owing or mu6tip6e app6i!ations to e0e!ute !on!urrent6y within the e&ent7-ase" !omputing para"igm5 . /001)C/T)O"S #&er sin!e its origina6 re6ease Contiki has -een supporte" on mu6tip6e p6at orms ranging rom sensor network no"es to out7"ate" home !omputers rom the 9E*A<s ?su!h as the Commo"ore /), Atari, App6e ((, et!5@ to resour!e !onstraine" em-e""e" systems5 (n a""ition to mu6tip6e p6at orms, Contiki has a6so -een "ep6oye" within a num-er o "i erent in"ustries or -oth !ommer!ia6 an" non7!ommer!ia6 usage5 App6i!ations &ary wi"e6y an" in!6u"e automo-i6es, airp6anes, sate66ites, reighter ships, oi6 "ri66ing eFuipment, !ontainer tra!king an" te6e&ision pro"u!tion eFuipment, to name a ew5 The Contiki "e&e6opment team a6so !onsists o a -roa" range o internationa6 !ompanies who are !ommitte" to a"&an!ing the Contiki operating system an" the p6at orms on whi!h it runs5 Cis!o an" Atme6 are just two o the !ontri-utors to the !ontinue" "e&e6opment o Contiki, re 6e!ting the types o areas in whi!h Contiki has -een "esigne" to operate5 Cis!o is a major networking !ompany an" Contiki has one o the sma66est u66y un!tiona6 network sta!ks in e0isten!e, in!6u"ing an (B&/ network sta!k5 Atme6 is a g6o-a6 !orporation with a signi i!ant ootho6" in the mi!ro7!ontro66er market, a key segment or whi!h Contiki was "esigne" to operate5 One o the main app6i!ations or Contiki is or use on no"es within a sensor network5 The networking aspe!t o these systems as we66 as the sma66 !omputing p6at orms tie "ire!t6y into the pro"u!t "omains o these two !ompanies where 6itt6e e&i"en!e is nee"e" in or"er to see why they ha&e "e!i"e" to -e!ome in&o6&e" in the "e&e6opment o the operating system5 2 '&'"T 3'4)")T)O" Sin!e e&ents !onstitute the heart o the Contiki Operating System they p6ay a &ery 6arge ro6e in the proper un!tioning an" intera!tion -etween the kerne6, app6i!ations an" "ri&ers within the system5 #a!h app6i!ation essentia66y is one pro!ess with a main entry point !orrespon"ing to an e&ent han"6er5 A66 intera!tions are -ase" on either share" g6o-a6 "ata or e&ent !ommuni!ation5 4.1 Event Types #&ents !an -e !6assi ie" in two "istin!t ways5 The irst way is to "etermine the "estination o the e&ent5 #&ents !an ha&e either a sing6e pro!ess re!ipient ?i5e5, sing6e re!ei&er e&ent@ or a66 pro!esses ?i5e5, -roa"!ast e&ent@5 The a!tua6 re!ei&er !6ass asso!iate" with an e&ent is "etermine" "uring run7time when the sen"er, using one o Contiki<s #&ent AB(s, inje!ts the e&ent into the system5 An e&ent is initiate" either -y the kerne6 or -y a pro!ess5 De&i!e "ri&ers e0e!uting on an interrupt !onte0t ?i5e5, !ause" -y an interrupt@ use a "i erent metho" o !ommuni!ation, po66 reFuests, "is!usse" 6ater5 >orma66y, the kerne6 wi66 on6y sen" e&ents to a pro!ess when it is !reate" ?(>(T e&ent@, stoppe" ?#X(T an" #X(T#D e&ents@ or po66e" ?BO'' e&ent@5 Bro!esses on the other han", !an sen" e&ents whene&er they "esire, "epen"ing upon the imp6emente" un!tiona6ity !orrespon"ing to the pro!ess5 (t shou6" -e note" that the kerne6 itse6 !annot re!ei&e an e&ent5

A pro!ess e&ent7han"6er is the on6y p6a!e where e&ents !an -e re!ei&e"5 The se!on" metho" -y whi!h an e&ent is !6assi ie" is "etermine" -ase" on whether the e&ent is a syn!hronous e&ent or an asyn!hronous e&ent, thus "istinguishing the kin" o e&ent -eing sent5 Asyn!hronous e&ents are Fueue" in the kerne6<s e&ent Fueue an" "e6i&ere" when the OS s!he"u6es that e&ent to -e "e6i&ere"5 Sin!e the e&ent Fueue is a G(GO, e&ents wi66 -e s!he"u6e" -ase" on the or"er in whi!h they were a""e" to the Fueue5 There are #&ent AB(s pro&i"e" -y Contiki to sen" an asyn!hronous e&ent5 These types o e&ents !an -e !onsi"ere" a "e erre" pro!e"ure !a66s sin!e the e&ent is not imme"iate6y "e6i&ere"5 When it is "e6i&ere", the re!ei&er pro!ess ?or pro!esses@ wi66 ha&e their e&ent han"6er routine ?i5e5, the main entry point or the pro!ess@ in&oke" with an argument spe!i ying the e&ent -eing sent5 The kerne6, on the other han", han"6es a syn!hronous e&ent imme"iate6y, !ausing the re!ei&ing pro!ess ?or pro!esses@ to -e imme"iate6y s!he"u6e" an" their e&ent han"6er to -e in&oke"5 A ter the syn!hronous e&ent is "e6i&ere", the origina6 pro!ess wi66 resume its e0e!ution5 This type o e&ent is ana6ogous to an inter7pro!ess pro!e"ure !a665 Contiki emp6oys run7to7!omp6etion semanti!s, whi!h is !ommon in e&ent7-ase" operating systems5 This means that no preemption is emp6oye" -y the operating system5 (nstea", the pro!esses are e0pe!te" to Fui!k6y pro!ess re!ei&e" e&ents, register to re!ei&e new e&ents an" ina66y, return rom the e&ent han"6er so that another pro!ess !an -e s!he"u6e"5 This reFuires that app6i!ations !ooperate with ea!h other or the system may !ome to a ha6t where one pro!ess is !onsuming a66 o the pro!essor<s time, pre&enting res!he"u6ing o other pro!esses5 This !ooperati&e en&ironment is not sa e rom a se!urity stan"point, -ut sin!e the system is &ery sma66 an" tight6y !oup6e", it is not e0pe!te" that interna6 se!urity wi66 -e a 6arge !on!ern5 Typi!a66y, app6i!ations o this si1e are sma66 enough that one or two peop6e wi66 imp6ement the entire system an" thus wi66 ha&e u66 !ontro6 o&er how the pro!esses -eha&e an" intera!t with ea!h other5 4.2 Event Structure Gor asyn!hronous e&ents, the kerne6 must keep enough in ormation so that it !an 6ater s!he"u6e a pro!ess to re!ei&e the e&ent5 There ore, or ea!h e&ent in the e&ent Fueue, three pie!es o in ormation are maintaine", as spe!i ie" within the kerne6<s sour!e) e&entH"ata stru!ture, an" i66ustrate" in TAB'# 95 The Ie&J ie6" !ontains the numeri!a6 &a6ue asso!iate" with an e&ent5 #a!h e&ent type, su!h as (>(T, #X(T, #X(T#D, et!5 has a uniFue &a6ue that is use" to "i erentiate -etween e&ent types5 This e&ent num-er wi66 -e "e6i&ere" to the pro!ess spe!i ie" in the IpJ ie6" when the e&ent is s!he"u6e", -y in&oking the pro!ess e&ent han"6er passing the e&ent num-er as a parameter5 (n a""ition, "ata !an -e asso!iate" with the e&ent5 The I"ataJ ie6" !ontains a pointer to the "ata that is to -e "e6i&ere" to the re!ei&ing pro!ess5 >ote that sin!e the kerne6 an" a66 o the app6i!ations share the same memory spa!e, they !an easi6y share "ata with ea!h other simp6y -y passing a
)

See 8!ontiki7+5)8!ore8sys8pro!ess5! in the kerne6 sour!e !o"e a&ai6a-6e at httpC88www5si!s5se8!ontiki8"own6oa"5htm65

pointer rom one p6a!e to another5 This is &ery e i!ient, as no "ata has to -e !opie" rom one pro!ess a""ress spa!e to another5 The on6y "istin!tion -etween a sing6e re!ei&er an" a -roa"!ast e&ent is what is spe!i ie" in the IpJ ie6"5 Gor a sing6e re!ei&er, this wi66 -e a pointer to the pro!ess stru!ture o the re!ei&ing pro!ess5 Gor a -roa"!ast e&ent, this wi66 !ontain the spe!ia6 !onstantC B;OC#SSHB;OADCAST ?whi!h is "e ine" as >%''@5
TAB'# 95 4ie d e& "ata p CO>T(:( #K#>T L%#%# ST;%CT%;# 3escription #&ent >um-er Bointer to "ata asso!iate" with e&ent5 Bointer to pro!ess stru!ture o re!ei&er5

sen" an e&ent to a pro!ess5 This wi66 a6so -e "is!usse" in se!tion /5


TAB'# +5 4ie d ne0t name threa" pt state nee"spo66 CO>T(:( B;OC#SS ST;%CT%;#

3escription Bointer to ne0t pro!ess in pro!ess 6ist5 >ame o the pro!ess5 Bointer to protothrea" e&ent han"6er5 Brotothrea" !orrespon"ing to pro!ess5 Bro!ess state ?>one, ;unning, Ca66e"@5 G6ag reFuesting that pro!ess !he!k or "ata5

5.2

5 0(OC'SS 3'4)")T)O" The kerne6 pro!ess mo"u6e ?i5e5, oun" in pro!ess5!@ is the heart o the OS5 (t !ontains a num-er o AB(s or manipu6ating pro!esses, e&ents an" s!he"u6ing5 There are two main "ata o-je!ts that e0ist here, the Bro!ess 'ist an" the G(GO Asyn!hronous #&ent Lueue5 The Bro!ess 'ist is imp6emente" as a sing6y 6inke" 6ist o pro!ess stru!tures5 This 6ist on6y !ontains pro!esses stru!tures or pro!esses that ha&e -een starte" in the system5 These !ou6" either -e pro!esses that were starte" when the OS -ootstrappe" or were "ynami!a66y starte" "uring the run7time o the system5 The 6ength o the Bro!ess 'ist !an grow an" shrink at run7time as new pro!esses are starte" an"8or o6"er pro!esses are stoppe"5 The G(GO Asyn!hronous #&ent Lueue is imp6emente" as a ring -u er whose 6ength is i0e" at !ompi6e time, through a !on igura-6e !onstant5 The asyn!hronous e&ents in this Fueue !an -e either o the sing6e re!ei&er or -roa"!ast type5 >ote that syn!hronous e&ents are ne&er put into this Fueue as they are "ispat!he" imme"iate6y5 The Fueue ho6"s e&ents that ha&e not yet -een "ispat!he"5 They wi66 -e s!he"u6e" at a 6ater time when the s!he"u6er is in&oke"5 5.1 Process Structure Gor a66 a!ti&e pro!esses in the system, Contiki must keep tra!k o them so that they !an -e s!he"u6e" when e&ents arri&e, restore !onte0t when they are -6o!ke" an" know what state they are in at any time5 Gor ea!h pro!ess in the pro!ess 6ist, si0 pie!es o in ormation are kept, as spe!i ie" within the kerne6<s pro!ess stru!ture, an" i66ustrate" in TAB'# +5 The Ine0tJ ie6" !ontains a pointer to the ne0t pro!ess stru!ture in the pro!ess 6ist, or >%'' i it is the 6ast stru!ture5 The InameJ ie6" !ontains a pointer to the name asso!iate" with the pro!ess5 The Ithrea"J ie6" !ontains a pointer to the e&ent7han"6er that is asso!iate" with the pro!ess5 #a!h pro!ess has an e&ent7han"6er asso!iate" with it, as e&ents are the main way that pro!esses !ommuni!ate with ea!h other5 The IptJ ie6" !ontains the protothrea" state5 Brotothrea"s are urther "is!usse" in se!tion M5+5 The IstateJ ie6" !ontains the !urrent state o the pro!ess5 This is "epen"ent upon whether the pro!ess has -een starte", stoppe" or is !urrent6y s!he"u6e" an" is urther "etai6e" in se!tion /5 Gina66y, the Inee"spo66J 6ag is use" to signa6 a pro!ess that nee"s to !he!k or "ata5 This is usua66y asso!iate" with "e&i!e "ri&ers an" is an in"ire!t way to

Protothreads A protothrea" is a sta!k6ess threa"5 This makes it great or use in sma66 ootprint systems, as memory is not waste" on mu6tip6e sta!ks that are usua66y on6y partia66y use"5 This means that protothrea"s are a6so we66 suite" or the types o app6i!ations that Contiki is targeting5 The main -ene it that protothrea"s pro&i"e is !on"itiona6 -6o!king insi"e a pro!ess e&ent7han"6er5 Tra"itiona66y, e&ent7han"6ers !annot -6o!k as they imp6ement run7to7!omp6etion semanti!s5 ( an e&ent7 han"6er "i" not return, it wou6" pre&ent the s!he"u6ing o other pro!esses5 Howe&er, protothrea"s a66ow -6o!king operations within an e&ent han"6er5 Through the use o spe!ia6 protothrea" -6o!king operations, some state ma!hines that are norma66y oun" within an e&ent7 -ase" app6i!ation !an -e e6iminate"5 The types o state ma!hines that !an -e e6iminate" through the use o protothrea"s are those that e0ist stri!t6y to a66ow the app6i!ation to work in the presen!e o a non7-6o!king e&ent han"6er5 This is i66ustrate" Fuite we66 in N9O whi!h pro&i"es a si"e7-y7si"e !omparison o a tra"itiona6 e&ent7-ase" app6i!ation an" a6so one imp6emente" using protothrea"s5 As !an -e seen in the si"e7-y7si"e !omparison, the a!tua6 si1e is signi i!ant6y "e!rease" through the use o protothrea"s5 The ramework asso!iate" with maintaining state through mu6tip6e in&o!ations o the e&ent han"6er has -een remo&e"5 The protothrea" e0amp6e shows that norma6 seFuentia6 -6o!king operations !an -e imp6emente" in a mu!h simp6er manner5 The resu6t is 6ess app6i!ation !o"e, !6earer imp6ementations, -6o!king operations an" e&ent in inite 6oops5 5.3 Protothread Implementation A protothrea" !onsists o two main e6ements, an e&ent7 han"6er an" a 6o!a6 !ontinuation5 The e&ent han"6er is the one asso!iate" with the pro!ess5 A 6o!ation !ontinuation is use" to sa&e an" restore the !onte0t when a -6o!king protothrea" AB( is in&oke"5 (t is a snapshot o the !urrent state o the pro!ess5 The "i eren!e -etween a 6o!a6 !ontinuation an" a more genera6 !ontinuation is that with a 6o!a6 !ontinuation, the !a66 history an" &a6ues o 6o!a6 &aria-6es are not preser&e"5 (n Contiki, protothrea" AB(s are imp6emente" as ma!ros5 These ma!ros, when e0pan"e", a!tua66y e0pose the imp6ementation o the protothrea", espe!ia66y the inner workings o the 6o!a6 !ontinuation5 ;e eren!e N9O pro&i"es an e0amp6e o an app6i!ation -oth with the protothrea" ma!ros, as wou6" -e seen in a norma6 app6i!ation, as we66 as the e0pan"e" &ersion o those ma!ros5 This "emonstrates that in one parti!u6ar imp6ementation o a 6o!a6 !ontinuation, a swit!h statement in !om-ination with the asso!iate" !ases, a!tua66y

sa&e an" restores the !onte0t o the threa"5 On!e this e0pansion is e0amine", it -e!omes !6ear that the e0p6i!it state in a tra"itiona6 state ma!hine has now -e!ome an imp6i!it state in the protothrea"5 When the ma!ro is e0pan"e", the num-ers asso!iate" with the !ase statements a!tua66y re er to the 6ine num-er in the origina6 sour!e where the -6o!king protothrea" !a66 e0iste", guaranteeing uniFue &a6ues5 As !an -e seen rom this e0pansion, -6o!king as imp6emente" -y a protothrea" is a66 an i66usion5 Hi""en IreturnJ statements !om-ine" with a swit!h statement is use" to pro&i"e an i66usion o -6o!king at the app6i!ation 6e&e65 Howe&er, un"er the hoo" these me!hanisms are use" to return rom the e&ent7han"6er an" a6so restore the !onte0t upon reentry5 'o!a6 !ontinuations !an -e imp6emente" in many "i erent ways, su!h as through the use o spe!ia6 har"ware, spe!ia6 !ompi6er support or through the use o a swit!h statement as "etai6e" in N+O5 The -ene it o using a swit!h statement is that no spe!ia6 har"ware or !ompi6er is reFuire"5 (t !an -e imp6emente" using an A>S( C !ompi6er5 There are a num-er o 6imitations asso!iate" with 6o!a6 !ontinuations5 One o the major 6imitations is that 6o!a6 &aria-6e state is not preser&e" a!ross a -6o!king protothrea" AB(5 'ooking at the imp6ementation, it is !6ear why this is not guarantee"5 The e0e!ution a!tua66y returns out o the e&ent han"6er, so the &a6ue o a 6o!a6 &aria-6e on the sta!k is not sa&e"5 Another 6imitation is that sin!e swit!h statements are uti6i1e" un"er the !o&ers, the app6i!ation !annot uti6i1e swit!h statements within the same routine as the protothrea" AB(s5 ( a swit!h statement were to -e use" in the same e&ent han"6er routine, the !ase statements !ou6" !6ash !ausing unwante" -eha&ior5 A""itiona66y, sin!e the protothrea" -6o!king AB(s e0pan" into in"i&i"ua6 !ases o the swit!h statement, an app6i!ation !annot -6o!k in a "i erent routine than where the main BTHB#P(>8BTH#>D swit!h statement resi"es5 This is ne!essary in or"er to preser&e the !ases within the swit!h statement5 6 0(OC'SS CO"T(O1 Gigure 9 i"enti ies the major users o the pro!ess !ontro6 AB(s5 The igure on6y i"enti ies the most !ommon AB(s that are use" or pro!ess an" e&ent han"6ing as we66 as s!he"u6ing5 The Operating System ?i5e5, Contiki@ is use" to start pro!esses "uring the -oot seFuen!e5 Bro!esses re6ate" to (8O an"8or kerne6 ser&i!es as we66 as genera6 app6i!ation pro!esses !an -e i"enti ie" to the kerne6 as nee"e" to -e automati!a66y starte" when the operating system is -oote"5 These types o ser&i!es !ou6" -e su!h things as networking sta!ks an" e&ent timers5 The a!tua6 ser&i!es to start wi66 -e spe!i i! to the system -eing "esigne"5 ( a ser&i!e is not nee"e", it is usua66y not in!6u"e" in or"er to !onser&e resour!es5 The kerne6 is a6so responsi-6e or running pro!esses as part o the s!he"u6er5 (nterrupt Han"6ers are typi!a66y asso!iate" with "e&i!e "ri&ers to ser&i!e (8O reFuests o the supporting har"ware5 These norma66y resu6t in the reFuest to ha&e a pro!ess po665 The a!tua6 po66ing a!ti&ity wi66 a6so -e "epen"ent on the type o "e&i!e5 One e0amp6e may-e asso!iate" with pro!essing "ata -ytes rom a seria6 port5 Bo66ing wi66 -e urther "is!usse" in se!tion /5+5 Bro!esses !an start an" stop other pro!esses ?as we66 as stopping themse6&es@ an" a6so post e&ents5 Sin!e e&ents are a

norma6 part o inter7pro!ess !ommuni!ation on Contiki, these AB(s are use" reFuent6y in genera6 app6i!ations5

4igure %. Common 0rocess Contro s 6.1 Process State Gigure + i66ustrates the "i erent states that a pro!ess !an -e in at any point an" time5 This is maintaine" in the IstateJ ie6" within the pro!ess stru!ture5 As soon as the pro!ess is starte", it is a""e" to the kerne6<s pro!ess 6ist, transitions to the ;%>>(>P state an" the syn!hronous (>(T e&ent is sent to it, a66owing any type o initia6i1ation that may -e reFuire", to o!!ur5 When a pro!ess is s!he"u6e", it wi66 transition to the CA''#D state whi6e the asso!iate" e&ent7han"6er is e0e!uting5 When a pro!ess is stoppe", syn!hronous e&ents wi66 -e sent to a66 pro!esses5 #X(T#D e&ents are poste" to a66 pro!esses e0!ept the e0iting pro!ess, to a66ow or re!6amation o resour!es asso!iate" with the e0iting pro!ess5 An #X(T e&ent wi66 -e poste" to the e0iting pro!ess to a66ow it to !6ean up a ter itse6 5

4igure +. 0rocess State Transitions 6.2 Process Polling Bo66ing is a way to signa6 a pro!ess to "o something5 (t is norma66y asso!iate" with interrupt han"6ers5 Contiki is "esigne" without interrupt 6o!king, so that i "esire", it may -e run atop a rea67time e0e!uti&e5 Due to this a!t, it is possi-6e that attempting to sen" an e&ent rom the interrupt !onte0t wou6" !ause ra!e !on"itions with the oregroun" kerne6

s!he"u6er5 (n or"er to si"estep these ra!e !on"itions, a po66ing 6ag is set in the pro!ess stru!ture o an asso!iate" (8O pro!ess ?using the I;eFuest Bro!ess -e Bo66e"J AB(@ "uring interrupt !onte0t5 When the oregroun" kerne6 s!he"u6er runs, it wi66 noti!e this 6ag is set an" trigger a BO'' e&ent -e poste" to the asso!iate" pro!ess5 That pro!ess wi66 then per orm whate&er "ata operations might -e asso!iate" with a Ipo66J operation5 This means that there are norma66y two portions to an (8O "ri&er= the portion asso!iate" with the interrupt that !auses the po66ing 6ag to -e set an" the portion asso!iate" with the pro!ess that per orms some kin" o (8O spe!i i! operation on the "ata5 7 SC,'381)"9 The Contiki OS emp6oys a 6ightweight s!he"u6er that "ispat!hes pro!esses when there are e&ents poste" against them or when the po66ing 6ag is set5 On!e a pro!ess is s!he"u6e" an" the e&ent han"6er starts e0e!uting, the kerne6 "oes not preempt it unti6 !omp6etion, as "es!ri-e" in N.O5 When the system -oots, as we66 as when a pro!ess -6o!ks or an e&ent, the s!he"u6er se6e!ts a new pro!ess to run5 Grom Contiki<s s!he"u6er perspe!ti&e, the highest priority is gi&en to pro!esses that nee"s to po665 The s!he"u6er knows when there e0ists one or more po66e" pro!esses -y !he!king a g6o-a6 system &aria-6e Ipo66HreFueste"J5 See Gigure . or a 6ow !hart "emonstrating this se6e!tion5 ( po66s are reFueste", then as the 6ow!hart in Gigure ) shows, the s!he"u6er wa6ks through the system<s pro!ess 6ist an" "ispat!hes a66 the pro!esses that ha&e their po66ing 6ags set5 When no po66ing pro!esses remain, the s!he"u6er ser&i!es an e&ent rom the e&ent Fueue an" in turn s!he"u6es the !orrespon"ing pro!ess5 The e&ent !an -e "estine" to on6y one pro!ess or -roa"!ast to a66 the running pro!esses in the system5 (n the !ase o a sing6e pro!ess e&ent, the !orrespon"ing pro!ess is "ispat!he" an" the s!he"u6er !he!ks or po66e" pro!esses again5 ( it is a -roa"!ast e&ent, then a ter ea!h e&ent has -een pro!esse", the s!he"u6er "ispat!hes a66 po66e" pro!esses i there are any 6agge"5

4igure 2. Schedu ing 0o ing 0rocesses Gor preempti&e mu6tithrea"ing, the Contiki OS pro&i"es a 6i-rary that is -ui6t atop the kerne6 an" pro&i"es an AB( to app6i!ations to run preempti&e6y in a mu6tithrea"e" mo"e5 These routines are !a66e" e0p6i!it6y -y app6i!ations to run, yie6" an" s!he"u6e threa"s5 : ';/<01' /001)C/T)O" A typi!a6 Contiki app6i!ation !onsists o one or more pro!esses "e ine" in a C mo"u6e5 The ske6eton o this mo"u6e is as o66owsC 95 Bro!ess De!6aration Q e&ery pro!ess in the app6i!ation shou6" -e "e ine" &ia the IB;OC#SSJ ma!ro -e ore using it5 Autostart 'ist Q 'ist o pro!esses that nee"e" to -e automati!a66y starte" when the mo"u6e is -oote"5 This 6ist is manage" -y the IA%TOSTA;THB;OC#SS#SJ ma!ro5 #&ent Han"6er Q #a!h pro!ess in the mo"u6e has a -o"y, whi!h un!tions as an e&ent han"6er5 The -o"y, whi!h starts with the B;OC#SSHTH;#AD ma!ro, !ontainsC a5 (nitia6i1ation Q ;esour!es are a66o!ate" an" &aria-6es initia6i1e"5 (n inite 'oop Q Waiting an" pro!essing o e&ents takes p6a!e5 ;esour!e Dea66o!ation Q ;e6ease resour!es at the en" o the pro!ess threa" ?-o"y@5

+5

.5

4igure .. 0rocess Schedu ing

-5 !5

'isting 9 "emonstrates a simp6e He66o Wor6" app6i!ation that !ontains a sing6e pro!ess, waits or a timer e&ent an" when the time e0pires, the IHe66o Wor6"J message is "isp6aye"5
#include "contiki.h" PROCESS(example_process, "example process"); AUTOSTART_PROCESSES(&example_process); PROCESS_THREAD(example_process, ev, data) { static str ct etimer timer; PROCESS_!E"#$(); etimer_set%&timer, C&OC'_CO$(_SECO$D); while(*) { PROPROCESS_+A#T_E,E$T%ev -- PROCESS_E,E$T_T#.ER); printf("Hello, world\n"); } PROCESS_E$D(); }

"e&i!es, it 6a!ks any we667"e ine" an" stru!ture" "ri&er ar!hite!ture, urther "etai6e" in N)O5 The "e&i!e "ri&ers< inter a!e &aries rom "e&i!e to "e&i!e an" rom one p6at orm to another5 %sua66y when a sensor or "e&i!e !hanges, a p6at orm spe!i i! 6ow76e&e6 !o"e han"6es interrupt iring, an" then a p6at orm in"epen"ent pro!ess, S#>SO;S whi!h starts at -oot time, is po66e" -y the interrupt ser&i!e routine to -roa"!ast e&ents ?o S#>SO;SH#K#>T type@ to a66 the running pro!esses in the system5 The poste" e&ents !arry in ormation a-out whi!h "e&i!e or sensor has -een !hange"5 An" ina66y, one or more pro!esses re!ei&ing these asyn!hronous e&ents may pro!ess the "ata generate" -y the sour!e sensor8"e&i!e5 %% <'<O(- </"/9'<'"T %+ 4)1' S-ST'< %. "'T*O(B)"9 13.1 uIP and l IP Stac!s 13.1.1 "vervie TCB8(B proto!o6s are wi"e6y use" in the networking o an operating system5 Howe&er, these proto!o6s !annot -e use" "ire!t6y in em-e""e" systems, as they !onsume too many resour!es= they reFuire a 6arge !o"e si1e to imp6ement an" !onsume a 6arge amount o memory5 (n NMO, A"am Dunke6s gi&es two possi-6e so6utionsC u(B ?mi!ro (B@ or 6w(B ?6ightweight (B@5 Though u(B an" 6ightweight (B are -oth simp6i ie" sta!ks imp6emente" or em-e""e" systems, their goa6s are s6ight6y "i erent5 6w(B has many simi6ar un!tions as TCB8(B, su!h as (B, (C$B, %DB an" TCB, an" it !an -e e0ten"e" to support a""itiona6 un!tiona6ity, whi6e u(B on6y supports the minimum set o eatures to keep the network running5 There ore, the resour!es they nee" are "i erent5 A!!or"ing to N/O, u(B takes one ourth o the ;A$ an" ;O$, !ompare" to 6w(B<s .A ki6o-ytes5 Both o them !an e&en run in 6imite" *7-it systems5 As !an -e seen, u(B an" 6w(B are simp6i ie" TCB8(B sta!ks or generi! purpose5 Some me!hanisms, whi!h are rare6y use" or 6ess important, are remo&e"5 >e0t we wi66 "is!uss the me!hanism "etai6s o these two sta!ks5 13.1.2 Implementation The u(B an" 6w(B sta!k are imp6emente" as a main !ontro6 6oop5 As Gigure M shows, the main !ontro6 6oop "oes two thingsC 95 +5 Che!ks or pa!kets that arri&e" rom the network5 Che!ks i a perio"i! timeout has o!!urre"5 (n the irst stage, i new pa!kets arri&e, the hea"ers o the pa!kets are parse" an" sent to spe!i i! app6i!ations in the operating system5 (n the se!on" stage, the network threa" !he!ks i there is a new pa!ket that nee"s to -e sent out to its peers5

1isting %. ,e o *or d 'xamp e = >OOT S'?8'"C' The Contiki -oot seFuen!e &aries rom one p6at orm to another, -ut the genera6 pro!ess is simi6ar5 Har"ware initia6i1ation, ser&i!es, "ri&er an" kerne6 pro!esses are starte" at -oot7up5 Then the system spins in an in inite 6oop where the s!he"u6e is in&oke" to start s!he"u6ing pro!esses5 'isting + pro&i"es an e0amp6e o a minima6 -oot seFuen!e5
//'er0el/Driver Processes to 1e started at 1oot time PROCINIT(&etimer_process,&tcpip_process, &serial_line_process ! int main("oid # process_init( !//i0itiali3e 'er0el process mod le procinit_init( !//Start 'er0el/Driver Processes // Start i0itial Applicatio0 Processes autostart_start(autostart_processes ! $hile(% # process_run( ! && Sc4ed le process%es) to r 0 ' ' // 'er0el .ai0 e0tr2 poi0t

1isting +. <inima >oot Sequence %@ )AO S8>S-ST'< The Contiki kerne6 "oes not pro&i"e a har"ware a-stra!tion 6ayer ?HA'@, instea" it 6ets app6i!ations an" "ri&ers !ommuni!ate "ire!t6y with the har"ware as "es!ri-e" in N.O5 #&en though Contiki supports a wi"e &ariety o sensors an"

The a"&antage o this me!hanism is that it "oesn<t use interrupts an" task s!he"u6ers5 These me!hanisms are &ery

popu6ar in other TCB8(B sta!ks= howe&er to support the 6imite" systems that Contiki targets, they are not use"5

o simu6taneous segments !annot -e urther 6imite", thus the !ongestion !ontro6 me!hanisms are not nee"e"5 6w(B has the a-i6ity to ha&e mu6tip6e in7 6ight segments an" there ore imp6ements a66 o TCB<s !ongestion !ontro6 me!hanisms5 13.1.6 Per(ormance The per orman!e o&erhea" is !a6!u6ate" using two major a!torsC 95 +5 Copying "ata rom network to host memory5 Che!ksum !a6!u6ation5 A sma66, em-e""e" "e&i!e "oes not ha&e the ne!essary pro!essing power to ha&e mu6tip6e prote!tion "omains an" the power to run a mu6titasking operating system5 There ore there is no nee" to !opy "ata -etween the sta!k an" the app6i!ation program5

There are other "etai6s we ha&en<t ta6ke" a-out5 Gor instan!e, the u(B&/, whi!h is the irst (B&/ sta!k or memory7 !onstraine" "e&i!es that passes a66 Bhase79 (B&/ rea"y !erti i!ation tests, is i66ustrate" in N*O5 $ore TCB8(B eatures imp6emente" -y u(B an" 6w(B !an -e oun" in NMO5 4igure 5. <ain Contro 1oop 13.1.3 #$% %ompliance ;GC 99++ is a "o!ument that is one o a pair that "e ines an" "is!usses the reFuirements or a host system5 The reFuirements !an -e "i&i"e" into two !ategories= those that "ea6 with the host7to7host !ommuni!ation an" those that "ea6 with !ommuni!ation -etween the app6i!ation an" the networking sta!k5 (n the sta!k, a66 ;GC reFuirements that a e!t host7to7host !ommuni!ation ha&e -een imp6emente"5 Howe&er, in or"er to re"u!e !o"e si1e, in Contiki, !ertain me!hanisms in the inter a!e -etween the app6i!ation an" the sta!k ha&e -een remo&e"5 13.1.4 &emory and 'u((er &anagement Due to the "i erent "esign goa6s o u(B an" 6w(B, they ha&e "i erent memory management so6utions5 6w(B uses "ynami! -u er a66o!ation me!hanisms5 The "ynami! memory is a66o!ate" rom a g6o-a6 poo6 o a&ai6a-6e memory -6o!ks5 The u(B sta!k "oes not use e0p6i!it "ynami! memory a66o!ation5 (nstea", it uses a sing6e g6o-a6 -u er or ho6"ing pa!kets an" has a i0e" ta-6e or ho6"ing !onne!tion state5 13.1.5 #etransmission) $lo %ontrol and %ongestion %ontrol 6w(B maintains two output Fueues or retransmissionC one ho6"s segments that ha&e not yet -een sent, the other ho6"s segments that ha&e -een sent -ut ha&e not yet -een a!know6e"ge"5 Howe&er, u(B "oes not keep tra!k o pa!ket !ontents a ter the "e&i!e "ri&er has sent them5 The purpose o TCB<s 6ow !ontro6 me!hanisms is to a66ow !ommuni!ation -etween hosts with wi6"6y &arying memory "imensions5 (n the imp6ementation, the app6i!ation !annot sen" more "ata than the re!ei&ing host !an -u er5 The !ongestion !ontro6 me!hanisms 6imit the num-er o simu6taneous TCB segments in the network5 Sin!e u(B on6y han"6es one in7 6ight TCB segment per !onne!tion, the amount 4igure 6. (ime Stack 13.3 *istri+uted T%P %aching Tra"itiona6 TCB is known to e0hi-it poor per orman!e in wire6ess en&ironments5 ;e eren!e NEO o!uses on the per orman!e o TCB in the !onte0t o wire6ess sensor networks an" intro"u!es the Distri-ute" TCB Ca!hing ?DTC@ me!hanism, whi!h in!reases TCB per orman!e5 DTC o&er!omes the pro-6em -y !a!hing TCB segments insi"e the sensor networks an" through the use o 6o!a6 retransmission o TCB segments5 Gurther more, DTC shi ts the -ur"en o the 6oa" rom &u6nera-6e no"es !6ose to the -ase station into the sensor network5 Other te!hniFues that ena-6e the use o TCB8(B or wire6ess sensor networks are a6so "e&e6ope"C spatia6 (B a""ress assignment, share" !onte0t hea"er !ompression an" app6i!ation o&er6ay routing as "es!ri-e" in N9AO5 An a"&antage o using 13.2 #ime Stac! The ;ime sta!k is a 6ayere" !ommuni!ation sta!k that pro&i"es a hierar!ha6 set o wire6ess network proto!o6s5 The goa6 o "esigning ;ime is to simp6i y the imp6ementation o !ommuni!ation proto!o6s5 The 6ayere" stru!ture as "etai6e" in N2O, is shown in Gigure /5 The (nternet ar!hite!ture, in the 6ayers o ;ime, is usua66y thin5 #a!h 6ayer a""s its own hea"er to outgoing messages5 The ;ime sta!k takes a 6itt6e more resour!es -ut signi i!ant6y re"u!es the !omp6e0ity o sensor network proto!o6 imp6ementations5

TCB8(B is to "ire!t6y !ommuni!ate with an in rastru!ture !onsisting either o a wire" (B network or o (B7-ase" wire6ess te!hno6ogy5 %2 S8<</(We ha&e attempte" to pro&i"e a -roa" o&er&iew o the Contiki operating system an" as we66 as supp6y a "etai6e" e0amination o some o the most important aspe!ts o the system5 Howe&er, there are many a""itiona6 eatures that !ou6" not -e "etai6e" in this paper5 A""itiona6 topi!s su!h as "ynami! 6oa"ing an" un6oa"ing o so tware, mu6ti7threa"ing within a protothrea" as we66 as the Contiki Too6kit P%( are some a""itiona6 areas that !ou6" -e e0p6ore" in a uture trek5 The a""itiona6 topi!s are important aspe!ts o the operating system= howe&er they are not the main o!us as they may on6y see 6imite" use "ue to their ni!he Fua6ities5 The topi!s se6e!te" or this paper were e0pe!te" to see more mainstream use an" were se6e!te" -ase" on that !riterion5 Contiki has pioneere" a num-er o inno&ations in operating system "esign5 (t was the irst operating system to imp6ement protothrea"s, (B7-ase" sensor networks, "ynami! 6oa"ing as we66 as preempti&e threa"s atop o e&ent7-ase" systems5 As a urther re 6e!tion upon the importan!e o these !on!epts, the statement that I(mitation is the sin!erest orm o 6atteryJ !an -e app6ie" to Contiki, as many o the operating systems that !ompete in the same arena, ha&e in!orporate" these inno&ations into their own systems5

%5 ('4'('"C'S
N9O A5 Dunke6s, O5 S!hmi"t an" T5 Koigt, I%sing Brotothrea"s or Sensor >o"e Brogramming,J Bro!ee"ings o the ;#A'WS><AM Workshop on ;ea67Wor6" Wire6ess Sensor >etworks5 Sto!kho6m, Swe"en, Rune +AAM5 N+O A5 Dunke6s, O5 S!hmi"t, T5 Koigt an" $5 A6i, IBrotothrea"sC Simp6i ying #&ent7Dri&en Brogramming o $emory7Constraine" #m-e""e" SystemsJ, Bro!ee"ings o the Gourth AC$ Con eren!e on #m-e""e" >etworke" Sensor Systems5 Bou6"er, Co6ora"o, %SA, >o&em-er +AA/5 N.O A5 Dunke6s, B5 Pron&a66 an" T5 Koigt, IContiki Q A 'ightweight an" G6e0i-6e Operating System or Tiny >etworke" SensorsJ, Swe"ish (nstitute o Computer S!ien!e, +AA)5 N)O $5 Ok6o-"1ija, $5 >iko6i!, K5 :o&a!e&i, ISeria6 Bort De&i!e or Contiki Operating SystemJ, Te6e!ommuni!ations orum T#'GO; +AAE5 Ser-ia, Be6gra"e, >o&em-er +AAE5 NMO A5 Dunke6s, IGu66 TCB8(B or *7Bit Ar!hite!turesJ, AC$8%seni0 $o-iSys, +AA.5 N/O A5 Dunke6s, I;ime Q A 'ightweight 'ayere" Communi!ation Sta!k or Sensor >etworksJ, #WS>, +AA25 N2O A5 Dunke6s an" R5 Kasseur, I(B or Smart O-je!tsJ, (BSO A66ian!e White Baper S9, Septem-er +AA*5 N*O $5 Dur&y, R5 A-ei66e, B5 Wetterwa6", C5 O<G6ynn, B5 'e&erett, #5 Pnoske, $5 Ki"a6es, P5 $u66igan, >5 Tsi tes, >5 Ginne an" A5 Dunke6s, I$aking Sensor >etworks (B&/ ;ea"yJ, AC$ SenSys, +AA*5 NEO A5 Dunke6s, T5 Koigt, R5 A6onso an" H5 ;itter, IDistri-ute" TCB Ca!hing or Wire6ess Sensor >etworksJ, $e"Ho!>et, +AA)5 N9AO A5 Dunke6s, T5 Koigt an" R5 A6onso, I$aking TCB8(B Kia-6e or Wire6ess Sensor >etworksJ, #WS>, +AA)5

You might also like