Вопрос по – Разница между счетными и двоичными семафорами

26

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededsemaphore == 0Error: User Rate Limit Exceeded Dev Null

Ваш Ответ

3   ответа
30

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

EDIT
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Umer Farooq
Error: User Rate Limit Exceeded Umer Farooq
Error: User Rate Limit Exceeded Umer Farooq
Error: User Rate Limit Exceeded
1

Error: User Rate Limit Exceeded

  1. Binary semaphore cannot handle Bounded wait as its just a variable that hold binary value. Counting semaphore It can handle bounded wait as it has converted a variable into a structure with a Queue.
  2. Strcuture implementation Binary semaphore: int s;

    Counting Semaphore: Struct S { int s; Queue q; }

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
9

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

thread A{
semaphore &s; //locks/semaphores are passed by reference! think about why this is so.
A(semaphore &s): s(s){} //constructor
foo(){
...
s.P();
;// some block of code B2
...
}

//thread B{
semaphore &s;
B(semaphore &s): s(s){} //constructor
foo(){
...
...
// some block of code B1
s.V();
..
}

main(){
semaphore s(0); // we start the semaphore at 0 (closed)
A a(s);
B b(s);
}

Error: User Rate Limit ExceededafterError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

thread mutual_ex{
semaphore &s;
mutual_ex(semaphore &s): s(s){} //constructor
foo(){
...
s.P();
//critical section
s.V();
...
...
s.P();
//critical section
s.V();
...

}

main(){
semaphore s(1);
mutual_ex m1(s);
mutual_ex m2(s);
}

Error: User Rate Limit ExceededhintError: User Rate Limit ExceededoneError: User Rate Limit Exceeded

Error: User Rate Limit Exceededmore than one valueError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

thread t1{
...
s.P();
//block of code B1

thread t2{
...
s.P();
//block of code B2

thread t3{
...
//block of code B3
s.V();
s.V();
}

So if your semaphore starts off closed, you ensure that t1 and t2 block, get added to the semaphore's list. Then along comes all important t3, finishes its business and frees t1 and t2. What order are they freed in? Depends on the implementation of the semaphore's list. Could be FIFO, could be based some particular priority,etc. (NoteError: User Rate Limit Exceeded

(Find outError: User Rate Limit Exceeded

Error: User Rate Limit Exceededmakes you understand things better!Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededHintError: User Rate Limit ExceededoneError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы