27
Solution With Semaphore Prepared By : 14pgit005 (Neha Jobanputra) 14pgit010 (Krupa Patel) 14pgit020 (Arpita Sanghani)

Solution With Semaphore

Embed Size (px)

DESCRIPTION

ppt for problem solved by semaphore

Citation preview

Solution With Semaphore

Solution With SemaphorePrepared By :

14pgit005 (Neha Jobanputra)

14pgit010 (Krupa Patel)

14pgit020 (Arpita Sanghani)4/6/2015 7:23 PM 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

1Producer-consumer problemChefs cook itemsandputthemonaconveyerbeltWaiters pick items off the beltProducer-consumer problemNowimaginemanychefs!...and many waiters!Producer-consumer problemApotentialmess!Producer-consumer problem

Chef (Producer)Waiter (Consumer)insertsitemsremovesitems Efficient implementation we can use circular fixed-size buffer consider buffer have n slots.

Shared resource:bounded bufferShared buerChef(Producer)Waiter(Consumer)insertPtrremovePtrWhat does thechef do with a new pizza?Where does thewaiter take a pizza from?iShared buerChef(Producer)Waiter(Consumer)nsertPtrinsertPtrremovePtrInsert pizzaShared buerChef(Producer)Waiter(Consumer)insertPtrremovePtrInsert pizzaShared buerChef(Producer)Waiter(Consumer)insertPtrremovePtrInsert pizzaShared buerChef(Producer)Waiter(Consumer)insertPtrremovePtrRemove pizzaremovePtrShared buerChef(Producer)Waiter(Consumer)insertPtrInsert pizzaremovePtrShared buerChef(Producer)Waiter(Consumer)insertPtrInsert pizzaremovePtrShared buerChef (Producer)Waiter(Consumer)BUFFER FULL:Producer must wait!Insert pizzainsertPtrremovePtrShared buerChef(Producer)Waiter(Consumer)Remove pizzainsertPtrremovePtrShared buerChef(Producer)Waiter(Consumer)removePtrRemove pizzainsertPtrShared buerChef(Producer)Waiter(Consumer)removePtrRemove pizzainsertPtrShared buerChef(Producer)Waiter(Consumer)removePtrRemove pizzainsertPtrShared buerChef(Producer)Waiter(Consumer)removePtrRemove pizzainsertPtrShared buerChef(Producer)Waiter(Consumer)removePtrRemovepizzainsertPtrShared buerChef(Producer)Waiter(Consumer)removePtrRemove pizzainsertPtrShared buerChef (Producer)Waiter(Consumer)Buffer empty:Consumerblocked!mustberemovePtrRemove pizzainsertPtrDesigning a solution

Chef (Producer)Waiter (Consumer)Wait for emptyInsert itemslotWait for item arrivalRemove itemSignal empty slot availableSignal item arrivalWhat Synchronization do we need?Answer is we required three semaphore.1) Mutex :- to control use of buffer2) To block the producers if buffer is full3) To block the consumer if buffer is empty

DesigningChef (Producer)asolutionWaiter (Consumer)Wait for emptyInsert itemslotWait for item arrivalRemove itemSignal empty slot availableSignal item arrivalWhatsynchronizationdo we need?Mutex(shared buffer)DesigningChef (Producer)asolutionWaiter (Consumer)Wait for emptyInsert itemslotWait for item arrivalRemove itemSignal empty slot availableSignal item arrivalWhatsynchronizationdo we need?Semaphore(# empty slots)DesigningChef (Producer)asolutionWaiter (Consumer)Wait for emptyInsert itemslotWait for item arrivalRemove itemSignal empty slot availableSignal item arrivalWhatsynchronizationdo we need?Semaphore(# filled slots)Producer-Consumer CodeCritical Section: moveinsert pointerCritical Section: moveremove pointerbuffer[ insertPtrdata;]=result =buffer[removePtr];insertPtr+ 1) % N;= (insertPtrremovePtr = (removePtr+1) % N;Producer-Consumer CodeCounting semaphore check and decrement the number of free slotssem_wait(&slots);mutex_lock(&mutex);buffer[ insertPtr ]data;Counting semaphore check and decrement the number of available itemssem_wait(&items);mutex_lock(&mutex)result = buffer[removePtr];Block ifthere are no items totakeBlock ifthere are no free slots=insertPtr = (insertPtr + 1) % N;mutex_unlock(&mutex);sem_post(&items);Done increment the number of available itemsremovePtr = (removePtr+1) % N;mutex_unlock(&mutex);sem_post(&slots);Done increment the number of free slots