<br><br><div class="gmail_quote">On Sat, Sep 17, 2011 at 2:20 PM, Razvan Pistolea <span dir="ltr"><<a href="mailto:razvy000@yahoo.com">razvy000@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font:inherit" valign="top"><br>SemaforGeneral(N){<br><br> c = N // contor; lasa maxim N<br> mutex = SemaforBinar(1) // protejeaza contorul<br>
s2 = SemaforBinar(N>1?1:0) // implementeaza "notificarea"<br> <br> wait()<br> s2.wait()<br> mutex.wait()<br> c--<br> if c > 0<br> s2.release()<br> mutex.release()<br>
<br>
release()<br> mutex.wait()<br> c++<br> if c == 1<br> s2.release()<br> mutex.release()<br>}<br><br>//exemplu:<br>s = SemaforGeneral(2) // c = 2, mutex = 1, s2 = 1<br>proces1: s.wait() // c = 1, mutex = 1, s2 = 0<br>
p2: s.wait() // c = 0, mutex = 1, s2 = 0<br>p3: s.wait() // c = 0, mutex = 1, s2 = 0 p3 blocat in s2<br>p4: s.wait() // c = 0, mutex = 1, s2 = 0 p4 blocat in s2<br>p2: s.release() // c = 1
deblocheaza p4 (p3 sau p4)<br> // p4 se blocheaza (posibil in mutex) dar e deblocat tot de p2<br> // c = 0, mutex = 1, s2 = 0<br>p4: s.release() // c = 1, deblocheaza p3, c = 0, mutex = 1, s2 = 0<br>
p1: s.release()<br>p3: s.release()</td></tr></tbody></table></blockquote></div><br>Ce se intampla daca inainte ca procesul 1 sa faca wait, sa zicem ca procesul 4 face release? Daca face de mai multe ori? c-ul o sa creasca.. acolo ar trebui verificata o limita ceva, totusi, inainte de incrementare...<br>
<br>-- <br>Laura Vasilescu<br>