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() // mia placut mult "little book of semaphores"