4. Implementation of C. V. synch.cc Condition::Condition(constchar* debugName, Lock* conditionLock){ name = debugName; waitQueue = new List<Semaphore *>;}voidCondition::Wait(Lock *conditionLock) { Semaphore *waiter; ASSERT(conditionLock>IsHeldByCurrentThread());waiter = new Semaphore("condition", 0); waitQueue->Append(waiter); conditionLock->Release(); waiter->P(); conditionLock->Acquire(); deletewaiter; } /*Queuecontainingallocated Semaphoresforeachwaitingthread*/ /* Checksifthecurrentthread has thelock. Creates a semaphore, initially 0. Addsthesemaphoretothequeue. Releasesthelock, and goestosleepuntil someonesends a Signal. Whensomeonesends a Signal, reaquiresthe lock and deletesthesemaphore*/
5. Implementation of C. V. synch.cc voidCondition::Signal(Lock *conditionLock) { Semaphore *waiter; ASSERT(conditionLock->IsHeldByCurrentThread()); if(!waitQueue->IsEmpty()){ waiter = waitQueue->Remove(); waiter->V(); }}voidCondition::Broadcast(Lock *conditionLock) { while(!waitQueue->IsEmpty()){ Signal(conditionLock); }} /*Checksifthethecurrentthread has thelock*/ /*Ifit’s true, removesthelastsemaphorefromthe waitqueue, assignsittothewaitersemaphore, and calls V, towakehim up*/ /* Wake up allthreadswaitingonthiscondition untilthewaitqueueisempty*/
7. Dining philosophers In computer science, the dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.
8.
9. Deadlock Refers to a specific condition when two or more processes are each waiting for the other to release a resource, or more than two processes are waiting for resources in a circular chain.
12. By cyclic turn By Several turns. Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, he gives his token to the next philosopher at his right.
13.
14. By Several turns Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, he gives his token to the next philosopher at his right.