• 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

Implementation of a Semaphore

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I modified my professor's example implementation of a semaphore so that it prints out what is happening inside the semaphore when you use it.

So with when we use the semaphore it prints

System.out.println("Number of permits is less than 1 in the semaphore being accessed by ThreadA so we will wait");

And so we see what the semaphore is doing when we use it.

I only modified 5 lines I think(by my count, the lines involving mutex2 as well as the System.out.println lines), and yet it only works about half the time. Is it because it is not reusable or something? How can I tell?


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Timothybruce Williams wrote:
I modified my professor's example implementation of a semaphore so that it prints out what is happening inside the semaphore when you use it.

So with when we use the semaphore it prints

System.out.println("Number of permits is less than 1 in the semaphore being accessed by ThreadA so we will wait");

And so we see what the semaphore is doing when we use it.

I only modified 5 lines I think(by my count, the lines involving mutex2 as well as the System.out.println lines), and yet it only works about half the time. Is it because it is not reusable or something? How can I tell?



First, I am going to assume that you understand your professor's implementation -- as I want to make this explanation simpler.

Let's say that thread A wants to acquire() the semaphore, but there isn't enough permits. So, at line 26. it setup and does an await() call. This call does two important things. One, it releases the lock that is referred to by the mutex variable. And two, it waits for a signal before it returns from the await() method call (after reacquiring the lock). Also, note that the lock referred to by mutex2 has NOT been released -- that will happen at line 28, and only after the await() method calls returns... In other words, you now have a state where mutex is released, but mutex2 is still locked.

Some time later, another thread (let's call it thread B) wants to return the permits. It does so by calling the semaphore's release() method. At line 41, it gets the mutex lock (which is currently free). At line 42, it tries to get the mutex2 lock, which is not free. Thread B is now waiting at line 42, until thread A releases the mutex2 lock. Thread A is waiting at the await() call (line 26) waiting for thread B to complete the release (and signal thread A).

This is a deadlock condition.

Henry

 
reply
    Bookmark Topic Watch Topic
  • New Topic