Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem with some Java Homegrown Semaphore Code

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with some Java Homegrown Semaphore Code

    Hi Everyone,

    This is an interesting problem that I am not able to find documentation for why this may be the case. I am teaching a course in Operating Systems (a typical junior-level course in CS departments). I asked by students to implement a semaphore class in Java using Peterson's algorithm for synchronization. I was toying around with a solution that I wrote, and noticed that if I take out one of the printf calls that I was using to demonstrate what is happening that sometimes a thread will hang in the P() method of the MySemaphore class. When the printf is in there is no problem, it works properly each time. My first thought was that the printf caused an i/o interrupt that provides an opportunity for the thread to yield and the other thread is able to execute. However, this does not ring true to me. So I am thinking that I missed something, and need another set of eyes on this. Take a look and let me know what you see.

    I uploaded all of the source files here, but this is a copy of the MySemaphore class and someone may be able to spot my problem here. There is a comment before the printf in the while (value < 0) loop of the P(int) method that shows you which line can be commented out to reproduce the hanging, if that line is not commented out it seems to work fine.

    Thanks for your help
    public class MySemaphore {
     
    	private int value;
    	private int favoredThread;
    	private boolean tWantsToEnter[] = new boolean[2];
    	private String name;
     
     
    	public MySemaphore(int v, String n) {
    		value = v;
     
    		// initial state
    		favoredThread = 1;
    		tWantsToEnter[0] = false;
    		tWantsToEnter[1] = false;
    		name = n;
    	}
     
     
    	public void V(int thread) {
    		value++;
    		System.out.printf("Thread %d is in %s.V() value = %d\n", thread, name, value);
    		// signal to waiting threads
    		tWantsToEnter[thread] = false;
    	}
     
     
    	public void P(int thread) {
    		int otherThread = (thread == 0) ? 1 : 0;
     
    		value--;
     
    		System.out.printf("Thread %d is in %s.P() value = %d\n", thread, name, value);
    		tWantsToEnter[thread] = true;
    		favoredThread = otherThread;
     
    		while (tWantsToEnter[otherThread] && favoredThread == otherThread)
    			System.out.printf("Thread %d is busy-waiting\n",thread);
     
    		while (value < 0) { // wait
     
    [B][COLOR="Red"]			// when this line is in, all is well. When it's out. One of the threads hangs here forever!
    [/COLOR][/B]			System.out.printf("Thread %d is busy-waiting value:%d < 0\n",thread, value);
     
     
    			while (tWantsToEnter[otherThread] && favoredThread == otherThread)
    				System.out.printf("Thread %d is busy-waiting\n",thread);
     
    		}
     
     
     
    	}
     
     
    }
    Attached Files Attached Files
    Last edited by helloworld922; March 17th, 2010 at 10:16 PM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with some Java Homegrown Semaphore Code

    It looks like you forgot the line of code at the end of your "critical section" that resets the tWantsToEnter for your current thread. See Wikipedia: Peterson's Algorithm.

    Why having that printf statement can cause it to fail/succeed is a mystery to me, but I think it's just some quirk with the timing and not because of any interrupts.

    As a side note, it might be better to perform any extra calculations after the first while comparison check. I did some testing and it doesn't cause the threads to hang after you've made the above fix, but it can potentially lead to the threads running in the wrong order, especially if the amount of stuff before who's turn it is to execute critical stuff takes different amounts of time to execute.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with some Java Homegrown Semaphore Code

    Thanks for the reply. However, that setting is done in the V() operation that performs the "signal". So if you look at the attachment, you will see that in the SynchronizedBufferPeterson.java file that there is a get() and put() method. These methods use the semaphore:

    So the producer would be:
    valueConsumed.P()
    buffer = nextVal
    valueProduced.V()

    The speed of the threads should not affect the way this works. When the printf is in there the output is different each because the context switching between the threads changes. However, the sum is always equal to 10.

    The "fix" you propose means that the buffer is no longer synchronized, which defeats the purpose and makes the code incorrect in another way.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with some Java Homegrown Semaphore Code

    Ah.. I didn't see the zip I'll give it another look possible some time tonight.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with some Java Homegrown Semaphore Code

    hmm... I ran your code (with the zipped java files this time) with that line commented out and left in. Neither case resulted in a thread left hanging indefinately, but only temporarily as it should be. I also looped the program ~1000 times, and everytime it succeeded in stopping both threads.

    One other change I did make was to decrease the amount of time Producer and Consumer slept before trying to put/get another value (otherwise I'd be waiting for such a long time for the program to finish).

    I'll run the same test again later when I get access to a computer with dual cores (this one only has a single core ).

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with some Java Homegrown Semaphore Code

    Thanks HelloWorld... Thats interesting. What version of Java JVM do you have running?

    I am on a Snow Leopard duocore running:

    java version "1.6.0_17"
    Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
    Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)

    I am just wondering if different versions of java jvm give different results, because I let the code run for hours (while I taught classes LOL) and it was still hanging.

    I appreciate the experiments you have run. I am think i will try it on some of our leopard machines and a linux box also.

    This is very curious

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with some Java Homegrown Semaphore Code

    I am running a Windows dual core system (ran it earlier with a single core windows system, too) with:
    Java SE Runtime Environment (build 1.6.0_18-b07)
    Java Hotspot Client VM (build 16.0-b13, mixed mode, sharing)

    I know there are some discrepancies between running/compiling Java on Mac systems, but I kind of doubt that something like this could be caused by these discrepancies.

Similar Threads

  1. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 09:36 PM
  2. Problem to organize my code in classes
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 21st, 2010, 12:13 PM
  3. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  4. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM
  5. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM