This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementation of a Semaphore  RSS feed

 
Timothybruce Williams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?


 
Henry Wong
author
Sheriff
Posts: 22831
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!