My code is a PID Manager. When a process is first created, it is assigned a uniq
ID: 3831892 • Letter: M
Question
My code is a PID Manager. When a process is first created, it is assigned a unique PID by the PID manager. The PID is returned to the PID manager when the process completes execution, and the manager may later reassign this PID. PIDs must be unique; no two active PIDs have the same PID.
int allocate_map(void) - Creates and initializes a data structure for representing PIDs; returns -1 if unsuccessful, 1 if successful
int allocate_pid(void) - Allocates and returns a PID; returns -1 if unable to allocate a PID (all PIDs are in use)
void release_pid(int pid) - Releases a PID
Modify the PID Manager where it consist of writing a multithreaded program that tests the program. Create a number of threads-for example, 100- and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process' termination). On UNIX and Linux systems, sleeping is accomplished through the sleep() function, which is passes an integer value representing the number of seconds to sleep.package pid.
My code so far.
****************************************************************************************
public class main {
public static void main(String[] args) {
PidManager.allocateMap();
System.out.println("Allocate PID :"+PidManager.allocatePID());
System.out.println("Allocate PID :"+PidManager.allocatePID());
System.out.println("Releasing PID :"+ 300);
PidManager.releasPID(300);
System.out.println("Allocate PID :"+PidManager.allocatePID());
}
}
class PidManager{
private static final int MIN_PID = 300;
private static final int MAX_PID = 5000;
private static int[] pid;
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.length; 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<pid.length; 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 releasPID(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
Allocate PID :300
Allocate PID :301
Releasing PID :300
Allocate PID :300
Explanation / Answer
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);
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
System.out.println("Releasing PID :"+ processId);
releasPID(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.length; 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<pid.length; 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 releasPID(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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.