b'\n_START_ARTICLE_\nMonitor (sincronizare)\n_START_PARAGRAPH_\n\xc3\x8en domeniul program\xc4\x83rii, un monitor este o metod\xc4\x83 de sincronizare a dou\xc4\x83 sau mai multe sarcini ce folosesc o resurs\xc4\x83 comun\xc4\x83, de obicei un dispozitiv hardware sau o mul\xc8\x9bime de variabile. \xc3\x8en concuren\xc8\x9ba pentru monitor compilatorul sau interpretorul introduce cod,_NEWLINE_\xc3\xaen mod \xe2\x80\x9etransparent\xe2\x80\x9d (automat \xc8\x99i neexplicit), pentru blocarea sau deblocarea unor proceduri specificate, f\xc4\x83r\xc4\x83 a fi nevoie ca programatorul s\xc4\x83 acceseze explicit elementele de sincronizare._NEWLINE_A fost inventat de Per Brinch Hansen, implementat pentru prima dat\xc4\x83 \xc3\xaen limbajul Concurrent Pascal \xc8\x99i folosit pentru a structura comunicarea \xc3\xaentre procese in sistemul de operare Solo.\n_START_SECTION_\nVariabile condi\xc8\x9bionale\n_START_PARAGRAPH_\nCa s\xc4\x83 nu se intre \xc3\xaentr-o stare de "a\xc8\x99teptare ocupat\xc4\x83", procesele trebuie sa poat\xc4\x83 s\xc4\x83 se semnaleze \xc3\xaen leg\xc4\x83tur\xc4\x83 cu evenimente notabile._NEWLINE_Monitoarele pun la dispozi\xc8\x9bie aceast\xc4\x83 capabilitate prin intermediul variabilelor condi\xc8\x9bionale._NEWLINE_C\xc3\xa2nd o func\xc8\x9bie a monitorului are nevoie ca o anumita condi\xc8\x9bie s\xc4\x83 fie adev\xc4\x83rat\xc4\x83 \xc3\xaenainte de a continua, atunci se asteapt\xc4\x83 dup\xc4\x83 o variabil\xc4\x83 condi\xc8\x9bional\xc4\x83 asociat\xc4\x83. A\xc8\x99tept\xc3\xa2nd, el elibereaz\xc4\x83 \xc3\xaencuietoarea \xc8\x99i este eliminat din mul\xc8\x9bimea proceselor care ruleaz\xc4\x83._NEWLINE_Orice alt proces care evalueaz\xc4\x83 conditia la adev\xc4\x83rat, poate folosi variabila condi\xc8\x9bional\xc4\x83 pentru a semnaliza un proces care a\xc8\x99teapt\xc4\x83._NEWLINE_Astfel, un proces semnalizat ob\xc8\x9binei iar\xc4\x83\xc8\x99i \xc3\xaencuietoarea \xc8\x99i poate continua._NEWLINE_Urm\xc4\x83torul monitor foloseste variabile de condi\xc8\x9bie pentru a implementa o comunicare \xc3\xaentre procese ce pot re\xc8\x9bine doar o valoare \xc3\xaentreag\xc4\x83 la un anumit moment._NEWLINE_monitor canal {_NEWLINE_  int continut_NEWLINE_  boolean plin\xc2\xa0:= false_NEWLINE_  condition snd_NEWLINE_  condition rcv_NEWLINE__NEWLINE_  function trimite(int mesaj) {_NEWLINE_    if plin then wait(rcv)   [Sematici Hoare: Vezi explica\xc8\x9bia mai jos]_NEWLINE_    contents\xc2\xa0:= mesaj_NEWLINE_    plin\xc2\xa0:= true_NEWLINE_    notify(snd)_NEWLINE_  }_NEWLINE__NEWLINE_  function primieste() {_NEWLINE_    var int primit_NEWLINE__NEWLINE_    if not plin then wait(snd)   [Semantici Hoare: Vezi explica\xc8\x9bia mai jos]_NEWLINE_    primit\xc2\xa0:= continut_NEWLINE_    plin\xc2\xa0:= false_NEWLINE_    notify(rcv)_NEWLINE_    return primit_NEWLINE_  }_NEWLINE_}\n_START_SECTION_\nSemantici Hoare si Mesa\n_START_PARAGRAPH_\nDin moment ce a\xc8\x99teptarea dup\xc4\x83 o variabil\xc4\x83 condi\xc8\x9bional\xc4\x83 duce la pierderea \xc3\xaencuietorii, cel care a\xc8\x99teapt\xc4\x83 trebuie s\xc4\x83 se asigure ca invariantul monitorului este satisf\xc4\x83cut \xc3\xaenainte ca s\xc4\x83 a\xc8\x99tepte._NEWLINE_\xc3\x8en exemplul de mai sus, acela\xc8\x99i lucru e valabil pentru notificare._NEWLINE_\xc3\x8en primele implement\xc4\x83ri (cunoscute sub numele de semantici Hoare), notificarea unei variabile condi\xc8\x9bionale f\xc4\x83cea ca procesul care a\xc8\x99tepta_NEWLINE_s\xc4\x83 primeasc\xc4\x83 \xc3\xaencuietoarea si s\xc4\x83 ruleze imediat, garant\xc3\xa2nd astfel conti\xc8\x9bia. Implementarea acestui comportament este complicat\xc4\x83_NEWLINE_\xc8\x99i are un cost mare. Este de asemenea incompatibil cu schedulere ce pot \xc3\xaentrerupe un proces \xc3\xaen mod arbitrat._NEWLINE_Din aceste motive, cercet\xc4\x83torii au luat \xc3\xaen considerare alte semantici pentru variabilele condi\xc8\x9bionale._NEWLINE_In most modern implementations (known as Mesa semantics), notifying does not take control away from the running process, but merely makes some waiting process runnable. The notifying process continues to hold the lock until it leaves the monitor function. The side effects of this approach are that the notifying process does not have to set up the monitor invariant before notifying, and the waiting process must double-check the condition it was waiting for. Specifically, if a monitor function includes the expression if test then wait(cv), another process could enter the monitor after the notification and make the condition untrue again before the waiting process runs. The expression must be rewritten as while test do wait(cv) so that the condition is re-checked before the process continues._NEWLINE_Implementations also provide a "notifyAll" or "broadcast" operation that notifies every process waiting on a given condition. This operation is useful, for example, when several processes are waiting for different amounts of storage to become available. Releasing storage can enable any number of these processes to proceed, but the scheduler does not know which ones._NEWLINE_A sample implementation for a condition variable is as follows:_NEWLINE_conditionVariable {_NEWLINE_  int queueSize = 0;_NEWLINE_  semaphore lock;_NEWLINE_  semaphore waiting;_NEWLINE_  _NEWLINE_  wait() {_NEWLINE_     lock.acquire();_NEWLINE_     queueSize++;_NEWLINE_     lock.release();_NEWLINE_     waiting.down();_NEWLINE_  }_NEWLINE_  _NEWLINE_  signal() {_NEWLINE_     lock.acquire();_NEWLINE_     if (queueSize > 0){_NEWLINE_        queueSize--;_NEWLINE_        waiting.up();_NEWLINE_     }_NEWLINE_     lock.release();_NEWLINE_  }_NEWLINE_}'