To identify the range the possible pid values are:
#define MIN_PID 300
#define MAX_PID 5000
- use data structure (of your choice);
- one strategy is to adopt what Linux has done and use a bitmap (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:
int allocate_ map (void)
- create and initialize a data structure for 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
Add a multithread program that would test previous part of the work. The number of threads are limited (only 100) and each thread will request a pid, sleep for a random period of time, and then release the pod.
This part should test two cases:
That all pids are used and unique.
That pids rotate once you go beyond the maximum amount of available pids.
Then add pthreads to ensure that every pid is safe from race conditions.