9 Replies - 358 Views - Last Post: 29 October 2012 - 02:03 PM Rate Topic: -----

#1 Jimmyboy12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

Why is my program dead locking?

Posted 28 October 2012 - 07:56 PM

Hello. I had a question on a problem in my book. How does this program deadlock? Im new to deadlocking never seen it before actually. Everything I find on line is either to general or to difficult to understand. How would you go about preventing a deadlock?

My results gets s2 then s1 then on other runs I get s1 and s2 and etc. Im guessing since it implements runnable... threading for each function is going to be random and locks up the system because the other function occurs??

import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *Jimmy Smith 10/28/2012
 */
public class Deadlocks {

    Semaphore _s1 = new Semaphore(1);
    Semaphore _s2 = new Semaphore(1);

    
    public void case1(int i) {

        // constructing threads
        Thread t1 = new Thread(new P1());
        Thread t2 = new Thread(new P2());
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Deadlocks.class.getName()).log(Level.SEVERE, "unable to join threads", ex);
        }

        System.out.println ("Completing test case 1 run " + i);
    }

   
    public static void main(String args[]) {
        Deadlocks testing = new Deadlocks();

        // run test case multiple times
        for (int i=0; i < 100; i++)
            testing.case1(i);
    }

    /**
     * Models a process P1 performing some computation.
     */
    class P1 implements Runnable {
        public void run() {
            try {
                _s1.acquire();
            } catch (InterruptedException ex) {
                Logger.getLogger(Deadlocks.class.getName()).log(Level.SEVERE, "unable to acquire lock of s1", ex);
            }
            System.out.println ("Acquired lock on s1");

            try {
                _s2.acquire();
            } catch (InterruptedException ex) {
                Logger.getLogger(Deadlocks.class.getName()).log(Level.SEVERE, "unable to acquire lock of s2", ex);
            }
            System.out.println ("Acquired lock on s2");

            // critical section
            // ....

            _s2.release();
            _s1.release();

            System.out.println ("Released all locks.");
        }
    }

     /**
     * Models a process P1 performing some computation.
     */
    class P2 implements Runnable {
        public void run() {
            try {
                _s2.acquire();
            } catch (InterruptedException ex) {
                Logger.getLogger(Deadlocks.class.getName()).log(Level.SEVERE, "unable to acquire lock of s2", ex);
            }
            System.out.println ("Acquired lock on s2");

            try {
                _s1.acquire();
            } catch (InterruptedException ex) {
                Logger.getLogger(Deadlocks.class.getName()).log(Level.SEVERE, "unable to acquire lock of s1", ex);
            }
            System.out.println ("Acquired lock on s1");

            // critical section
            // ....

            _s1.release();
            _s2.release();

            System.out.println ("Released all locks.");
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my program dead locking?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Why is my program dead locking?

Posted 29 October 2012 - 05:20 AM

Here's a pretty good description of why deadlocking occurs.

You'll see varying results from run to run because the running of multiple threads cannot be reliably predicted. The order in which they are scheduled and the processor time given to each as they are run depends on many variables.

You can add some print statements to your code to observe the wide range of variations that can occur. I was wondering how many test cases actually ran before deadlock, so I added a print statement to show which test case was running. I also modified the existing print statements in P1 and P2 to see which was locking, releasing, etc.

On my system (4 cores acting as 8), deadlock usually occurs on the first test case with both P1 and P2 locking a resource the other would need. Which ran first varied, though P1 seemed to run first more often than P2. On a single run out of several, 4 test cases completed which required each thread to release locks before the other tried to acquire locks.

Here's an example output from one run that was able to run 2 test cases:
Testing case 0.
P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.
Completing test case 1 run 0
Testing case 1.
P1 Acquired lock on s1
P2 Acquired lock on s2

Was This Post Helpful? 0
  • +
  • -

#3 Jimmyboy12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

Re: Why is my program dead locking?

Posted 29 October 2012 - 12:30 PM

View PostGregBrannon, on 29 October 2012 - 05:20 AM, said:

Here's a pretty good description of why deadlocking occurs.

You'll see varying results from run to run because the running of multiple threads cannot be reliably predicted. The order in which they are scheduled and the processor time given to each as they are run depends on many variables.

You can add some print statements to your code to observe the wide range of variations that can occur. I was wondering how many test cases actually ran before deadlock, so I added a print statement to show which test case was running. I also modified the existing print statements in P1 and P2 to see which was locking, releasing, etc.

On my system (4 cores acting as 8), deadlock usually occurs on the first test case with both P1 and P2 locking a resource the other would need. Which ran first varied, though P1 seemed to run first more often than P2. On a single run out of several, 4 test cases completed which required each thread to release locks before the other tried to acquire locks.

Here's an example output from one run that was able to run 2 test cases:
Testing case 0.
P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.
Completing test case 1 run 0
Testing case 1.
P1 Acquired lock on s1
P2 Acquired lock on s2


Oh cool that is helpful. But Did you edit the code for it not to deadlock? My keeps stopping at.

run:
P2 Acquired lock on s2
P1 Acquired lock on s1
BUILD STOPPED (total time: 1 second)
Was This Post Helpful? 0
  • +
  • -

#4 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: Why is my program dead locking?

Posted 29 October 2012 - 12:53 PM

My suggestion is scrap that part and redo it in a different way.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Why is my program dead locking?

Posted 29 October 2012 - 01:00 PM

I thought the purpose of the program was to demonstrate deadlocking. If not just a deadlocking demonstration, what is it supposed to do?
Was This Post Helpful? 0
  • +
  • -

#6 Jimmyboy12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

Re: Why is my program dead locking?

Posted 29 October 2012 - 01:04 PM

View PostGregBrannon, on 29 October 2012 - 01:00 PM, said:

I thought the purpose of the program was to demonstrate deadlocking. If not just a deadlocking demonstration, what is it supposed to do?


Yes that is true. But part B ask me to implement a solution that prevents the deadlock and test it with the code. Hoq did you get so many results. My always stopped there.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Why is my program dead locking?

Posted 29 October 2012 - 01:17 PM

Sure that if you have a thread thaht
_s1.acquire()
_s2.acquire()
and another that
_s2.acquire()
_s1.acquire()

you are looking for problem. The first rule of reserving resources in a computer environment (tape drive, rows in a database, ...) is to always reserve them in the same order
Was This Post Helpful? 0
  • +
  • -

#8 Jimmyboy12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 12

Re: Why is my program dead locking?

Posted 29 October 2012 - 01:35 PM

View Postpbl, on 29 October 2012 - 01:17 PM, said:

Sure that if you have a thread thaht
_s1.acquire()
_s2.acquire()
and another that
_s2.acquire()
_s1.acquire()

you are looking for problem. The first rule of reserving resources in a computer environment (tape drive, rows in a database, ...) is to always reserve them in the same order

Ohh I see. So basically change s2 and s1 to s1 and s2 and it relases all my locks. And yes it does look like P1 likes to executes first. Is that Right? My results.

Completing test case 1 run 35
P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.
Completing test case 1 run 36
P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.
Completing test case 1 run 37
P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Why is my program dead locking?

Posted 29 October 2012 - 01:45 PM

Did you forget to change your print ? Because

P1 Acquired lock on s1
P1 Acquired lock on s2
P1 Released all locks.
P2 Acquired lock on s2
P2 Acquired lock on s1
P2 Released all locks.

is surely not good

All thread accessing S1 and S2 have to do it in the same order if they don't

P1 can hold S1
P2 can hold S2

and then P1 will wait for P2 to release S2 and P2 will wait for P1 to release S1 and you are in a dead lock
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Why is my program dead locking?

Posted 29 October 2012 - 02:03 PM

Modifying the code according to pbl's advice you should be able to get the program to run to completion without deadlocking. Were you able? If not, post your latest code.

If you were able, try sleeping one of the threads ~0.25 second to see what happens. It's instructive because it slows the execution down enough to "see" what's going on. By sleeping, I mean add:

Thread.sleep( 250 );

to either P1 or P2 in the "critical section" to simulate some calculation that may be occurring there.

This post has been edited by GregBrannon: 29 October 2012 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1