Jam1 Jam1 - 2 months ago 7
Java Question

Java Null PointerException. Trying to implement semaphores

I have been working on this code for days. And I am trying to figure out what is the problem. I am implementing semaphores and how they choose between read and write processes, for this case the read

mu
processes are more important and thus have more priority. And I think my
Semaphore
class is giving the problem and not the main method. The exception is at line 29 in the Semaphore class which has this code
for (int i = 0; i < updatedProcessArray.length; i++)


I have random 0's and 1's, the 0's are read processes and the 1's are write processes. The Assumption in my program is that all processes comes at the same time and because the readers
0
always have more priority they will always be processed first. When a processes is finished a -1 will mark their array index. The program loops through the array until every thing in the array is negative -1, well that was the objective.

Semaphore class

class Semaphore
{
public int [] updatedProcessArray; // initializing array for update
public int readcount = 0; // keep track of sharedData process

public int signal; // process signal on 00 for reader, on for 01 writer, 10 wait for reader, 11 wait for writer.
//public int priortyReader = 0; // priorty check for readers, with 1 being priorty
public int processesCompleted = 0;


//Semaphore constructor
Semaphore(int[] processArray)
{
int[] updatedProcessArray = processArray.clone(); // copy array
}

public int signalQueue(int muxtex, int wrt, int index)
{
int mu = muxtex; // assgining all process semaphore
int wr = wrt; // assigning writer semahpore
int currentIndex = index; // index for current process
int priorityIndex = 0; // index for priority
int priorityReader = 0;
int minimumWriterIndex = 0; // index of lowest writer (writer with the highest priority)

//int array[];
// loop through array
for (int i = 0; i < updatedProcessArray.length; i++)
{
// Checking the top priority in the queue

//independent if statement
if (wr == updatedProcessArray[i] && currentIndex == i)
{
if (minimumWriterIndex != 1)
minimumWriterIndex = 1; // record first index with writer
}

if (mu == updatedProcessArray[i] && currentIndex == i)
{
//priorityIndex = 0;
//signal = 00; // the priority has been found, reader.


// As soon as the first priority is found reader priorty is found,
// there is no greater priorty in the queue
//priorityReader = 1; // all reader processes have priority over writer processes
//priorityIndex = i; // recording the priority index
return (updatedProcessArray[i] = -1); // marking off to show that job being processed.


}
else
{
if (mu == updatedProcessArray[i])
// keeping track of readers
priorityReader = 1; // to show all reader processes have priority over writer processes

}
}

if (priorityReader != 1 && minimumWriterIndex == 1)
return (updatedProcessArray[minimumWriterIndex] = -1);

return 0;
}

public int waitQueue(int mutex, int wrt, int index)
{
// declaring variables
int mu = mutex;
int wr = wrt;
int currentIndex = index;

if (readcount < 1)
{
// comparing current index value with semaphore mutex
if (mu == updatedProcessArray[currentIndex])
{
signal = 10;
return signal; //reader process waiting
}
// comparing current index value with semaphore wrt
if (wr == updatedProcessArray[currentIndex])
{
signal = 11; //writer process waiting
return signal;
}
return 0;
}
return 0;
}
// for our purpose Signal method will be used for wait as well
public int signal()
{
if (signal == 00 || signal == 01)
{
readcount++;
return signal;
}


if (signal == 10 || signal == 11)
{
readcount--;
return signal;
}
return 0;
}
public int getProcessesCompleted()
{
int sum = 0;
for (int i = 0; i < updatedProcessArray.length; i++)
{
if (updatedProcessArray[i] == -1)
sum++; // update sum
}
return sum;
}
public int[] updateArray()
{
return updatedProcessArray;
}
}


main method

import java.util.Random;

class SemaphoreTest
{
public static void main(String args[])
{
Random rn = new Random(); // creating a new random object

int processArraySize = rn.nextInt(11); // size of processArray

int[] processArray = new int[processArraySize]; // giving array size

int mutex = 0; // semaphore for readers
int wrt = 1; // semaphore for writers

// Now we will initialize populate processArray
for (int i = 0; i < processArraySize; i++)
{
int x = rn.nextInt(2); // we will denote 0 as reader and 1 as writer
processArray[i] = x; // assigning x to index i

}

Semaphore semaphore = new Semaphore(processArray); // creating a new semaphore object
int signal = 0;

int processCompleted = 0;
while(processCompleted < processArraySize)
{
// return the new and updated processArray each time
// a -1 will be where the processes have been completed
// the process will continue untill all process have been completed only -1's in the array


for (int i = 0; i < processArraySize; i++)
{


semaphore.signalQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
signal = semaphore.signal();
if (signal == 00)
{
System.out.println("reader: using shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 01)
{
System.out.println("writer: using shared data \t index: " + i
+ "\tnumber of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
// wait semaphore
semaphore.waitQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
if (signal == 10)
{
System.out.println("reader: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 10)
{
System.out.println("writer: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
}//end of for loop

// updating process completed
processCompleted = semaphore.getProcessesCompleted();

}//end of while loop

}//end of method
}

Answer

in the constructor Semaphore,You must refer to the class attribute with the keyword this should not create a new array as you work with the array updatedProcessArray

change

Semaphore(int[] processArray)
    {
        int[] updatedProcessArray = processArray.clone(); // copy array     
    }

to

Semaphore(int[] processArray)
    {
        this.updatedProcessArray = processArray.clone(); // copy array     
    }

an additional recommendation could use a switch structure to avoid many if

     switch(signal)
            {
                case 00: System.out.println("reader: using shared data \t index: " + i 
                  + "number of processe(s) completed: " + semaphore.getProcessesCompleted() );break;
                case 01:instruction;break;
                case 02: instruction;break;
                default: not option
            }