My program is a PID manager that allocated a unique process identifier to each p
ID: 3822774 • Letter: M
Question
My program is a PID manager that allocated a unique process identifier to each process. It creates a number of threads that requested and released process identifiers.
Modify the code to ensuring that the data structure used to represent the availability of the process identifiers is safe from race conditions. Use Pthreads, mutex locks.
My Code
*************************************************************************************************
package pid;
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);
System.out.println(threadName + " is sleeping");
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
System.out.println("Releasing PID :"+ processId);
releasePID(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[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
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 releasePID(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
*************************************************************************************************
Creating the thread = Process 1
Starting Process 1
Creating the thread = Process 2
Starting Process 2
Running Process 1
Creating the thread = Process 3
Starting Process 3
Running Process 2
Creating the thread = Process 4
Starting Process 4
Running Process 3
Creating the thread = Process 5
Starting Process 5
Running Process 4
Creating the thread = Process 6
Starting Process 6
Creating the thread = Process 7
Starting Process 7
Running Process 5
Creating the thread = Process 8
Starting Process 8
Running Process 7
Creating the thread = Process 9
Starting Process 9
Running Process 6
Running Process 8
Creating the thread = Process 10
Starting Process 10
Running Process 9
Running Process 10
Process 3 is sleeping
Thread Process 3 exiting.
Process 4 is sleeping
Process 1 is sleeping
Thread Process 1 exiting.
Releasing PID :300
Process 2 is sleeping
Thread Process 2 exiting.
Releasing PID :301
Thread Process 4 exiting.
Releasing PID :303
Releasing PID :302
Process 5 is sleeping
Thread Process 5 exiting.
Releasing PID :304
Process 10 is sleeping
Process 8 is sleeping
Process 6 is sleeping
Thread Process 6 exiting.
Releasing PID :306
Thread Process 10 exiting.
Process 9 is sleeping
Thread Process 9 exiting.
Releasing PID :308
Thread Process 8 exiting.
Releasing PID :307
Process 7 is sleeping
Thread Process 7 exiting.
Releasing PID :309
Releasing PID :305
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <windows.h>
using namespace std;
#define MIN_PID 300
#define MAX_PID 5000
int threadVar = 0;
pthread_mutex_t mutex;
struct pid_tab
{
int pid;
bool bitmap;
}pidArr[4700];
int allocate_map(void)
{
int j,i;
for(j = MIN_PID, i =0; j <= MAX_PID; j++, i++)
{
pidArr[i].pid = j;
pidArr[i].bitmap = 0;
}
if(j == MAX_PID && i == 4700)
return 1;
else
return -1;
}
int allocate_pid(void)
{
for(int j = MIN_PID, i =0; j <= MAX_PID; j++, i++)
{
if(pidArr[i].bitmap == 0)
{
pidArr[i].pid = j;
pidArr[j].bitmap = 1;
return j;
break;
}
}
return -1;
}
void release_pid(int pid)
{
for(int j = 0; j <= 4700; j++)
{
if(pidArr[j].pid == pid)
{
pidArr[j].bitmap = 0;
}
}
}
void * threadCall(void* voidA)
{
int ret = allocate_pid();
while (threadVar < 100)
{
pthread_mutex_lock(&mutex);
if (threadVar >= 100)
{
pthread_mutex_unlock(&mutex);
break;
}
threadVar++;
Sleep(100);
cout<<" "<<threadVar;
//cout<<" "<<pidArr[threadVar].pid;
pthread_mutex_unlock(&mutex);
}
Sleep(5);
release_pid(ret);
}
int main()
{
int j =0;
pthread_t thread[100];
cout<<" 100 threads was created. Every and each thread would print the value of variable 'threadVar' and increment it by 1 along with a delay of 100ms each and every process execution";
Sleep(3000);
for(j = 0; j < 100; j++)
{
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread[j], NULL, threadCall, NULL);
threadCall(NULL);
}
for(j = 0; j < 100; j++)
{
pthread_join(thread[j], NULL);
pthread_mutex_destroy(&mutex);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.