Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please code in C++ or Java An operating system\'s pad manager is responsible for

ID: 3817085 • Letter: P

Question

Please code in C++ or Java

An operating system's pad manager is responsible for managing process identifiers. When a process is first created, it is assigned a unique pad by the pad manager. the pad is returned to the pad manager when the process completes execution, and the manager may later reassign this pad. Process identifiers are discussed more fully in Section 3.3.1. What is most important here is to recognize that process identifiers must be unique; no two active processes can have the same pad. Use the following constants to identify the range of possible pad values: #define MIN_PID 300 #define MAX_PID 5000 You may use any data structure of your choice to represent the availability of process identifiers. One strategy is to adopt what Linux has done and use a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a value of 1 indicates that the process id is currently in use. Implement the following API for obtaining and releasing a pad: int allocate_ map (void)-Creates and initializes a data structure for representing pads; returns-1 if unsuccessful, 1 if successful int allocate_ pid(void)-Allocates and returns a pad; returns-1 if unable to allocate a pad (all pads are in use) void release_pid(int pid)-Releases a pad

Explanation / Answer


public class PidManagerTest {

   public static void main(String[] args) {
       PidManager.allocatMap();
       System.out.println("Allocate PID :"+PidManager.allocatPID());
       System.out.println("Allocate PID :"+PidManager.allocatPID());
       System.out.println("Releasing PID :"+ 300);
       PidManager.releasPID(300);
       System.out.println("Allocate PID :"+PidManager.allocatPID());
   }

}

class PidManager{
   private static final int MIN_PID = 300;
   private static final int MAX_PID = 5000;
   private static int[] pid;
   public static int allocatMap(){
       pid = new int[MAX_PID - MIN_PID];
       if(pid == null) return -1;
       for(int i=0;i<pid.length;i++){
           pid[i] = 0;
       }
       return 1;
   }
   public static int allocatPID(){
       if(pid==null){
           System.out.println("PID Manager is not initialized ");
           return -1;
       }
       int pidNum =-1;
       for(int i=0;i<pid.length;i++){
           if(pid[i]==0){
               pid[i]=1;
               pidNum = i + MIN_PID;
               break;
           }
       }
       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;
   }
}