last t ime
play

Last t ime Need f or synchronizat ion primit ives 7: Synchronizat - PDF document

Last t ime Need f or synchronizat ion primit ives 7: Synchronizat ion Locks and building locks f rom HW primit ives Semaphores and building semaphores f rom Last Modif ied: locks 6/ 7/ 2004 1:21:28 PM -1 -2 Semaphores f or


  1. Last t ime � Need f or synchronizat ion primit ives 7: Synchronizat ion � Locks and building locks f rom HW primit ives � Semaphores and building semaphores f rom Last Modif ied: locks 6/ 7/ 2004 1:21:28 PM -1 -2 Semaphores f or expressing ordering Uses of Semaphores � Mut ual exclusion � I nit ialize semaphore value t o 0 � Binar y semaphor es (wait / signal used j ust like � Code: lock/ unlock) P P � “hold” i j Μ Μ � Managing N copies of a r esour ce � Count ing semaphor es A wait � “ent er” signal B � Anyt hing else? � Execut e B in P j only af t er A execut ed in P i � Anot her t ype of synchr onizat ion is t o expr ess � Not e: I f signal execut es f irst , wait will or der ing/ scheduling const r aint s � “Don’t allow x t o pr oceed unt il af t er y” f ind it is an signaled st at e (hist ory!) -3 -4 Event s and Signals Window’s Event s � Recall: UNI X signals � Cr eat e/ dest r oy � Kill = send signal; Signal = cat ch signal HANDLE CreateEvent ( LPSECURITY_ATTRIBUTES lpsa, // security privileges (default = NULL) � Many syst em def ined but also signals lef t t o user BOOL bManualReset , // TRUE if event must be reset manually def init ion BOOL bInitialState , // TRUE to create event in signaled state � Can be used f or synchronizat ion LPTSTR lpszEventName ); // name of event (may be NULL) • Signal handler set s a f lag BOOL CloseHandle ( hObject ); • Main t hr ead polls on t he value of t he f lag � W ait • Busy wait t hough DWORD WaitForSingleObject ( � Window’s Event s HANDLE hObject, // object to wait for � Synchronizat ion obj ect s used somewhat like semaphores DWORD dwMilliseconds ); when t hey are used f or ordering/ scheduling const raint s � Signal (all t hr eads t hat wait on it r eceive) � One process/ t hread can wait f or an event t o be signaled BOOL SetEvent ( HANDLE hEvent ); //signal on by anot her process/ t hread BOOL ResetEvent( HANDLE hEvent ); //signal off -5 -6 1

  2. Generalize t o Messaging Compiler help? � Synchronizat ion based on dat a t ransf er � There is no synt act ic connect ion bet ween (at omic) across a channel t he semaphore ( or lock or event ) and t he � I n general, messages can be used t o shared dat a/ resources it is prot ect ing express ordering/ scheduling const raint s � Thus t he “meaning” of t he semaphore is � Wait f or message bef or e do X def ined by t he pr ogr ammer ’s use of it � Send message = signal � Bad sof t war e engineer ing � Direct ext ension t o dist ribut ed syst ems • Semaphores basically global variables accessed by all t hreads � Easy f or pr ogr ammer s t o make mist akes -7 -8 P rogramming Language Support Monit ors � Add programming language support f or � A monit or is a sof t ware module t hat synchronizat ion encapsulat es: � Declar e a sect ion of code t o r equir e mut ually � Shar ed dat a st r uct ur es exclusive access (like J ava’s synchr onized) � Pr ocedur es t hat oper at ed on t hem � Associat e t he shar ed dat a it self wit h t he � Synchr onizat ion r equir ed of pr ocesses t hat locking aut omat ically invoke t hese pr ocedur es � Monit or = programming language support t o � Like a public/ privat e dat a int erf ace enf orce synchronizat ion prevent s access t o privat e dat a members; � Mut ual exclusion code added by t he compiler ! Monit ors prevent unsynchronized access t o shared dat a st ruct ures -9 -10 Example: bankAccount Monit or Monitor bankAccount{ int balance; balance Shared dat a int readBalance( ){return balance}; One t hr ead void upateBalance(int newBalance){ I n Monit or balance = newBalance; Locking added r eadBalance S } by t he compiler! int withdraw ( int amount) { updat eBalance Pr ocedur es Wait ing queue wit hdraw balance = balance – amount; return balance; deposit } int deposit (int amount){ balance = balance + amount; return balance; } } -11 -12 2

  3. Wait ing I nside a Monit ors Wait and signal � What if you need t o wait f or an event wit hin one � Condit ion variables, like semaphores, have t he t wo operat ions have t he t wo of t he procedures of a monit or? operat ions, wait and signal. � Monit or s as we have seen t o t his point enf or ce mut ual exclusion – what about t he � The oper at ion x. wait() means t hat t he pr ocess invoking t his oper at ion is suspended unt il � I nt r oduce anot her synchr onizat ion obj ect , t he anot her pr ocess invokes x. signal(); condit ion var iable � The oper at ion wait allows anot her pr ocess t o � Wit hin t he monit or declar e a condit ion var iable: ent er t he monit or (or no one could ever call condit ion x; signal!) � The x. signal oper at ion r esumes exact ly one suspended pr ocess. I f no pr ocess is suspended, t hen t he signal oper at ion has no ef f ect -13 -14 Monit or Wit h Condit ion Semaphores vs Condit ion Variables Variables Condit ion Var iables and t heir associat ed wait queues � I ’d like t o be able t o say t hat condit ion variables are j ust like semaphores but … balance One t hr ead � Wit h condit ion variables, if no process is Running in suspended t hen t he signal operat ion has no Monit or r eadBalance S ef f ect updat eBalance � Wit h semaphores, signal increment s t he Wait ing queue wit hdraw value regardless of whet her any process is deposit wait ing � Semaphores have “hist ory” (t hey remember signals) while condit ion variables have no hist ory -15 -16 Condit ion Variables f or Condit ion Variable Alone? ordering/ scheduling � Could you use a condit ion variable concept out side of monit ors? � Code: � Yes, basically a semaphore wit hout hist ory P P i j � Couldn’t do locking wit h it because no mut ual Μ Μ exclusion on it s own A wait � Couldn’t do r esour ce management (count ing signal B semaphor e) because no value/ hist or y � Could you use it f or or der ing/ scheduling � Execut e B in P j only af t er A execut ed in P i const r aint s? Yes but wit h dif f er ent semant ics � I f signal f ir st , it is lost ; wait will block unt il next signal ( no hist ory!) -17 -18 3

  4. P seudo-Monit ors P t hread’s Condit ion Variables � Monit or = a lock (implied/ added by � Cr eat e/ dest r oy compiler) f or mut ual exclusion PLUS zero int pthread _cond _init (pthread_cond_t *cond, pthread _condattr_t *attr); int pthread _cond _destroy ( pthread _cond _t *cond); or more condit ion variables t o express � W ait ordering const raint s int pthread _cond _wait (pthread_cond_t *cond, pthread _mutex_t *mut ); � What if we want ed t o have monit or wit hout � Timed Wait int pthread _cond _timedwait ( pthread _cond _t *cond, pthread_mutex_t *mut, const programming language support ? struct timespec *abstime); � Declar e locks and t hen associat e condit ion � Signal var iables wit h a lock int pthread _cond _signal (pthread _cond_t *cond); � I f wait on t he condit ion var iable, t hen r elease � Br oadcast t he lock int pthread _cond _broadcast (pthread _cond_t *cond); -19 -20 Example: P seudo- monit ors Monit or I nvariant s pthread_mutex _t monitorLock; � Monit or invariant s = rules t hat must hold pthread_cond_t conditionVar; whenever no t hread is in t he monit or void pseudoMonitorProc(void) � Not checked by compiler { pthread_mutex _lock(&mutexLock ); � More like pre/ post condit ions t o be ….. respect ed by t he programmer pthread_cond_wait(&conditionVar, &monitorLock); …. pthread_mutex _unlock(&mutexLock); } -21 -22 Who f irst ? Does it mat t er? Yes � I f t hread in Monit or calls x.signal waking � I f signalee runs immediat ely, t hen clearly anot her t hread t hen who is running in t he “condit ion” being signaled st ill holds monit or now? (Can’t bot h be running in t he � Signaler must r est or e any “monit or invar iant s” monit or!) bef or e signaling � Hoare monit ors � I f signalee runs lat er, t hen when it f inally � Signalee r uns; signaler blocks does ent er t he monit or it must recheck � Signaler put on monit or queue condit ion bef ore execut ing � Mesa monit ors � Signaler need not r est or e any “monit or � Signaler cont inues; signalee blocks invar iant s” bef or e signaling – j ust bef ore � Signalee moved f r om condit ion var iable queue exit ing t o monit or queue -23 -24 4

Recommend


More recommend