Question

1. The first task in this assignment creates the pid manager whose implementation can simply be...

1. The first task in this assignment creates the pid manager whose implementation can simply be a single class. Of course, you can create any other classes you might need to implement the pid manager. You may use any data structure of your choice to represent the availability of process identifiers. One strategy adopts Linux’s approach of 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. Use the following constants to identify the range of possible pid values: MIN_PID is 300 and MAX_PID is 5000. Have your pid manager implement the following API for obtaining and releasing a pid:

 int allocateMap( ) – Creates and initializes a data structure for representing pids; returning 0 if unsuccessful or 1 if successful.

 int allocatePid( ) – Allocates and returns a pid; returns 1 if unable to allocate a pid, all pids are in use.

 void releasePid(int pid) – Releases a pid.

2. The second task in this assignment consists of writing a multithreaded program that tests your pid manager. Implementing the threads uses either extends Thread or implements Runnable. Create a number of threads where each thread requests a pid, sleeps for a random period of time, releases the pid, and then terminates. Sleeping for a random period of time approximates the typical pid usage in which a new process acquires a pid, the process executes and then terminates, releasing the pid upon its termination. Download the SleepUtilities.java file needed for the assignment. A thread sleeps by calling the SleepUtilities.nap(duration) function, passing an integer value representing the number of seconds to sleep, where duration is a randomly generated integer between 60 and 300. Before sleeping, each thread should print out (on a new line) the message, “My PID is: x.”, where x is the actual pid for the thread.

//SleepUtilities.java

public class SleepUtilities
{
  private static final int NAP_TIME = 5;

  // Nap between zero and NAP_TIME seconds.
  public static void nap()
  {
    nap(NAP_TIME);
  }

  // Nap between zero and duration seconds.
  public static void nap(int duration)
  {
    int sleeptime = (int) (duration * Math.random());

    try
    {
     Thread.sleep(sleeptime * 1000);
    }
    catch (InterruptedException e) { }
  }
}

3. Create a driver class and make the name of the driver class Assignment1 containing only one method: public static void main(String args[]). The main method itself is fairly short containing code to do the following:

a. Create the pid manager object.

b. Create 100 threads.

c. For each thread, pass the pid manager into the thread and begin the execution of the thread.

d. The main method needs to keep track of the threads and take care of each thread before it can end. The methods of the Thread class you'll need for this are join() and isAlive(). If the thread is dead then execute a join on it in order to properly dispose of that thread. If the thread is still alive then move on to the next thread. The main method keeps doing this check until the last remaining thread has died and been properly disposed.

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
1. The first task in this assignment creates the pid manager whose implementation can simply be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Java language to write exercise 4.20 pid manager Exercise 3.20 An operating system’s pid manager...

    Use Java language to write exercise 4.20 pid manager Exercise 3.20 An operating system’s pid manager is responsible for managing process identifiers. When a process is first created, it is assigned a unique pid 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 must be unique. No two active processes may have the same pid. Use the following constants to identify the...

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • fill in the blanks with java code The following code implements a Java timer that looks...

    fill in the blanks with java code The following code implements a Java timer that looks like the application below. . Once the application starts it keeps counting seconds until it is terminated. Timer (sec): 20 You are given a partial implementation of the program. Fill out the missing code to complete the program. import java.awt.*; import java.awt.event.; import java.util."; import javax.swing. *; import javax.swing. Timer; * This program shows a timer that is updated once per second. public class...

  • This Individual Assignment is a set of three problems. The first is a recursion "warm up"...

    This Individual Assignment is a set of three problems. The first is a recursion "warm up" exercise, and the second two are QuickSort variations. The "warm up" should be implemented as a static method in your main App class and the second two will use additional classes (as instructed below). All three problems should be included in the same NetBeans project (exported to ZIP as usual). ----------------- All of your classes should be properly encapsulated, follow all proper coding conventions...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • This assignment simply requests that you take the quicksort algorithm that you have been working with...

    This assignment simply requests that you take the quicksort algorithm that you have been working with and make it generic. You are to demonstrate the effectiveness of your work by defining the main method that passes a list of Integers, Doubles, and Strings and sorts them all. NOTE: this is the code, please tell me which one of the blocks of code does. import java.util.*; public class Driver{ public static <T extends Comparable<T>> void quickSort(T[] data, int a, int b)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT