Rsourc Allocation Abstract This is a small toy xampl which is wll-suitd as a first introduction to Cnts. Th CN modl is dscribd in grat dtail, xplaining th basic concpts of C-nts. Hnc, it can b rad by popl with no/littl tri nt background. Th CN modl dscribs how two diffrnt kinds of procsss ar sharing thr diffrnt kinds of rsourcs. It is simpl to undrstand and asy to simulat/modify. Th xampl is takn from Sct. 1.2 of Vol. 1 of th CN book. Dvlopd and Maintaind by: Kurt Jnsn, Aarhus Univrsity, Dnmark (kjnsn@daimi.au.dk).
2 CN Modl Assum that w hav a st of procsss, which shar a common pool of rsourcs. Thr ar two diffrnt kinds of procsss (calld p-procsss and q-procsss) and thr diffrnt kinds of rsourcs (calld r-rsourcs, s-rsourcs, and t-rsourcs). Th procsss could b diffrnt computr programs (.g., txt ditors and drawing programs) whil th rsourcs could b diffrnt facilitis shard by th programs (.g., tap drivs, lasr printrs and plottrs). ach procss is cyclic and during th individual parts of its cycl, th procss nds to hav xclusiv accss to a varying amount of th rsourcs. Th rsourc allocation systm is modlld by th C-nt shown blow. Initial Marking M 0 color U = with p q; color I = int; color = product U*I; color = with ; var x: U; var i: I; 1 R 1 1 3 (q,0) A 3 3 (q,0) T1 if x =q thn 1 (q,i+1) ls mpty 2 (p,0) 2 2 (p,0) B cas x of if x=p p => 2 thn 1 (p,i+1) q => 1 ls mpty T2 [x=q] 3 S 3 3 if x=p thn 1 ls mpty if x=q thn 1 ls mpty C T3 D 2 T 2 2 T4 2 cas x of p => 2 q => 1 T5
Th procsss can b in fiv diffrnt stats, rprsntd by th placs A. ach plac may contain on or mor markrs, calld tokns. ach tokn carris a data valu calld th tokn colour. Th data valu may b of arbitrarily complx typ (.g., a rcord whr th first fild is a ral, th scond a txt string, whil th third is a list of intgr pairs). For a givn plac all tokns must hav tokn colours that blong to a spcifid typ. This typ is calld th colour st of th plac. Th us of colour sts in C-nts is totally analogous to th us of typs in programming languags. Colour sts dtrmin th possibl valus of tokns (analogously to th way in which typs dtrmin th possibl valus of variabls and xprssions). For historical rasons w talk about colourd tokns which can b distinguishd from ach othr in contrast to th plain tokns of an ordinary tri nt. By convntion w writ colour sts in italics. From th abov figur, it can b sn that th placs A hav th typ as colour st, whil th placs R T hav th typ as colour st. Th dclarations of th colour sts (in th uppr lft cornr of th figur) tll us that ach tokn on A has a tokn colour which is a pair (bcaus th colour st is dclard to b th Cartsian product of two othr colour sts U and I). Th first lmnt of th pair is an lmnt of U and thus it is ithr p or q (bcaus th colour st U is dclard to b an numration typ with ths two lmnts). Th scond lmnt is an intgr (bcaus th colour st I is dclard by mans of th CN ML standard typ int, which contains all intgrs in an implmntation-dpndnt intrval). Intuitivly, th first lmnt of a tokn tlls whthr th tokn rprsnts a p-procss or a q-procss, whil th scond lmnt tlls how many full cycls th procss has compltd. It can also b sn that all th tokns on R T hav th sam tokn colour ( is th only lmnt of ). Intuitivly, this mans that ths tokns carry no information apart from thir prsnc/absnc at a plac. Th initial marking is dtrmind by valuating th initialization xprssions, i.., th undrlind xprssions nxt to th placs. In th initial marking thr ar thr (q,0)-tokns on A and two (p,0)-tokns on B, whil C, D and hav no tokns (by convntion w omit initialization xprssions which valuat to th mpty multi-st). Morovr, R has on -tokn, S has thr -tokns and T has two -tokns. Th marking of ach plac is a multi-st ovr th colour st attachd to th plac. W nd multi-sts to allow two or mor tokns to hav idntical tokn colours. If w only workd with sts it would b impossibl, for xampl, to hav thr (q,0)-tokns in th initial marking of A. Th currnt marking of a givn plac is rprsntd by mans of a small circl (with an intgr saying how many tokns thr ar) and a txt string nxt to th circl (with a multi-st saying what th individual tokn colours ar, and which cofficints thy hav). By convntion w omit th circl and th txt string for placs which hav no tokns. In th figur abov, th currnt marking 3
4 is idntical to th initial marking, and this mans that th small circls and thir txt strings contain th sam information as th initialization xprssions. ach of th fiv transitions T1 T5 rprsnts a shift from on stat to th nxt. Th surrounding arc inscriptions tll us how rsourcs ar rsrvd and rlasd. To s how this works, lt us considr transition T2 which has thr surrounding arcs. Th arc xprssion appars twic (on th input arc from B and on th output arc to C) whil cas x of p=>2` q=>1` appars onc (on th input arc from S). Togthr ths thr arc xprssions hav two variabls, x and i, and from th dclarations it can b sn that x has typ U whil i has typ I. At a first glanc on might also think that, p and q ar variabls, but from th dclarations it can b sn that this is not th cas: is an lmnt of th colour st, whil p and q ar lmnts of U. This mans that thy ar constants. Intuitivly, th thr arc xprssions tll us that an occurrnc of T2 movs a tokn from B to C without changing th colour (bcaus th two arc xprssions ar idntical). Morovr, th occurrnc Marking M 1 color U = with p q; color I = int; color = product U*I; color = with ; var x: U; var i: I; 1 R 1 1 3 (q,0) A 3 3 (q,0) T1 if x =q thn 1 (q,i+1) ls mpty 2 (p,0) 1 1 (p,0) B cas x of if x=p p => 2 thn 1 (p,i+1) q => 1 ls mpty T2 [x=q] 3 if x=q thn 1 ls mpty C 1 1 (p,0) S 1 1 T3 if x=p thn 1 ls mpty D 2 T 2 2 T4 2 cas x of p => 2 q => 1 T5
rmovs a multi-st of tokns from S. This multi-st is dtrmind by valuating th corrsponding arc xprssion. As it can b sn, th multi-st dpnds upon th kind of procss involvd. A p-procss nds two s-rsourcs to go from B to C (and thus it rmovs two -tokns from S), whil a q-procss only nds on s-rsourc to go from B to C (and thus it rmovs only on -tokn from S). Now lt us b a littl mor prcis, and xplain in dtail how th nabling and occurrnc of C-nt transitions ar dtrmind. Th transition T2 has two variabls (x and i), and bfor w can considr an occurrnc of th transition ths variabls hav to b bound to colours of th corrsponding typs (i.., lmnts of th colour sts U and I). This can b don in many diffrnt ways. On possibility is to bind x to p and i to zro: thn w gt th binding b 1 = <x=p,i=0>. Anothr possibility is to bind x to q and i to 37: thn w gt th binding b 2 = <x=q,i=37>. For ach binding w can chck whthr th transition, with that binding, is nabld (in th currnt marking). For th binding b 1 th two input arc xprssions valuat to (p,0) and 2, rspctivly. Thus w conclud that b 1 is nabld in th initial marking bcaus ach of th input placs contains at last th tokns to which th corrsponding arc xprssion valuats (on (p,0)-tokn on B and two -tokns on S). For th binding b 2 th two arc xprssions valuat to (q,37) and. Thus w conclud that b 2 is not nabld (thr is no (q,37)-tokn on B). A transition can occur in as many ways as w can bind th variabls that appar in th surrounding arc xprssions (and in th guard introducd blow). Howvr, for a givn marking, it is usually only a fw of ths bindings that ar nabld. Whn a transition is nabld (for a crtain binding) it may occur, and it thn rmovs tokns from its input placs and adds tokns to its output placs. Th numbr of rmovd/addd tokns and th colours of ths tokns ar dtrmind by th valu of th corrsponding arc xprssions (valuatd with rspct to th binding in qustion). A pair (t,b) whr t is a transition and b a binding for t is calld a binding lmnt. Th binding lmnt (T2,b 1 ) is nabld in th initial marking M 0 and it transforms M 0 into th marking M 1 (shown blow). Analogously, w conclud that th binding lmnt (T1,<x=q,i=0>) is nabld in M 0 and that it transforms M 0 into th marking M 2 (shown blow). W say that ach of th markings M 1 and M 2 is dirctly rachabl from M 0. Th binding lmnt (T2,b 2 ) is not nabld in M 0 and thus it cannot occur. Nxt, lt us tak a closr look at transition T5. This transition movs a tokn from plac to ithr A or B (p-procsss go to B, whil q-procsss go to A). Simultanously th transition updats th cycl countr i. Notic that diffrnt bindings for a transition may not only rsult in diffrnt tokn colours but also in diffrnt numbrs of tokns. In particular this may man that th multi-st of tokns which ar addd/rmovd, for a givn binding, may b mpty, as 5
6 illustratd by th two thick output arcs of T5. W hav positiond th first sgmnts of th two arcs on top of ach othr to illustrat th clos rlationship btwn thm. Howvr, it should b strssd that this has no formal maning. Th only purpos is to mak th drawing mor radabl for human bings. Nxt lt us look at transition T1, which in addition to th arc xprssions has a guard: [x=q]. A guard is a boolan xprssion (i.., an xprssion that valuats to ithr tru or fals). It may hav variabls in xactly th sam way that th arc xprssions hav. Th purpos of a guard is to dfin an additional constraint which must b fulfilld for a transition to b nabld. In this cas th guard tlls us that it is only tokns rprsnting q-procsss which can mov from A to B (bcaus th guard for all bindings <x=p, > valuats to fals and thus prvnts nabling). It is asy to s that w in this cas could hav omittd th guard, bcaus w nvr will hav p-tokns on plac A. Howvr, Marking M 2 color U = with p q; color I = int; color = product U*I; color = with ; var x: U; var i: I; 1 R 3 (q,0) A 2 2 (q,0) T1 if x =q thn 1 (q,i+1) ls mpty 2 (p,0) 3 2 (p,0)++1 (q,0) B cas x of if x=p p => 2 thn 1 (p,i+1) q => 1 ls mpty T2 [x=q] 3 if x=q thn 1 ls mpty C S 2 2 T3 if x=p thn 1 ls mpty D 2 T 2 2 T4 2 cas x of p => 2 q => 1 T5
adding th guard maks our dscription mor robust towards rrors. Whn th sam variabl nam appars mor than onc, in th guard/arc xprssions of a singl transition, w only hav on variabl (with multipl apparancs). ach binding of th transition spcifis a colour for th variabl and this colour is usd for all th apparancs. Howvr, it should b notd that th apparancs of x around T1 ar totally indpndnt of th apparancs of x around T2 in th sns that th two sts of apparancs in th sam stp can b bound to diffrnt colour valus. It can b shown that th rsourc allocation systm prsntd abov has no dadlock (i.., no rachabl marking in which no binding lmnt is nabld). Howvr what happns if w chang th numbr of procsss or th numbr of rsourcs? As an xampl, lt us assum that th initial marking has an xtra s-rsourc (i.., an xtra -tokn on S). On should xpct that this small modification cannot lad to a dadlock bcaus dadlocks appar whn w hav too fw rsourc tokns, and thus an xtra rsourc tokn cannot caus a dadlock. Is th argumnt abov convincing? At a first glanc: ys! Howvr, this argumnt is wrong. Adding th xtra s-rsourc actually mans that w can rach a dadlock. This can b sn by ltting th two p-procsss advanc from stat B to stat D, whil th q-procsss rmain in stat A. Hopfully, this small xampl dmonstrats that informal argumnts about bhavioural proprtis ar dangrous and this is on of our motivations for th dvlopmnt of th mor formal analysis mthods, such as occurrnc graphs and plac invariants. 7