Trouble understanding Semaphores

  • Thread starter momentum
  • Start date
In summary: I was just wondering if the count was always decreasing and if not, why not?Yes, the count is always decreasing.
  • #1
momentum
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?
 
Technology news on Phys.org
  • #2
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
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?
 
  • #4
momentum said:
Confused.

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

I'm confused by your question. What does that mean?
 
  • #5
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
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.
 
  • #7
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
  • #8
DrClaude said:
It appears to be missing from the flowchart, but the text mentions
Thanks.
 

FAQ: Trouble understanding Semaphores

What are semaphores and why are they used in programming?

Semaphores are synchronization mechanisms used in operating systems and programming languages to control access to shared resources. They are used to prevent multiple processes or threads from accessing a shared resource at the same time, which can lead to data corruption or unexpected behavior.

How do semaphores work?

Semaphores work by maintaining a count that represents the number of processes or threads that can access a shared resource at any given time. When a process or thread wants to access the resource, it must acquire the semaphore. If the count is greater than 0, the semaphore is decremented and the process or thread can access the resource. If the count is 0, the semaphore is in use and the process or thread must wait until it is released by another process.

What is the difference between binary and counting semaphores?

A binary semaphore can only have two states: 0 (in use) or 1 (available). It is typically used to control access to a single resource. A counting semaphore, on the other hand, can have multiple states and is often used to control access to multiple instances of a resource. For example, a counting semaphore with a count of 5 would allow 5 processes to access a resource simultaneously.

How can I avoid deadlocks when using semaphores?

Deadlocks can occur when two or more processes or threads are waiting for a semaphore to be released by another process, resulting in a deadlock. To avoid this, it is important to use semaphores correctly and ensure that they are released after use. Additionally, using timeouts or implementing a resource allocation hierarchy can help prevent deadlocks from occurring.

What are some common mistakes to avoid when using semaphores?

One common mistake when using semaphores is forgetting to release them after use. This can lead to deadlocks or unexpected behavior. Another mistake is using semaphores incorrectly, such as using a counting semaphore to control access to a single resource. It is important to understand how semaphores work and use them appropriately to avoid these mistakes.

Back
Top