Trouble understanding Semaphores

  • Thread starter Thread starter momentum
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding semaphores, particularly focusing on a flowchart that illustrates their operation. Participants are examining the representation of the semaphore counter, questioning the absence of a decrement operation in the flowchart, and discussing the implications of this omission.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions why the flowchart does not include a decrement operation (count--) despite stating that access is denied when the counter is zero.
  • Another participant acknowledges the flowchart's correctness but notes it fails to address the release of the semaphore, which would involve decrementing the counter.
  • There is confusion about whether the count is decremented in the flowchart, with participants seeking clarification on this point.
  • Some participants express concern about the reliability of the source material from GeeksForGeeks, suggesting it may not align with the standards of Physics Forums.
  • A later reply indicates that while the decrement operation is not shown in the flowchart, the accompanying text does mention that the semaphore's count is decremented when a permit is acquired.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether the flowchart is correct, as there is ongoing confusion regarding the representation of the decrement operation. Some participants believe it is a mistake, while others suggest that the text clarifies the operation.

Contextual Notes

The discussion highlights potential limitations in the flowchart's representation of semaphore operations, particularly the absence of a decrement operation, which may lead to misunderstandings about how semaphores function.

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   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?
 
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   Reactions: momentum
DrClaude said:
It appears to be missing from the flowchart, but the text mentions
Thanks.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
15
Views
3K