Dining Philosopher Principle
-
Upload
bandungtousa -
Category
Documents
-
view
229 -
download
0
Transcript of Dining Philosopher Principle
-
8/12/2019 Dining Philosopher Principle
1/18
P state machine framework example
Application NoteDining PhilosophersProblem (DPP) Example
Document Revision DAugust 2012
Copyright Quantum Leaps, LLC
www.quantum-leaps.com
www.state-machine.com
http://www.quantum-leaps.com/http://www.state-machine.com/http://www.quantum-leaps.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
2/18
i
Table o !ontents
1 "ntro#uction $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$ $$$$ 12 Re%uirements $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 1
& Design an# "mplementation $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ 23. !tep " !equence #iagrams ...................................................................................................................... $3.$ !tep $" !ignals, %&ents, an' (cti&e )*+ects ......................................................................................... ..... 33.3 !tep 3" !tate achines .................................................................................................................... .......... 3. !tep " /nitiali0ing an' !tarting the (pplication .................................................................... .......... ............ $3. !tep " 1racefully 2erminating the (pplication .................................................................... .......... ............
' Reerences $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$ 1
!ontact "normation $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$ 1
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
-
8/12/2019 Dining Philosopher Principle
3/18
1 of 1
1 "ntro#uction2his (pplication 4ote 'escri*es the classic #ining Philosophers Pro*lem 5#PP6 as an example
application for the QP state machine framework. #PP was pose' an' sol&e' *y %'sger #i+kstra *ack in78 9#i+kstra 8:. 2he #PP application is relati&ely simple an' can *e teste' only with a couple of L%#son your target *oar'. !till, #PP contains six concurrent acti&e o*+ects that exchange e&ents &ia pu*lish-su*scri*e an' 'irect e&ent posting mechanisms. 2he application uses fi&e time e&ents 5timers6, as well as'ynamic an' static e&ents. 2his (pplication 4ote 'escri*es step-*y-step how to 'esign an' implemente'of #PP with QP.
N*TE+2his (pplication 4ote assumes the QP;C framework an' uses example co'e in C to explainimplementation 'etails. irst, your always nee' to un'erstan' what your application is suppose' to accomplish. /n the case of asimple application, the requirements are con&eye' through the pro*lem specification, which for the #PPis as follows.
>i&e philosophers are gathere' aroun' a ta*le with a *ig plate of spaghetti in the mi''le 5see >igure 6.?etween each philosopher is a fork. 2he spaghetti is so slippery that a philosopher nee's two forks to eatit. 2he life of a philosopher consists of alternate perio's of thinking an' eating. @hen a philosopher wantsto eat, he tries to acquire forks. /f successful in acquiring two forks, he eats for a while, then puts 'ownthe forks an' continues to think. 2he key issue is that a finite set of tasks 5philosophers6 is sharing a finiteset of resources 5forks6, an' each resource can *e use' *y only one task at a time. 5(n alternati&eoriental &ersion replaces spaghetti with rice an' forks with chopsticks, which perhaps explains *etter whyphilosophers nee' two chopsticks to eat.6
(s an a''itional feature, the #ining Philosophers can *e pause' for an ar*itrary perio' of time. #uringthis pause' perio', the Philosophers 'onAt get permissions to eat. (fter the pause perio', thePhilosophers shoul' resume normal operation.
,igure 1 The Dining Philosophers Problem$
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
-
8/12/2019 Dining Philosopher Principle
4/18
2 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
& Design an# "mplementation
&$1 -tep 1+ -e%uence Diagrams
( goo' starting point in 'esigning any e&ent-'ri&en system is to 'raw sequence 'iagrams for the mainscenarios 5main use cases6 i'entifie' from the pro*lem specification. 2o 'raw such 'iagrams, you nee' to*reak up your pro*lem into acti&e o*+ects with the main goal of minimi0ing the coupling among acti&eo*+ects. Bou seek a partitioning of the pro*lem that a&oi's resource sharing an' requires minimalcommunication in terms of num*er an' si0e of exchange' e&ents.
#PP has *een specifically concei&e' to make the philosophers conten' for the forks, which are theshare' resources in this case. /n acti&e o*+ect systems, the generic 'esign strategy for han'ling suchshare' resources is to encapsulate them insi'e a 'e'icate' acti&e o*+ect an' to let that o*+ect managethe share' resources for the rest of the system 5i.e., instea' of sharing the resources 'irectly, the rest ofthe application shares the 'e'icate' acti&e o*+ect6. @hen you apply this strategy to #PP, you willnaturally arri&e at a 'e'icate' acti&e o*+ect to manage the forks. 2his acti&e o*+ect has *een name'2a*leD.
2he sequence 'iagram in>igure $shows the most representati&e e&ent exchanges among any twoa'+acent Philosophers an' the 2a*le acti&e o*+ects.
,igure 2 The se%uence #iagram o the DPP application$
., Philo/n0 Table
2/%)E25$6
Philo/m0
thin1ing thin1ing serving
hungr2
-
8/12/2019 Dining Philosopher Principle
5/18
& of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
5F6 2he Philosopher9n: recei&es the 2/%)E2 e&ent, an' *eha&es exactly as Philosopher9m:, that is,transitions to hungryD an' posts
-
8/12/2019 Dining Philosopher Principle
6/18
' of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
(0! exte5n Q)ctive 2 const )_*a3leA 12 Gopaq"eG pointe5 to *a3le ) 21
#endif 12 dpp_h 21
56 >or smaller applications, such as the #PP, all signals can *e 'efine' in one enumeration 5ratherthan in separate enumerations or, worse, as preprocessor #definemacros6. (n enumeration
automatically guarantees the uniqueness of signals.5$6 4ote that the user signals must start with the offset Q_.%E/_%+,to a&oi' o&erlapping the reser&e'
Q%P signals.536 2he glo*ally pu*lishe' signals are groupe' at top of the enumeration. 2he 9);_$._%+,
enumeration automatically keeps track of the maximum pu*lishe' signals in the application.56 2he Philosophers post the =.7,/e&ent 'irectly to the 2a*le o*+ect rather than pu*licly pu*lish
the e&ent 5perhaps a Philosopher is em*arrasse'D to *e hungry, so it 'oes not want otherPhilosophers to know a*out it6. 2his 'emonstrates 'irect e&ent posting an' pu*lish-su*scri*emechanism coexisting in a single application.
56 2he 9);_%+,enumeration automatically keeps track of the total num*er of signals use' in the
application.
5F6 %&ery e&ent with parameters, such as the *a3leEvt'eri&es from the QEvent*ase structure.
2he listing shows how to keep the co'e an' 'ata structure of e&ery acti&e o*+ect strictly encapsulate'within its own C-file. >or example, all co'e an' 'ata for the acti&e o*+ect *a3leare encapsulate' in the
file ta3leHc, with the external interface consisting of the function *a3le_cto5(!an' the pointer
)_*a3le.
58-G6 2hese functions perform an early initiali0ation of the acti&e o*+ects in the system. 2hey play therole of static constructorsD, which in C you nee' to call explicitly, typically at the *eginning ofmain(!.
57-H6 2hese glo*al pointers represent acti&e o*+ects in the application an' are use' for posting e&ents'irectly to acti&e o*+ects. ?ecause the pointers can *e initiali0e' at compile time, they are
'eclare' const, sot that they can *e place' in ). 2he acti&e o*+ect pointers are opaqueD,*ecause they cannot access the whole acti&e o*+ect, *ut only the part inherite' from the Q)ctive
structure.
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
7/18
of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
&$& -tep &+ -tate 6achines
(t the application le&el, you can mostly ignore such aspects of acti&e o*+ects as the separate taskcontexts, or pri&ate e&ent queues, an' &iew them pre'ominantly as state machines. /n fact, your main +o*in 'e&eloping QP application consists of ela*orating the state machines of your acti&e o*+ects.
>igure 35a6 shows the state machines associate' with Philosopher acti&e o*+ect, which clearly shows thelife cycle consisting of states thinkingD, hungryD, an' eatingD. 2his state machine generates thea&or entry an' exit actions o&er actions on transitions.
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
thinking
entry ;
exit ;
hungry
entry ;
eating
entry ;
exit ;
;
2/%)E2
%(2, #)4% ;
9QJ%K2JC(!252a*le%&t6-p...
MM P
-
8/12/2019 Dining Philosopher Principle
8/18
of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
>igure 35*6 shows the state machine associate' with the 2a*le acti&e o*+ect. 2his state machine is tri&ial*ecause 2a*le keeps track of the forks an' hungry philosophers *y means of exten'e' state &aria*les,rather than *y its state machine. 2he state 'iagram in >igure 35*6 o*&iously 'oes not con&ey how the2a*le acti&e o*+ect *eha&es, as the specification of actions is missing. 2he actions are omitte' from the'iagram, howe&er, *ecause inclu'ing them require' cutting an' pasting most of the 2a*le co'e into the'iagram, which woul' make the 'iagram too cluttere'. /n this case, the 'iagram simply 'oes not a''much &alue o&er the co'e.
,igure ' Numbering o philosophers an# ors(see the macros 5E,T() an# R"8:T() in5isting 2)$
n
n/125n6
L%>25n6
L%>25L%>25n66 /1
-
8/12/2019 Dining Philosopher Principle
9/18
; of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
(?! #define /+,=*(n_! (("int8_t!(((n_! O (7_$=+B - .!! P 7_$=+B!!(6! #define BEJ*(n_! (("int8_t!(((n_! O .! P 7_$=+B!! #define J/EE (("int8_t!0! #define .%E (("int8_t!!
12 Bocal o3Kects -----------------------------------------------------------21(C! static *a3le l_ta3leA 12 the sin&le instance of the *a3le active o3Kect 21
12 ,lo3al-scope o3Kects ----------------------------------------------------21(8! Q)ctive 2 const )_*a3le l_ta3leHs"pe5A 12 Gopaq"eG ) pointe5 21
12HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 12 L(11?! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21(D! void *a3le_cto5(void! ' "int8_t nA *a3le 2me l_ta3leA
(0! Q)ctive_cto5(me->s"pe5 Q_%*)*E_I)%*(*a3le_initial!!A
(! fo5 (n 0.A n < 7_$=+BA OOn! ' me->fo5NnF J/EEA me->is="n&54nF 0.A @ @ 12 L(11! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 12 L(111! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 12 L(11110! 21 static Q%tate *a3le_initial(*a3le 2 const me QEvt const 2 const e! ' "int8_t nA (void!eA 12 s"pp5ess the compile5 wa5nin& a3o"t "n"sed pa5amete5 21
(! Q%_R_+I*+7)/(l_ta3le!A Q%_J.7_+I*+7)/(Q=sm_top!A Q%_J.7_+I*+7)/(*a3le_initial!A Q%_J.7_+I*+7)/(*a3le_se5vin&!A
Q%_%+,_+I*+7)/(7E_%+, (void 2!0!A 12 &lo3al si&nals 21 Q%_%+,_+I*+7)/(E)*_%+, (void 2!0!A Q%_%+,_+I*+7)/($).%E_%+, (void 2!0!A Q%_%+,_+I*+7)/(*E/9+7)*E_%+, (void 2!0!A
Q%_%+,_+I*+7)/(=.7,/_%+, me!A 12 si&nal K"st fo5 *a3le 21
(:! Q)ctive_s"3sc5i3e(me->s"pe5 7E_%+,!A
(! Q)ctive_s"3sc5i3e(me->s"pe5 $).%E_%+,!A(?! Q)ctive_s"3sc5i3e(me->s"pe5 *E/9+7)*E_%+,!A
fo5 (n 0.A n < 7_$=+BA OOn! ' me->fo5NnF J/EEA me->is="n&54nF 0.A(6! %$_displa4$hil%tat(n GthinNin&G!A @ 5et"5n Q_*/)7(*a3le_se5vin&!A @ 12 L(1111! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
10/18
< of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
static Q%tate *a3le_active(*a3le 2 const me QEvt const 2 const e! ' Q%tate stat"sA switch (e->si&! ' 12 L(111110! 21 case *E/9+7)*E_%+,M '(C! %$_te5minate(0!A stat"s Q_=)7BE(!A 35eaNA @ 12 L(11111! 21 case E)*_%+,M '(C! Q_E///(!A stat"s Q_=)7BE(!A 35eaNA @ defa"ltM ' stat"s Q_%.$E/(Q=sm_top!A 35eaNA @
@ 5et"5n stat"sA @ 12 L(11111! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 static Q%tate *a3le_se5vin&(*a3le 2 const me QEvt const 2 const e! ' Q%tate stat"sA switch (e->si&! ' 12 L(11111! 21 case Q_E7*/_%+,M ' "int8_t nA fo5 (n 0.A n < 7_$=+BA OOn! ' 12 &ive pe5missions to eatHHH 21 if ((me->is="n&54nF S 0.! (me->fo5NBEJ*(n!F J/EE! (me->fo5NnF J/EE!! ' *a3leEvt 2teA
me->fo5NBEJ*(n!F .%EA me->fo5NnF .%EA te Q_7ET(*a3leEvt E)*_%+,!A te->philo7"m nA QJ_$.B+%=(te->s"pe5 me!A me->is="n&54nF 0.A %$_displa4$hil%tat(n Geatin& G!A @ @ stat"s Q_=)7BE(!A
35eaNA @ 12 L(1111110! 21 case =.7,/_%+,M ' "int8_t n mA
n Q_EU*_I)%*(*a3leEvt!->philo7"mA 12 phil + m"st 3e in 5an&e and he m"st 3e not h"n&54 21 Q_)%%E/*((n < 7_$=+B! (me->is="n&54nF 0.!!A
%$_displa4$hil%tat(n Gh"n&54 G!A
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
11/18
= of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
m BEJ*(n!A 12 L(111111010! 21 if ((me->fo5NmF J/EE! (me->fo5NnF J/EE!! ' *a3leEvt 2peA me->fo5NmF .%EA me->fo5NnF .%EA pe Q_7ET(*a3leEvt E)*_%+,!A pe->philo7"m nA QJ_$.B+%=(pe->s"pe5 me!A %$_displa4$hil%tat(n Geatin& G!A stat"s Q_=)7BE(!A @ 12 L(11111101! 21 else ' me->is="n&54nF .A stat"s Q_=)7BE(!A @ 35eaNA @
12 L(111111! 21 case 7E_%+,M ' "int8_t n mA *a3leEvt 2peA
n Q_EU*_I)%*(*a3leEvt!->philo7"mA 12 phil + m"st 3e in 5an&e and he m"st 3e not h"n&54 21 Q_)%%E/*((n < 7_$=+B! (me->is="n&54nF 0.!!A
%$_displa4$hil%tat(n GthinNin&G!A m BEJ*(n!A 12 3oth fo5Ns of $hilnF m"st 3e "sed 21 Q_)%%E/*((me->fo5NnF .%E! (me->fo5NmF .%E!!A
me->fo5NmF J/EEA me->fo5NnF J/EEA m /+,=*(n!A 12 checN the 5i&ht nei&h3o5 21
if ((me->is="n&54mF S 0.! (me->fo5NmF J/EE!! ' me->fo5NnF .%EA me->fo5NmF .%EA me->is="n&54mF 0.A pe Q_7ET(*a3leEvt E)*_%+,!A pe->philo7"m mA QJ_$.B+%=(pe->s"pe5 me!A %$_displa4$hil%tat(m Geatin& G!A @
m BEJ*(n!A 12 checN the left nei&h3o5 21 n BEJ*(m!A 12 left fo5N of the left nei&h3o5 21 if ((me->is="n&54mF S 0.! (me->fo5NnF J/EE!! ' me->fo5NmF .%EA me->fo5NnF .%EA me->is="n&54mF 0.A pe Q_7ET(*a3leEvt E)*_%+,!A pe->philo7"m mA QJ_$.B+%=(pe->s"pe5 me!A %$_displa4$hil%tat(m Geatin& G!A @
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
12/18
10 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
stat"s Q_=)7BE(!A 35eaNA @ 12 L(111111! 21 case E)*_%+,M ' Q_E///(!A stat"s Q_=)7BE(!A 35eaNA @ 12 L(111111:! 21 case $).%E_%+,M ' stat"s Q_*/)7(*a3le_pa"sed!A 35eaNA @ defa"ltM ' stat"s Q_%.$E/(*a3le_active!A 35eaNA @ @
5et"5n stat"sA @ 12 L(11111:! HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 static Q%tate *a3le_pa"sed(*a3le 2 const me QEvt const 2 const e! ' Q%tate stat"sA switch (e->si&! ' H H H defa"ltM ' stat"s Q_%.$E/(*a3le_active!A 35eaNA @ @ 5et"5n stat"sA @
56 2o achie&e true encapsulation, 2he 'eclaration of the acti&e o*+ect structure is place' in thesource file 5.C file6.
5$6 %ach acti&e o*+ect in the application 'eri&es from the Q)ctive*ase structure.
536 2he 2a*le acti&e o*+ect keeps track of the forks in the array fo5NF. 2he forks are num*ere' as
shown in >igure .56 !imilarly, the 2a*le acti&e o*+ect nee's to remem*er which philosophers are hungry, in case the
forks arenOt imme'iately a&aila*le. 2a*le keeps track of hungry philosophers in the arrayis="n&54F. Philosophers are num*ere' as shown in >igure .
5-F6 2he helper macros BEJ*(!an' /+,=*(!access the left an' right philosopher or fork,
respecti&ely, as shown in >igure .586 2he 2a*le acti&e o*+ect is allocate' statically, which makes it inaccessi*le outsi'e of the .C file.5G6 %xternally, the 2a*le acti&e o*+ect is known only through the opaqueD pointer )_*a3le. 2he
pointer is 'eclare' constO 5with the const after the O6, which means that the pointer itself cannot
change. 2his ensures that the acti&e o*+ect pointer cannot change acci'entally an' also allowsthe compiler to allocate the acti&e o*+ect pointer in ).
576 2he function *a3le_cto5(!performs the instantiation of the 2a*le acti&e o*+ect. /t plays the role
of the static constructorD, which in C you nee' to call explicitly, typically at the *eginning ofmain(!.
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
13/18
11 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
N*TE+/n C==, static constructors are in&oke' automatically *efore main(!. 2his means that in the C==
&ersion of #PP 5foun' in \qpcpp\examples\80x86\dos\watcom\l\dpp\6, you pro&i'e a regular
constructor for the 2a*le class an' 'onOt *other with calling it explicitly.
-
8/12/2019 Dining Philosopher Principle
14/18
12 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
&$' -tep '+ "nitiali>ing an# -tarting the Application
ost of the system initiali0ation an' application startup can *e written in a platform-in'epen'ent way. /nother wor's, you can use essentially the same main(!function for the #PP application with many QP
ports.
2ypically, you start all your acti&e o*+ects from main(!. 2he signature of the Q)ctive_sta5t(!function
forces you to make se&eral important 'ecisions a*out each acti&e o*+ect upon startup. >irst, you nee' to'eci'e the relati&e priorities of the acti&e o*+ects. !econ', you nee' to 'eci'e the si0e of the e&entqueues you pre-allocate for each acti&e o*+ect. 2he correct si0e of the queue is actually relate' to thepriority, as 'escri*e' in Chapter 7 of P!iCC$. 2hir', in some Q> ports, you nee' to gi&e each acti&eo*+ect a separate stack, which also nee's to *e pre-allocate' a'equately. (n' finally, you nee' to 'eci'ethe or'er in which you start your acti&e o*+ects.
2he or'er of starting acti&e o*+ects *ecomes important when you use an )! or 2)!, in which aspawne' threa' starts to run imme'iately, possi*ly preempting the main(!threa' from which you launch
your application. 2his coul' cause pro*lems, if for example the newly create' acti&e o*+ect attempts topost an e&ent 'irectly to another acti&e o*+ect that has not *een yet create'. !uch situation 'oes notoccur in #PP, *ut if it is an issue for you, you can try to lock the sche'uler until all acti&e o*+ects arestarte'. Bou can then unlock the sche'uler in the QJ_on%ta5t"p(!call*ack, which is in&oke' right *efore
Q> takes o&er control. !ome 2)!s 5e.g., RC;)!-//6 allow you to 'efer starting multitasking until afteryou start acti&e o*+ects. (nother alternati&e is to start acti&e o*+ects from within other acti&e o*+ects, *utthis 'esign increases coupling *ecause the acti&e o*+ect that ser&es as the launch pa' must know thepriorities, queue si0es, an' stack si0es for all acti&e o*+ects to *e starte'.
5isting & "nitiali>ing an# -tarting the DPP Application (ile main$c)$
#incl"de Gqp_po5tHhG #incl"de GdppHhG #incl"de G3spHhG
12 Bocal-scope o3Kects -----------------------------------------------------21(! static QEvt const 2l_ta3leQ"e"e%to7_$=+BFA(! static QEvt const 2l_philoQ"e"e%to7_$=+BF7_$=+BFA(:! static Q%"3sc5Bist l_s"3sc5%to9);_$._%+,FA
12 sto5a&e fo5 event poolsHHH 21(! static QJ_9$B_EB(*a3leEvt! l_sml$ool%to.27_$=+BFA 12 small pool 21
12HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH21 int_t main(void! ' "int8_t nA
(?! $hilo_cto5(!A 12 instantiate all $hilosophe5 active o3Kects 21(6! *a3le_cto5(!A 12 instantiate the *a3le active o3Kect 21
(C! QJ_init(!A 12 initialiVe the f5amewo5N and the "nde5l4in& /* Ne5nel 21(8! %$_init(!A 12 initialiVe the %$ 21
12 o3Kect dictiona5iesHHH 21(D! Q%_R_+I*+7)/(l_sml$ool%to!A Q%_R_+I*+7)/(l_ta3leQ"e"e%to!A Q%_R_+I*+7)/(l_philoQ"e"e%to0F!A Q%_R_+I*+7)/(l_philoQ"e"e%toF!A Q%_R_+I*+7)/(l_philoQ"e"e%toF!A Q%_R_+I*+7)/(l_philoQ"e"e%to:F!A
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
15/18
1& of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
Q%_R_+I*+7)/(l_philoQ"e"e%toF!A
(0! QJ_ps+nit(l_s"3sc5%to Q_+9(l_s"3sc5%to!!A 12 init p"3lish-s"3sc5i3e 21
12 initialiVe event poolsHHH 21(! QJ_pool+nit(l_sml$ool%to siVeof(l_sml$ool%to! siVeof(l_sml$ool%to0F!!A
fo5 (n 0.A n < 7_$=+BA OOn! ' 12 sta5t the active o3KectsHHH 21(! Q)ctive_sta5t()_$hilonF ("int8_t!(n O .! l_philoQ"e"e%tonF Q_+9(l_philoQ"e"e%tonF! (void 2!0 0. (QEvt 2!0!A @(:! Q)ctive_sta5t()_*a3le ("int8_t!(7_$=+B O .! l_ta3leQ"e"e%to Q_+9(l_ta3leQ"e"e%to! (void 2!0 0. (QEvt 2!0!A
(! 5et"5n (int_t!QJ_5"n(!A 12 5"n the QJ application 21 @
5-$6 2he memory *uffers for all e&ent queues are statically allocate'.536 2he memory space for su*scri*er lists is also statically allocate'. 2he 9);_$._%+,enumeration
comes in han'y here.56 2he macro QJ_9$B_EB(*a3leEvt!pro&i'es correctly aligne' memory *lock of the si0e at least
as *ig as the siVeof(*a3leEvt!for all e&ents that are ser&e' *y the smallD e&ent pool.
5-F6 2he main(!function starts with calling all static constructorsD 5see Listing 58-G66. 2his step is not
necessary in C==.586 Q> is initiali0e' together with the un'erlying )!;2)!.5G6 2he target *oar' is initiali0e'.576 2he macros staring with Q!J pertain to the Q-!PB software tracing instrumentation an' are
acti&e only in the !PB *uil' configuration.5H6 2he pu*lish-su*scri*e mechanism is initiali0e'. Bou 'onOt nee' to call Q>Jps/nit56 if your
application 'oes not use pu*lish-su*scri*e.56 Ep to three e&ent pools can *e initiali0e' *y calling QJ_pool+nit(!up to three times. 2he
su*sequent calls must *e ma'e in the or'er of increasing *lock-si0es of the e&ent pools. Bou'onOt nee' to call QJ_pool+nit(!if your application 'oes not use 'ynamic e&ents.
5$-36 (ll acti&e o*+ects are starte' using the opaqueD acti&e o*+ect pointers 5see Listing 57-H66. /nthis particular example, the acti&e o*+ects are starte' without pri&ate stacks.
-
8/12/2019 Dining Philosopher Principle
16/18
1' of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
&$ -tep + 8raceull Terminating the Application
2erminating an application is not really a *ig concern in em*e''e' systems, *ecause em*e''e'programs almost ne&er ha&e a nee' to terminate gracefully. 2he +o* of a typical em*e''e' system isne&er finishe' an' most em*e''e' software runs fore&er or until the power is remo&e', whiche&er comesfirst.
N*TE+Bou still nee' to carefully 'esign an' test the fail-safe mechanism triggere' *y a CPE exceptionor assertion &iolation in your em*e''e' system. in&okes the QJ_onIlean"p(!call*ack. 2his
call*ack gi&es the application the last chance to cleanup glo*ally 5e.g., the #)! &ersion restores theoriginal #)! interrupt &ectors6.
>inally, you can also stop in'i&i'ual acti&e o*+ects an' let the rest of the application continue execution.2he cleanest way to en' an acti&e o*+ectOs threa' is to ha&e it stop itself *y calling Q)ctive_stop(me!,
which shoul' cause a return from the acti&e o*+ectOs threa' routine. )f course to commit a suici'eD&oluntarily, the acti&e o*+ect must *e running, an' cannot *e waiting for an e&ent. /n a''ition, *efore
'isappearing, the acti&e o*+ect shoul' release all the resources acquire' 'uring its lifetime. (''itionally,the acti&e o*+ect shoul' unsu*scri*e from recei&ing all signals, an' somehow shoul' make sure that nomore e&ents will *e poste' to it 'irectly. Enfortunately, all these requirements cannot *e pre-programme'generically an' always require some work on the application programmerOs part.
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
17/18
1 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
' Reerences
Document 5ocation
9P!iCC$: Practical EL !tatecharts inC;C==, !econ' %'itionD, iro !amek,4ewnes, $HHG, /!?4 H8HFG8HF
(&aila*le from most online *ook retailers, such asama0on.com. !ee also" http";;www.state-machine.com;psicc$
9QP;C HG: QP;C eference anualD,Quantum Leaps, LLC, $HHG
http";;www.state-machine.com;'oxygen;qpc;
9QP;C== HG: QP;C== eferenceanualD, Quantum Leaps, LLC, $HHG
http";;www.state-machine.com;'oxygen;qpcpp;
9QP-nano HG: QP-nano eferenceanualD, Quantum Leaps, LLC, $HHG
http";;www.state-machine.com;'oxygen;qpn;
9QL (4-#irectory H8: (pplication 4ote"QP #irectory !tructureD, Quantum Leaps,
LLC, $HH8
http";;www.state-machine.com;'oc; -(4JQPJ#irectoryJ!tructure.p'f
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/http://www.amazon.com/http://www.state-machine.com/psicc2/http://www.state-machine.com/psicc2/http://www.state-machine.com/doxygen/qpc/http://www.state-machine.com/doxygen/qpcpp/http://www.quantum-leaps.com/doxygen/qpn/http://www.state-machine.com/doc/%1FAN_QP_Directory_Structure.pdfhttp://www.state-machine.com/doc/%1FAN_QP_Directory_Structure.pdfhttp://www.amazon.com/http://www.state-machine.com/psicc2/http://www.state-machine.com/psicc2/http://www.state-machine.com/doxygen/qpc/http://www.state-machine.com/doxygen/qpcpp/http://www.quantum-leaps.com/doxygen/qpn/http://www.state-machine.com/doc/%1FAN_QP_Directory_Structure.pdfhttp://www.state-machine.com/doc/%1FAN_QP_Directory_Structure.pdfhttp://www.state-machine.com/ -
8/12/2019 Dining Philosopher Principle
18/18
1 of 1
(pplication 4ote"#ining Philosophers Pro*lem %xample
www.state-machine.com
!ontact "normation
.uantum 5eaps3 55!H3 Co**le i'ge #ri&eChapel (S6e-mail"infoTquantum-leaps.com@%? " www.state-machine.com
Practical EL!tatecharts inC;C==, !econ'%'itionD 5P-i!!26,
*y iro !amek,4ewnes, $HHG,/!?4 H8HFG8HF
Copyright Quantum Leaps, LLC. (ll ights eser&e'.
http://www.state-machine.com/mailto:[email protected]:[email protected]:[email protected]://www.state-machine.com/mailto:[email protected]://www.state-machine.com/http://www.state-machine.com/