Trouble understanding Semaphores

  • Thread starter Thread starter momentum
  • Start date Start date
AI Thread Summary
The discussion centers around understanding the concept of semaphores, particularly in relation to a flow diagram that illustrates their operation. A semaphore uses a counter to control access to a shared resource, allowing access when the counter is greater than zero and denying it when the counter is zero. The confusion arises from the flow diagram, which shows only the increment operation (count++) and the condition for access (count>0), but does not depict the decrement operation (count--) that occurs when a thread releases the semaphore. Participants note that while the flowchart may be incomplete, the accompanying text clarifies that the counter is decremented when a thread acquires a permit. The discussion highlights the importance of both acquiring and releasing semaphores in managing access to resources, and acknowledges that the flowchart may not fully represent this process.
momentum
Messages
111
Reaction score
0
I am trying to understand Semaphore. It says

A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied.

Source: https://www.geeksforgeeks.org/semaphore-in-java/

My Question:

Why there is no count-- in the flow diagram?

I only see count++ & count>0 .in the flow diagram? how count will be decremented then?

Is this flow diagram correct?
 
Technology news on Phys.org
It’s correct as far as it goes but it doesn’t cover the case of the thread releasing the semaphore.

In contrast, the code calls sem.acquire to get permission and then sem.release when the work is done. These two methods control the semaphore counter mentioned in the thread.

The usual case is to allow one thread only to access the resource. However, there may be a use case for n threads to run and no more than n threads perhaps to keep a system enduser responsive so we would throttle the number of compute threads.
 
Last edited by a moderator:
  • Like
Likes harborsparrow
jedishrfu said:
It’s correct as far as it goes but it doesn’t cover the case of the thread releasing the semaphore.

In contrast, the code calls sem.acquire to get permission and then sem.release when the work is done. These two methods control the semaphore counter mentioned in the thread.

The usual case is to allow one thread only to access the resource. However, there may be a use case for n threads to run and no more than n threads perhaps to keep a system enduser responsive so we would throttle the number of compute threads.

Confused.

Did you mean count-- happens or not ? Yes/No?
 
momentum said:
Confused.

Did you mean count-- happens or not ? Yes/No?

I'm confused by your question. What does that mean?
 
okay ...It says "..If the counter is greater than zero, then access is allowed. If it is zero, then access is denied..."

in this flowchart diagram Source: https://www.geeksforgeeks.org/semaphore-in-java/

they have count++ which is increasing.

But there is no decreasing count -- in the flowchart elsewhere which could make that to zero?
why there is no count -- in the flowchart? Is it a mistake in the flowchart? This is my concern.
 
momentum said:
Is it a mistake in the flowchart? This is my concern.
This question was completely answered in #2. I am not sure why you are asking here about something posted on another site; why don't you use the feedback on that site? I have seen a lot of rubbish on GeeksForGeeks, and I don't think it is part of PhysicsForums mission to deal with that.
 
momentum said:
okay ...It says "..If the counter is greater than zero, then access is allowed. If it is zero, then access is denied..."

in this flowchart diagram Source: https://www.geeksforgeeks.org/semaphore-in-java/

they have count++ which is increasing.

But there is no decreasing count -- in the flowchart elsewhere which could make that to zero?
why there is no count -- in the flowchart? Is it a mistake in the flowchart? This is my concern.
It appears to be missing from the flowchart, but the text mentions
https://www.geeksforgeeks.org/semaphore-in-java/ said:
  • If the semaphore’s count is greater than zero, then the thread acquires a permit, which causes the semaphore’s count to be decremented.
 
  • Like
Likes momentum
DrClaude said:
It appears to be missing from the flowchart, but the text mentions
Thanks.
 
Back
Top