operating system\'s pid manager is responsible for managing process identifiers.
ID: 3593652 • Letter: O
Question
operating system's pid manager is responsible for managing process identifiers. When a process is first created, it is assigned a unique pi 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. 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 pid Use the following constants to identify the range of possible pid values #define MIN-PID 300 #define MAX-PID 5000 You may use any data structure of your choice to represent the avail ability 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 pid 1nt allocate.nap(void)-Creates and initializes adata 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 · · .Explanation / Answer
int allocate_map(void){
//This would create an array of size of MAX_PID - MIN_PID and we would
//initialize each value to 0 indicating it is not taken yet.
int size = MAX_PID - MIN_PID;
int processArray[size];
for (int i= 0 ;i < size ; i++){
processArray[i] = 0;
}
return 1;
}
int allocate_pid(void){
//We would start from the start in the array and check whether it is already assigned.
//if not, we would return the the index adding MIN_PID as the offset.
int size = MAX_PID - MIN_PID;
//processArray[] is the pid data structure initialized by the allocate_map method
for (int i= 0 ;i < size ; i++){
if(processArray[i] == 0){
//This pid is available. Return.
//Adding MIN_PID, since our array indexes are with a negative offset MIN_PID
//update the value at this position as used
processArray[i] = 1;
return i + MIN_PID;
}
}
//If the control reaches here, that means there are no available pids, return 1;
return 1;
}
void realease_pid(int pid){
//We just need to update the value at the given pid index to the 0, so that it can be used again.
//We are subtracting the offset since we added it while returning the pid.
int index = pid - MIN_PID;
//Update the value to 0
processArray[i] = 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.