Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphore deadlock

So I have a problem using semaphore. Writing a code where are 4 rooms and some visitors. Each room has a certain cap for the amount of visitors they can hold. So entering a full room would trigger a wait(). The visitors must not leave a room before they can enter another, so they are always in a room.

public class Semaphore {

  private int placesLeft;

  public Semaphore(int placesInRoom) {
    this.placesLeft = placesInRoom;
  }

  public synchronized void acquire(Visitor visitor) {
    Semaphore sem = visitor.getRoom().getSemaphore();

    try {
      while (placesLeft <= 0) {
        this.wait();
    }

  } catch (InterruptedException e) {}

  sem.release();
  placesLeft--;
}

public synchronized void release() {
  placesLeft++;
  this.notifyAll();
}

Deadlock appears when 2 people are trying to enter each other's rooms. Also for some reason the placesLeft count is not coming out right.

So what should I do?

EDIT:

Been busy with something else, reviving the question. The problem doesnt occure because of rooms get full, lock occures when person1 from room1 wants to enter room2 and the same time person2 from room2 wants to enter room1. As I understant its something to do with synchrozing maybe? They get stuck before release, so release is not called. As i understand one rooms accuire and release cannot be called same time. So basicly room1 semaphore release cannot be called cuz on same time the accuire is called, same for room2? I'm newbie coder and synchronizing is not so clear yet. Removing synchronizes from one or another doesnt seem to work (is prolly wrong also).

like image 592
user1335170 Avatar asked Apr 15 '12 22:04

user1335170


People also ask

What is a semaphore deadlock?

The implementation of semaphore with a waiting queue may result in a satiation where two more processes are waiting indefinitely for an event that can be caused only by one of the waiting processes. When such a state is reached that process are said to be deadlocked.

Can semaphore lead to deadlock?

Improper use of semaphores with wait queues can cause deadlock. Deadlock means a group of processes are all waiting for each other for some event.

Do semaphores prevent deadlocks?

The first two properties express the basic feature of the semaphore or (mutual exclusive) lock operations. Therefore, one usually prevents deadlock by negating either the Hold and Wait or the Circularity conditions.

Is starvation possible in semaphore?

The simplest source of starvation are weak semaphores. If you are using a synchronization primitive (or building your own) that behaves similarly, then starvation will result.


2 Answers

Instead of implementing your own, how about using java.util.concurrent.Semaphore which is build into the Java Standard Library?

The java.util.concurrent package has a great tutorial covering Semaphores and the many other useful synchronization mechanisms which it provides.

like image 171
ulmangt Avatar answered Oct 18 '22 09:10

ulmangt


Deadlock occur when there is a cycle in the dependency graph. When 2 people are trying to enter each other's rooms, this is evidently a cycle, and deadlock is a natural consequence.

However, you want to treat cycles in other way: when cycle occur, people all move along the cycle (there can be more than 2 people exchanging rooms).

So you should first determine if a cycle is formed, and then change visitors' locations.

like image 37
Alexei Kaigorodov Avatar answered Oct 18 '22 07:10

Alexei Kaigorodov