kat_sys_2010

Page 66

58

KEFALAIO 2. ULOPOIHSH PELATWN KAI UPHRETWN

Condition variables

àna condition variable susqet zetai me k poio lock, aplì reentrant, kai diajètei dÔo mejìdou : ti wait kai notify. Mìno èna n ma pou èqei desmeÔsei to lock mpore na ti kalèsei. ätan klhje h wait, ekteloÔntai atomik dÔo b mata: (a) to lock eleujer¸netai kai (b) h ektèlesh tou n mato emplèketai. ätan k poio n ma tn kalèsei th notify, tìte k poio llo n ma tw pou e nai emplegmèno apì kl sh th wait apemplèketai apì th wait kai pospaje na desmeÔsei to lock. Prosoq ; h kl sh th notify ègine me to lock desmeumèno apì to n ma tn , ra to n ma tw den xekin amèsw , all afoÔ to tn eleujer¸sei to lock. Up rqei ep sh kai h mèjodo notifyAll h opo a apemplèkei ìla ta n mata pou e nai emplegmèna sth wait. Fusik , ta n mata aut ja emplakoÔn ìla sto lock tou condition variable. Ta condition variables e nai isodÔnama me tou shmatofore , ne thn ènnoia ìti èna shmatoforèa mpore na ulopoihje apì condition variables, kai ant strofa. DÔo tètoie aploðkè ulopoi sei qwr elègqou sfalm twn fa nontai sto Prog 2.14. 2.8.2

Teqnikè sugqronismoÔ nhm twn

Oi mhqanismo sugqronismoÔ nhm twn pou perigr yame pio p nw e nai arket aplo gia na mporoÔn na ulopoihjoÔn me mikrì kìsto se sÔgqronou epexergastè . Me b sh autoÔ , mporoÔn na ulopoihjoÔn periplokìteroi mhqanismo , h qr sh twn opo wn mpore na aplousteÔsei ton k¸dika th efarmog . Ja exet soume parak tw k poiou apì tou pio dhmofile . Monitors

H dom twn condition variables prot jhke apì ton Hoare san upomhqanismì mia genikìterh dom me to ìnoma monitor. àna monitor omoi zei polÔ me èna antike meno, afoÔ perièqei topik ped a (attributes) kai mejìdou . àqei ìmw tou ex periorismoÔ . • Ta ped a e nai idiwtik (private). • To monitor perièqei ep sh èna reentrant lock, kai mhdèn perissìtera condition variables, pou ìla susqet zontai me to dio lock, autì tou monitor. • Tèlo , h kl sh k je mejìdou tou monitor desmeÔei to lock me thn e sodo kai to apodesmeÔei me thn èxodo th kl sh .

àqoume dh sunant sei èna par deigma monitor, sthn ulopo hsh th kl sh

SemaphoreByCondition sto Prog. 2.14. Mia llh qr sh monitor ja g nei sto

Prog 2.15 pou ulopoie th dom bounded buffer pou ja exet soume parak tw. H gl¸ssa Java qrhsimopoie ta monitors polÔ ektetamèna: k je antike meno th Java e nai dun mei èna monitor. Bèbaia, sÔmfwna me ton austhrì orismì twn monitors, autì den e nai swstì; mìno ta antike mena eke na pou ìla ta ped a tou


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.