Results 1 to 8 of 8
  1. #1
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Question Java semaphores, a question/bug regarding Signal.

    Why does my release method for S not work? It allows me to run the function, without the benefit of the signal working. It does not increment the value of S, and doesn't allow the sleeping scheduler to run, even though it signals it right after "Process i is done". The S functions work in other sections of the code, but does not allow me to release in the thread part of the code. I put a comment where I am having the problem in giant text.

    To help understand the program, There are 5 threads being run, semaphore S signals the scheduler, which unblocks the process with the lowest ID. The part where I put i == 1, that just means I am still furbishing the scheduler and how it should run, but I DEFINITELY KNOW, that the first process to arrive has to be serviced (as a rule).


    Java Code:
    import java.io.IOException;
    import java.util.concurrent.Semaphore;
    
    public class Main {
    	public static void main(String[] args) throws IOException {
    	
    		int numberOfProcesses = 5;
    		
    		int N = numberOfProcesses + 1;
    		Process[] p = new Process[N];
    		      for (int i = 0; i < N; i++)
    		      {
    		        p[i] = new Process(i, N);
    		        p[i].start();
    		      }
    
    	}
    
    }
    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Random;
    import java.util.concurrent.Semaphore;
    class Process extends Thread 
    {
    	private static volatile Semaphore[] B;
    	private static volatile Semaphore S;
    	private static volatile int count = 0;
    	private static volatile boolean [] needCriticalSection;
    	ArrayList ProcessWaiting;
    	private int id;
    	private int totalP;
    	private int priority; // Inverts the lower process ID to reflect a higher priority.
    
    	private static Semaphore MutexA;
    	
    	  public Process(int i, int n) {
    		this.id = i;
    		this.totalP = n;
    		MutexA = new Semaphore(1,false);
    		 B = new Semaphore[totalP];
    		needCriticalSection = new boolean [totalP];
    		needCriticalSection[0] = false;
    		S = new Semaphore(0,false);
    		ProcessWaiting = new ArrayList();
    		for(int j = 1; j < totalP; j ++)
    		{
    			B[j] = new Semaphore(0,false);
    			needCriticalSection[j] = false;
    		}
    	}
    
    	
    	  public int sleepRNG(int highpoint) throws InterruptedException
    	  {
    	   Random r = new Random();
    	   int sleeptime=r.nextInt(highpoint);
    	   sleep(sleeptime);
    	   return sleeptime;
    	  }
    
    public void run()
      {
    	
    	if(id == 0) // scheduler
    	{
    		System.out.println("Scheduler ready");
    		while(true)
    		{
    			try {
    				S.acquire();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			System.out.println("Scheduler being used");
    			ProcessWaiting = new ArrayList<Integer>();
    				for(int i = 0; i < needCriticalSection.length; i ++)
    				{
    					if(needCriticalSection[i] == true)
    					{
    						ProcessWaiting.add(i);
    					}
    					
    				}
    			    Object obj = Collections.min(ProcessWaiting);
    			    int higherPriority = (Integer) obj;
    			    B[higherPriority].release();
    		}		
    	}
    	else // processes
    	{
    
    		try 
    		{
    			sleepRNG(50);
    		} 
    		catch (InterruptedException e) 
    		{
    			e.printStackTrace();
    		}
    
    		try
    		{
    		MutexA.acquire();
    		}
    		catch(InterruptedException a)
    		{
    			
    		}
    		count ++;
    		MutexA.release();
    		
    		if(count == 1)	//very first thread runs first
    		{
    			B[id].release();
    			//System.out.println(id);
    
    		}
    
    		System.out.printf("Process %d is REQUESTING access to the CS \n", id);
    		needCriticalSection[id] = true;
    		while(true)
    		{
    			try {
    				B[id].acquire();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			System.out.printf("Process %d is INSIDE the CS \n", id);
    			needCriticalSection[id] = false;
    			
    			try 
    			{
    				sleepRNG(75);
    			} 
    			catch (InterruptedException e) 
    			{
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    
    			System.out.printf("Process %d is FINISHED with the CS \n", id);
    			S.release(); // BUG HERE
    			
    			
    		}
    		
    	}
    
      }
    }

  2. #2
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Java semaphores, a question/bug regarding Signal.

    Bug at 128

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Java semaphores, a question/bug regarding Signal.

    Why is this posted twice in the same forum?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Java semaphores, a question/bug regarding Signal.

    I wasn't sure if to put it with beginner or advanced.

  5. #5
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Java semaphores, a question/bug regarding Signal.

    Is there a reason why my code works in Dr. Java but not Eclipse?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Java semaphores, a question/bug regarding Signal.

    I have no idea about those two programs. I use the java.exe program for my testing.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default Re: Java semaphores, a question/bug regarding Signal.

    Your processes use global data represented with static variables in the class Process. that global data should be initialized once. Instead, they are initialized in every call to the Process constructor. Good programming avoids static variables. Make separate class for the global data, make one object of it, and pass the object to the each new Process.

  8. #8
    Ameer3881 is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Java semaphores, a question/bug regarding Signal.

    Quote Originally Posted by rfq View Post
    Your processes use global data represented with static variables in the class Process. that global data should be initialized once. Instead, they are initialized in every call to the Process constructor. Good programming avoids static variables. Make separate class for the global data, make one object of it, and pass the object to the each new Process.
    Brilliant explanation. Thanks. Our professor taught us in the use of statics :( its all i know. But your explanation makes sense, it actually was the cause of my problem. What i did was, when count == 0 (even before first thread is executed), call constructor, else ------>

Similar Threads

  1. Semaphores
    By Ameer3881 in forum New To Java
    Replies: 9
    Last Post: 04-10-2012, 02:39 AM
  2. Detect USB port signal
    By mine0926 in forum Advanced Java
    Replies: 4
    Last Post: 01-28-2011, 05:37 AM
  3. Signal processing API.
    By ivanloes in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 10:22 AM
  4. Semaphores
    By MuslimCoder in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-15-2010, 05:55 AM
  5. Waiting on multiple semaphores at the same time
    By flok in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-10-2009, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •