Question: My program is a PID manager that allocated a unique process identifier to each process. It creates a number of threads that requested and released

My program is a PID manager that allocated a unique process identifier to each process. It creates a number of threads that requested and released process identifiers.

Modify the code to ensuring that the data structure used to represent the availability of the process identifiers is safe from race conditions. Use Pthreads, mutex locks.

My Code

*************************************************************************************************

package pid;

public class main{ public static void main(String[] args) { PidManager.allocateMap(); // Creating different threads PidManager P1 = new PidManager( "Process 1"); P1.start(); PidManager P2 = new PidManager( "Process 2"); P2.start(); PidManager P3 = new PidManager( "Process 3"); P3.start(); PidManager P4 = new PidManager( "Process 4"); P4.start(); PidManager P5 = new PidManager( "Process 5"); P5.start(); PidManager P6 = new PidManager( "Process 6"); P6.start(); PidManager P7 = new PidManager( "Process 7"); P7.start(); PidManager P8 = new PidManager( "Process 8"); P8.start(); PidManager P9 = new PidManager( "Process 9"); P9.start(); PidManager P10 = new PidManager( "Process 10"); P10.start(); } } class PidManager implements Runnable{ private static final int MIN_PID = 300; private static final int MAX_PID = 5000; private static int[] pid; private Thread t; private String threadName; PidManager(String name) { threadName = name; System.out.println("Creating the thread = " + threadName ); } public void run() { System.out.println("Running " + threadName ); int processId = 0; try { processId = allocatePID(); // Let the thread sleep for a while. Thread.sleep(50); System.out.println(threadName + " is sleeping"); }catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); System.out.println("Releasing PID :"+ processId); releasePID(processId); }

public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } }

public static int allocateMap(){

//Sets range for pid range pid = new int[MAX_PID - MIN_PID];

//returns -1 if pid is unavailable if(pid == null) return -1;

//sets all pid to 0, which indicates the process if of i is available for(int i=0; i pid[i] = 0; }

return 1; }

public static int allocatePID(){

if(pid == null){ System.out.println("PID Manager is not initialized "); return -1; }

//pidNum is used to determine if the pid was allocate int pidNum =-1; //Sets values to 1 to indicate the pid is currently in use. for(int i=0; i

if(pid[i] == 0){ pid[i] = 1;

//Adds value to pidNum to determine for loop was executed. pidNum = i + MIN_PID; break; } }

//If for loop was not executed, prints error if(pidNum == -1){ System.out.println("Unable to allocat PID"); } return pidNum; }

public static void releasePID(int pidNum){

if(pid == null){ System.out.println("PID Manager is not initialized "); return; }

if(pidNum < MIN_PID || pidNum > MAX_PID){ System.out.println("Given PID is out or Range"); }

int newPid = pidNum - MIN_PID;

if(pid[newPid] == 0){ System.out.println("PID"+newPid+" is already released "); return; }

pid[newPid]=0; } }

OutPut

*************************************************************************************************

Creating the thread = Process 1 Starting Process 1 Creating the thread = Process 2 Starting Process 2 Running Process 1 Creating the thread = Process 3 Starting Process 3 Running Process 2 Creating the thread = Process 4 Starting Process 4 Running Process 3 Creating the thread = Process 5 Starting Process 5 Running Process 4 Creating the thread = Process 6 Starting Process 6 Creating the thread = Process 7 Starting Process 7 Running Process 5 Creating the thread = Process 8 Starting Process 8 Running Process 7 Creating the thread = Process 9 Starting Process 9 Running Process 6 Running Process 8 Creating the thread = Process 10 Starting Process 10 Running Process 9 Running Process 10 Process 3 is sleeping Thread Process 3 exiting. Process 4 is sleeping Process 1 is sleeping Thread Process 1 exiting. Releasing PID :300 Process 2 is sleeping Thread Process 2 exiting. Releasing PID :301 Thread Process 4 exiting. Releasing PID :303 Releasing PID :302 Process 5 is sleeping Thread Process 5 exiting. Releasing PID :304 Process 10 is sleeping Process 8 is sleeping Process 6 is sleeping Thread Process 6 exiting. Releasing PID :306 Thread Process 10 exiting. Process 9 is sleeping Thread Process 9 exiting. Releasing PID :308 Thread Process 8 exiting. Releasing PID :307 Process 7 is sleeping Thread Process 7 exiting. Releasing PID :309 Releasing PID :305

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!