Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello I need some help with this coding problem written in Java. I am not sure how to create multiple threads and what to do

hello I need some help with this coding problem written in Java. I am not sure how to create multiple threads and what to do within my sleep method to improve my code. This my problem question down below. my code is down below and I really need some help with this homework.

Write a multithreaded program that tests your solution to HW#1. You will create several threads for example, 100 and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process termination).

On UNIX and Linux systems, sleeping is accomplished through the sleep() function, which is passed an integer value representing the number of seconds to sleep.

this is my code I need help with, to create multiple threads and to fix the sleep method.

public class Pid_manager2 {

//declaring minimum pid and maximum pid

private int MIN_PID = 300;

private int MAX_PID = 5000;

//pid map of type boolean this acts like a bitmap

private boolean pid_map[];

public int allocate_map()

{

//find the size of the map that is equal to the max pid - min pid

int size = MAX_PID - MIN_PID;

//allocating memory for the pid map

pid_map = new boolean[size];

//incase the map is null i.e allocation is failed we return -1

if(pid_map == null)

return -1;

//initializing the map with false values

for(int i=0;i

{

pid_map[i] = false;

}

//returning 1

return 1;

}

public int allocate_pid()

{

/*

for allocating a pid we traverse the pid_map from start to end if we find any

free entry i.e having a false value we replace the false value with true and return

that pid

*/

int pid = 0;

int size = MAX_PID - MIN_PID;

//traverse untill we find a false value and pid vale is not equal to size

while( pid!=size && pid_map[pid] != false )

{

pid++;

}

//if the value of pid is equal to the size of the map then all the pid's are occupied

//so we return -1

if(pid == size)

{

return -1;

}

//replace the false value with true

pid_map[pid] = true;

//return the pid + MIN_PID to bring the pid within range of MIN_PID and MAX_PID

return pid+MIN_PID;

}

public void release_pid(int pid)

{

//just replace the false with true in the pid_map for corresponding pid - MIN_PID value

pid_map[pid-MIN_PID] = false;

}

//Test method to test the functionality of the above methods

public static void main(String[] args)

{

int pid;

/* I create a PID object to processed */

Pid_manager2 pm = new Pid_manager2();

if(pm.allocate_map() != -1)

{

pid = pm.allocate_pid();

if(pid!=-1){

System.out.println("Process allocated with PID :"+ pid);

pm.release_pid(pid);

System.out.println("Process with PID :" + pid + " released");

}

else

{

System.out.println("Process cannot be allocated");

}

}

else{

System.out.println("Error in allocating PID map");

}

int sleep(int z){

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What are proxy contests and how are they used?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago