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

Thread: Implementing Semaphores

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

    Default Implementing Semaphores

    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
    Junior Member
    Join Date
    Mar 2010
    Location
    Home-Ujjain /Job-Mumbai
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Re: Implementing Semaphores

    Many processors have an atomic test-and-set machine language instruction. Those that do not can still implement an atomic test-and-set using an atomic swap (as shown below) or some other atomic read-modify-write instruction.

     #define LOCKED 1
     int TestAndSet(int* lockPtr) {
         int oldValue;
         oldValue = SwapAtomic(lockPtr, LOCKED);
         return oldValue == LOCKED;
     }

    where SwapAtomic atomically first reads the current value pointed to by lockPtr and then writes 1 to the location. Being atomic, SwapAtomic never uses cached values and always commits to the shared memory store (RAM).

    for Enabling and disabling interrupts you will have to study c in deep at hardware level.

    for more please visit any other C++ forum.
    Programmer

Similar Threads

  1. implementing interface from encrypted jar
    By mark111 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2010, 02:38 PM
  2. Help needed regarding implementing RSS feed using JSP
    By smsouvik_2008 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 21st, 2010, 04:34 AM
  3. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 08:44 PM
  4. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM
  5. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM