I've recently finished a project for an Operating Systems course, but on one specific part of the project I was wondering if I made the best decision. In this project, we need to design a PID Manager.
The purpose behind a PID Manager is to assign a unique Process Identifier (PID) to a process when it is first created. When the process completes execution, the PID is returned to the PID Manager so this same PID can be reassigned to a later process. No two processes can be assigned the same PID.
The following is the interface we needed to implement:
public interface PIDManager
{
public static final int MIN_PID = 4;
public static final int MAX_PID = 127;
public int getPID();
public int getPIDWait();
public void releasePID(int pid);
}
So getPID() simply returns a unique PID, but returns -1 if no PIDs are available. getPIDWait() returns a unique PID, but blocks the calling process until a PID becomes available. releasePID() releases the PID so that it can become available to another process that needs to use it.
We need some type of data structure to keep track of the assigned PIDs. The problem description says that this data structure needs to be free of race conditions and deadlock. For example, the same PID could be concurrently assigned to more than one process, which is something that we would not want.
So I recently finished this project. I chose to use an ArrayList to keep track of the assigned PIDs. I chose an ArrayList because the fact that it grows and shrinks in size provided a lot of convenience, making it easy to add or remove PIDs from the ArrayList and to check to see if a PID is contained within it. But now I'm not sure if an ArrayList could cause race conditions or deadlock. Would it work to use an ArrayList for this type of situation? If you would like, I can provide my implementation.