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 3 of 3

Thread: Test and Set Explanation

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

    Default Test and Set Explanation

    For our project we will be implementing semaphore using P() and V() in java. Before I can do that I need to understand what TestandSet and Enabling/Disabling interrupts to. I understand test and set alone without the addition of semaphores but I do not understand it when semaphores are being implemented. My knowledge is with Test and Set with two threads for example you have a global variable occupied and it is true if a thread is in its critical section. The thread can enter its critical section based on its local variable mustWait. Now lets say Thread one wants to enter and mustwait=true then it repeatedly calls testandset(mustwait, occupied) until occupied is false. When the other thread sets occupied to false after leaving its critical section, when Thread one calls testandset(mustwait, occupied), occupied would be set to false which is copied into mustwait allowing thread one to enter its critical section.

    Now with semaphores understand when a thread wants to enter its critical section P(S) is called where
    if S>0
     S=S-1;
    Else
     The thread is placed on the waiting queue.
     
    //when it is done executing it calls V(S) which says:
    If any threads are waiting on S
      Resume with the next waiting thread in the queue
    Else
      S=S+1;

    Can someone explain these two implementations of Semaphores with Test and Set and Enabling/disabling interrupts?

    Test and Set
    typedef struct {
    int count;
    queue q;
    int t;
    } SEMAPHORE;
     
    P(s)
    SEMAPHORE *s;
    {
    Disable interrupts;
    while (TAS(s->t) != 0) /* do nothing */;
    if (s->count > 0) {
    s->count = s->count-1;
    s->t = 0;
    Enable interrupts;
    return;
    }
    Add process to s->q;
    s->t = 0;
    Redispatch;
    }
     
    V(s)
    SEMAPHORE *s;
    {
    Disable interrupts;
    while (TAS(s->t) != 0) /* do nothing */;
    if (s->q empty) {
    s->count += 1;
    } else {
    Remove first process from s->q;
    Wake it up;
    }
    s->t = 0;
    Enable interrupts;
    }

    Enabling/Disabling Interrupts
    Using Enable/Disable Interrupts
     
    class Semaphore { 
    int number = 0; 
    }
     
    Semaphore:() {
    Disable interrupts;
    while (number == 0) {
    Enable interrupts;
    Disable interrupts;
    }
    number = number - 1;
    Enable interrupts;
    }
     
    Semaphore::V() {
    Disable interrupts;
    number++;
    Enable interrupts;
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Test and Set Explanation

    Does you project require the use of all of these extra bells and whistles, or just require the concept of control over multithreading? I ask because java uses a very basic way to help with multithreading: synchronized. This should be sufficient. See
    Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
    Semaphore - JavaWorld

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

    Default Re: Test and Set Explanation

    yes it requires these thats why I need the explanation of what the previous psuedocode is doing. Thank you for the links!

Similar Threads

  1. A simple test for you all
    By Maximillian in forum The Cafe
    Replies: 6
    Last Post: December 2nd, 2009, 04:15 PM
  2. Explanation on Arrays NEEDED
    By lamagiapc in forum Collections and Generics
    Replies: 4
    Last Post: November 7th, 2009, 11:36 PM
  3. Replies: 0
    Last Post: February 3rd, 2009, 01:15 AM