Results 1 to 8 of 8
- 04-10-2012, 12:07 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
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 } } } }
- 04-10-2012, 12:07 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: Java semaphores, a question/bug regarding Signal.
Bug at 128
- 04-10-2012, 01:48 AM #3
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.
- 04-10-2012, 01:57 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: Java semaphores, a question/bug regarding Signal.
I wasn't sure if to put it with beginner or advanced.
- 04-10-2012, 05:02 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: Java semaphores, a question/bug regarding Signal.
Is there a reason why my code works in Dr. Java but not Eclipse?
- 04-10-2012, 01:12 PM #6
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.
- 04-10-2012, 02:07 PM #7
Member
- Join Date
- Mar 2012
- Location
- Novosibirsk, Russia
- Posts
- 13
- Rep Power
- 0
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.
- 04-10-2012, 03:02 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: Java semaphores, a question/bug regarding Signal.
Similar Threads
-
Semaphores
By Ameer3881 in forum New To JavaReplies: 9Last Post: 04-10-2012, 02:39 AM -
Detect USB port signal
By mine0926 in forum Advanced JavaReplies: 4Last Post: 01-28-2011, 05:37 AM -
Signal processing API.
By ivanloes in forum New To JavaReplies: 2Last Post: 12-18-2010, 10:22 AM -
Semaphores
By MuslimCoder in forum Threads and SynchronizationReplies: 0Last Post: 04-15-2010, 05:55 AM -
Waiting on multiple semaphores at the same time
By flok in forum Threads and SynchronizationReplies: 3Last Post: 11-10-2009, 03:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks