Trouble understanding Semaphores

  • Thread starter Thread starter momentum
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
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?
 
Physics 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   Reactions: 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?
 
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   Reactions: momentum
DrClaude said:
It appears to be missing from the flowchart, but the text mentions
Thanks.