Trouble understanding Semaphores

  • Thread starter momentum
  • Start date
  • #1
111
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?
 

Answers and Replies

  • #2
12,526
6,318
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
  • #3
111
0
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?
 
  • #4
anorlunda
Staff Emeritus
Insights Author
9,156
6,154
Confused.

Did you mean count-- happens or not ? Yes/No?
I'm confused by your question. What does that mean?
 
  • #5
111
0
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.
 
  • #6
pbuk
Science Advisor
Gold Member
2,050
820
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.
 
  • #7
DrClaude
Mentor
7,555
3,894
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
  • #8
111
0
It appears to be missing from the flowchart, but the text mentions
Thanks.
 

Related Threads on Trouble understanding Semaphores

  • Last Post
Replies
3
Views
6K
Replies
4
Views
2K
  • Last Post
Replies
4
Views
971
  • Last Post
Replies
1
Views
1K
  • Last Post
2
Replies
31
Views
3K
Replies
5
Views
2K
  • Last Post
Replies
4
Views
2K
Replies
17
Views
4K
  • Last Post
Replies
5
Views
4K
Top