[so] so Digest, Vol 65, Issue 5

Laura Vasilescu vasilescu.laura at gmail.com
Tue Sep 20 13:21:12 EEST 2011


On Sat, Sep 17, 2011 at 2:20 PM, Razvan Pistolea <razvy000 at yahoo.com> wrote:

>
> SemaforGeneral(N){
>
>     c = N                        // contor; lasa maxim N
>     mutex = SemaforBinar(1)        // protejeaza contorul
>     s2 = SemaforBinar(N>1?1:0)    // implementeaza "notificarea"
>
>     wait()
>         s2.wait()
>         mutex.wait()
>         c--
>         if c > 0
>             s2.release()
>         mutex.release()
>
>     release()
>         mutex.wait()
>         c++
>         if c == 1
>             s2.release()
>         mutex.release()
> }
>
> //exemplu:
> s = SemaforGeneral(2)    // c = 2, mutex = 1, s2 = 1
> proces1: s.wait()        // c = 1, mutex = 1, s2 = 0
> p2: s.wait()            // c = 0, mutex = 1, s2 = 0
> p3: s.wait()            // c = 0, mutex = 1, s2 = 0        p3 blocat in s2
> p4: s.wait()            // c = 0, mutex = 1, s2 = 0        p4 blocat in s2
> p2: s.release()            // c = 1 deblocheaza p4 (p3 sau p4)
>                         // p4 se blocheaza (posibil in mutex) dar e
> deblocat tot de p2
>                         // c = 0, mutex = 1, s2 = 0
> p4: s.release()            // c = 1, deblocheaza p3, c = 0, mutex = 1, s2 =
> 0
> p1:    s.release()
> p3: s.release()


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...

-- 
Laura Vasilescu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cursuri.cs.pub.ro/pipermail/so/attachments/20110920/492b40a5/attachment.htm>


More information about the so mailing list