class Semaphore int sem Wait Queue q Semaphore



















































![system DINING_PHILOSOPHERS VAR me: semaphore, initially 1; s[5]: semaphore s[5], initially 0; pflag[5]: {THINK, system DINING_PHILOSOPHERS VAR me: semaphore, initially 1; s[5]: semaphore s[5], initially 0; pflag[5]: {THINK,](https://slidetodoc.com/presentation_image_h2/5c78905419836c97be7d2077e8f69bdf/image-52.jpg)




























- Slides: 80
信号量的实现 class. Semaphore { int sem; Wait. Queue q; } Semaphore: : P() { sem--; if (sem < 0) { Add this thread t to q; block(p); } } Semaphore: : V() { sem++; if (sem<=0) { Remove a thread t from q; wakeup(t); } }
用信号量解决生产者-消费者问题 Class Bounded. Buffer { mutex = new Semaphore(1); full. Buffers = new Semaphore(0); empty. Buffers = new Semaphore(n); } Bounded. Buffer: : Deposit(c) { empty. Buffers->P(); mutex->P(); Add c to the buffer; mutex->V(); full. Buffers->V(); } ■ P、V操作的顺序有影响吗? Bounded. Buffer: : Remove(c) { full. Buffers->P(); mutex->P(); Remove c from buffer; mutex->V(); empty. Buffers->V(); }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ Condition: : Signal(){ } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Condition: : Signal(){ } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; Condition: : Signal(){ } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex Condition: : Signal(){ } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex require(lock); } Condition: : Signal(){ }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex require(lock); } Condition: : Signal(){ if (num. Waiting > 0) { } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex require(lock); } Condition: : Signal(){ if (num. Waiting > 0) { Remove a thread t from q; } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex require(lock); } Condition: : Signal(){ if (num. Waiting > 0) { Remove a thread t from q; wakeup(t); //need mutex } }
条件变量实现 Class Condition { int num. Waiting = 0; Wait. Queue q; } Condition: : Wait(lock){ num. Waiting++; Add this thread t to q; release(lock); schedule(); //need mutex require(lock); } Condition: : Signal(){ if (num. Waiting > 0) { Remove a thread t from q; wakeup(t); //need mutex num. Waiting--; } }
用管程解决生产者-消费者问题 class. Bounded. Buffer { … Lock lock; int count = 0; Condition not. Full, not. Empty; } Bounded. Buffer: : Deposit(c) { Bounded. Buffer: : Remove(c) { Add c to the buffer; count++; } Remove c from buffer; count--; }
用管程解决生产者-消费者问题 class. Bounded. Buffer { … Lock lock; int count = 0; Condition not. Full, not. Empty; } Bounded. Buffer: : Deposit(c) { lock->Acquire(); } Bounded. Buffer: : Remove(c) { lock->Acquire(); Add c to the buffer; count++; Remove c from buffer; count--; lock->Release(); }
用管程解决生产者-消费者问题 class. Bounded. Buffer { … Lock lock; int count = 0; Condition not. Full, not. Empty; } Bounded. Buffer: : Deposit(c) { lock->Acquire(); while (count == n) not. Full. Wait(&lock); Add c to the buffer; count++; Bounded. Buffer: : Remove(c) { lock->Acquire(); Remove c from buffer; count--; not. Full. Signal(); lock->Release(); } }
用管程解决生产者-消费者问题 class. Bounded. Buffer { … Lock lock; int count = 0; Condition not. Full, not. Empty; } Bounded. Buffer: : Deposit(c) { lock->Acquire(); while (count == n) not. Full. Wait(&lock); Add c to the buffer; count++; not. Empty. Signal(); lock->Release(); } Bounded. Buffer: : Remove(c) { lock->Acquire(); while (count == 0) not. Empty. Wait(&lock); Remove c from buffer; count--; not. Full. Signal(); lock->Release(); }
Hansen 管程与 Hoare 管程 Hansen-style : Deposit(){ lock->acquire(); while (count == n) { not. Full. wait(&lock); } Add thing; count++; not. Empty. signal(); lock->release(); } Hoare-style: Deposit(){ lock->acquire(); if (count == n) { not. Full. wait(&lock); } Add thing; count++; not. Empty. signal(); lock->release(); } ■ Hansen管程 条件变量释放仅是 一个提示 需要重新检查条件 ■ 特点 高效 ■ Hoare管程 条件变量释放同时表示放弃 管程访问 释放后条件变量的状态可用 ■ 特点 低效
system DINING_PHILOSOPHERS VAR me: semaphore, initially 1; s[5]: semaphore s[5], initially 0; pflag[5]: {THINK, HUNGRY, EAT}, initially THINK; /* for mutual exclusion */ /* for synchronization */ /* philosopher flag */
procedure philosopher(i) { while TRUE do { THINKING; take_chopsticks(i); EATING; drop_chopsticks(i); } } procedure take_chopsticks(i) { DOWN(me); /* critical section */ pflag[i] : = HUNGRY; test[i]; UP(me); /* end critical section */ DOWN(s[i]) /* Eat if enabled */ } void test(i) /* Let phil[i] eat, if waiting */ procedure drop_chopsticks(int i) { { if ( pflag[i] == HUNGRY DOWN(me); /* critical section */ && pflag[i-1] != EAT test(i-1); /* Let phil. on left eat if possible */ && pflag[i+1] != EAT) test(i+1); /* Let phil. on rght eat if possible */ then { UP(me); /* up critical section */ pflag[i] : = EAT; } UP(s[i]) } }
用信号量解决读者-写者问题 Writer write ; Reader read;
用信号量解决读者-写者问题 Writer P(Write. Mutex); write ; V(Write. Mutex) ; Reader P(Write. Mutex) ; read; V(Write. Mutex) ;
用信号量解决读者-写者问题 Writer P(Write. Mutex); write ; V(Write. Mutex) ; Reader if (Rcount == 0) P(Write. Mutex) ++Rcount; ; read; V(Write. Mutex) ;
用信号量解决读者-写者问题 Writer P(Write. Mutex); write ; V(Write. Mutex) ; Reader if (Rcount == 0)P(Write. Mutex) ++Rcount; ; read; -Rcount; if (Rcount == V(Write. Mutex) 0) ;
用信号量解决读者-写者问题 Writer P(Write. Mutex); write ; V(Write. Mutex) ; Reader P(Count. Mutex); if (Rcount == 0)P(Write. Mutex) ++Rcount; ; V(Count. Mutex); read; -Rcount; if (Rcount == V(Write. Mutex) 0) ;
用信号量解决读者-写者问题 Writer P(Write. Mutex); write ; V(Write. Mutex) ; 此实现中,读者优先 Reader P(Count. Mutex); if (Rcount == 0)P(Write. Mutex) ++Rcount; ; V(Count. Mutex); read; P(Count. Mutex); -Rcount; if (Rcount == V(Write. Mutex) 0) ; V(Count. Mutex)
用管程解决读者-写者问题 ■ 两个基本方法 Database: : Read() { Wait until no writers; read database; check out – wake up waiting writers; } Database: : Write() { Wait until no readers/writers; write database; check out – wake up waiting readers/writers; } ■ 管程的状态变量 AR AW WR WW = = 0; 0; // // # # of of active readers active writers waiting readers waiting writers
用管程解决读者-写者问题 ■ 两个基本方法 Database: : Read() { Wait until no writers; read database; check out – wake up waiting writers; } Database: : Write() { Wait until no readers/writers; write database; check out – wake up waiting readers/writers; } ■ 管程的状态变量 AR = 0; // AW = 0; // WR = 0; // WW = 0; // Lock lock; Condition ok. To. Read; Condition ok. To. Write; # # of of active readers active writers waiting readers waiting writers
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Read() { lock. Acquire(); } lock. Release(); Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Read() { lock. Acquire(); } AR++; lock. Release(); Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Read() { lock. Acquire(); while (? ? ? ) { WR++; ok. To. Read. wait(&lock); WR--; } AR++; lock. Release(); } Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Read() { lock. Acquire(); while ((AW+WW) > 0) { WR++; ok. To. Read. wait(&lock); WR--; } AR++; lock. Release(); } Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); } Private Database: : Start. Read() { lock. Acquire(); while ((AW+WW) > 0) { WR++; ok. To. Read. wait(&lock); WR--; } AR++; lock. Release(); } Private Database: : Done. Read() { lock. Acquire(); AR--; } lock. Release();
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); } Private Database: : Start. Read() { lock. Acquire(); while ((AW+WW) > 0) { WR++; ok. To. Read. wait(&lock); WR--; } AR++; lock. Release(); } Private Database: : Done. Read() { lock. Acquire(); AR--; if (? ? ? ) { ok. To. Write. signal(); } lock. Release(); }
解决方案详情:读者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Read() { //Wait until no writers; Start. Read(); read database; //check out – wake up waiting writers; Done. Read(); } Private Database: : Start. Read() { lock. Acquire(); while ((AW+WW) > 0) { WR++; ok. To. Read. wait(&lock); WR--; } AR++; lock. Release(); } Private Database: : Done. Read() { lock. Acquire(); AR--; if (AR ==0 && WW > 0) { ok. To. Write. signal(); } lock. Release(); }
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Write() { lock. Acquire(); } AW++; lock. Release(); Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); }
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Write() { lock. Acquire(); while (? ? ? ) { WW++; ok. To. Write. wait(&lock); WW--; } AW++; lock. Release(); } Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); }
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Private Database: : Start. Write() { lock. Acquire(); while ((AW+AR) > 0) { WW++; ok. To. Write. wait(&lock); WW--; } AW++; lock. Release(); } Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); }
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); } Private Database: : Start. Write() { lock. Acquire(); while ((AW+AR) > 0) { WW++; ok. To. Write. wait(&lock); WW--; } AW++; lock. Release(); } Private Database: : Done. Write() { lock. Acquire(); AW--; } lock. Release();
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); } Private Database: : Start. Write() { lock. Acquire(); while ((AW+AR) > 0) { WW++; ok. To. Write. wait(&lock); WW--; } AW++; lock. Release(); } Private Database: : Done. Write() { lock. Acquire(); AW--; if (WW > 0) { ok. To. Write. signal(); } } lock. Release();
解决方案详情:写者 AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Lock lock; Condition ok. To. Read; Condition ok. To. Write; Public Database: : Write() { //Wait until no readers/writers; Start. Write(); write database; //check out-wake up waiting readers/writers; Done. Write(); } Private Database: : Start. Write() { lock. Acquire(); while ((AW+AR) > 0) { WW++; ok. To. Write. wait(&lock); WW--; } AW++; lock. Release(); } Private Database: : Done. Write() { lock. Acquire(); AW--; if (WW > 0) { ok. To. Write. signal(); } else if (WR > 0) { ok. To. Read. broadcast(); } lock. Release(); }