• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in Semaphores concept

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends

I am developing a thread program using Semaphores.
But i am new to concept
actually the need is to develop this program

1.A group of M fraternity brothers & sorority sisters is having a party & drinking from a keg that can hold 100 servings of beverage . one unlucky pledge is responsible for replacing the keg each time it empties. When a partier wants to drink, he or she fills a cup from keg, unless it is empty. If the keg is empty , the partier wakes up the pledge and then waits until the pledge has returned with a new keg. The behavior of the partiers and pledge is specified by the following �threads�:


Partier:
loop
tellPledgeIfKegIsEmpty
getServingFromKeg
drink
waitSomeAmountOfTime
end loop

Pledge:
loop
waitForKegToEmpty
getnewKeg
end loop
1. Develop a Java monitor or Ada protected record which implements the actions of a semaphore.
2. Write a solution to the above program using that semaphore implementation for synchronization.Your Java or Ada solution should have a single command-line argument representing the number of partiers.
3. Write a solution to the above program without using the semaphore implementation.This program can use a separate class or protected record for synchronization but it should be one specially developed for the program(rather than a generic semaphore).It should not be exactly equivalent to the solution of 2). Again, use a single command-line argument representing the number of partiers.

i developed the third solution





please help me in compleating this assignment....
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what happens when you run it?
 
sudhasrinivas pallam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends
i tried a semaphore program it is working fine for 1st time but 2nd time it is hanging up


please help me out in fixing the bug......
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to look harder at your code. Make sure it does what the professor is asking.

Your hangup comes from these two lines:


When you first go through the method call, _semaphore is equal to _free. The while loop immediately finished. Then the _semaphore is set to _busy.

The second time through _semaphore has already been set to _busy, so the while loop condition is false. You then don't provide any means for changing the _semaphore from _busy to _free (the _unlock() call is outside the infinite while loop you have in the run method).

Since your entire code is basically running in one Thread, there would be no means for anyone to change that state from _busy to _free.

You will need quite a bit of re-design to get to where you need to be. First, the instructions indicate that a Partier, and the Pledge should each act as a Thread, and their should be a number of Partiers defined by a command line argument. This probably means that EACH partier should be its own thread, and the pledge should be its own thread. You have one thread which does the job of one (very thirsty) partier AND the pledge. This design not only breaks the instructions but also makes it hard to properly use the semaphore.

Try re-making the code where the Partier is a class that implements Thread, and Pledge is a class that implements Thread. Start of the partiers and have the partiers tell the pledge when to start. You will get in to some trouble when you get this far, so when you do, come back and we can help.
reply
    Bookmark Topic Watch Topic
  • New Topic