Question: For this Java project, we will try to simulate an operating system running many different processes via round-robin scheduling by using a CircularLinkedList. For the

For this Java project, we will try to simulate an operating system running many different processes via round-robin scheduling by using a CircularLinkedList. For the purposes of this project, you are provided a Process class to emulate the behavior of a thread. It has two important methods: run(int) to make the Process "run" for a specified number of iterations, and isFinished(), which is a flag to let the calling method know that its work is complete. A class PrimeFinder, also provided, extends Process. This class finds all prime numbers in a range and prints them to console within its run method. It also announces to the console when it has finished its task (for clarity). Put these files (SinglyLinkedList.java, CircularLinkedList.java, Process.java, and PrimeFinder.java) in the same directory as your solution. Your tasks: 1. All your code should be placed in a file called Solution.java. Your work can be done in the main method or in other methods within Solution.java and called by the main method (which you must provide). 2. Create ten PrimeFinder objects, named Process 1 to Process 10. 3. First PrimeFinder instance should be constructed to find primes between 0 and 1000. 4. Second PrimeFinder should be constructed to find primes between 1001 and 2000, etc. 5. Tenth PrimeFinder should find primes between 9001 and 10000; 6. Add all of these to an instance of CircularLinkedList such that the first element has name Process 1 and the last element has name Process 10. 7. The above can all be done in a single for-loop, but this is not required. 8. Next, use the CircularLinkedList instance to run the PrimeFinders. Each Process should be run for an interval 10-100 (Hint: pass this as a parameter to its run method), before the CircularLinkedList is rotated and the next process is run for the same duration. This process is continued until all the threads are finished (Hint: use the isFinished() method to find out if a PrimeFinder has finished its task). 10.Briefly explain how you would alter the code in your solution to use SinglyLinkedList instead of CircularLinkedList and why using CircularLinkedList is better than SinglyLinkedList for this scenario.

Sample run for the base assignment (this uses a run duration of 25):

Process 1 found a prime: 2

Process 1 found a prime: 3

Process 1 found a prime: 5

Process 1 found a prime: 7

Process 1 found a prime: 11

Process 1 found a prime: 13

Process 1 found a prime: 17

Process 1 found a prime: 19

Process 1 found a prime: 23

Process 2 found a prime: 1009 etc.

Necessary codes:

class Process:

public abstract class Process { //to help identify the thread, similar to Java's Thread class private String name;

//flag to let calling method know this thread has completed it's task public abstract boolean isFinished();

/** a simulation of running a thread for a certain number of operations *intended to simulate running a thread for a given amount of time */ public abstract void run(int runDuration);

public Process(String name) { this.name = name; }

public String getName() { return name; } }

Prime Finder:

public class PrimeFinder extends Process {

private int min; private int max; private int current; private boolean isFinished;

/** * Creates a PrimeFinder object to look for primes between the min and max, inclusive * @param name a String to help identify the Process * @param min the lower bound * @param max the upper bound */ public PrimeFinder(String name, int min, int max) { super(name); this.min = Math.max(min, 2); this.max = max; current = this.min; isFinished = false; }

/** * Creates a PrimeFinder object to look for primes between 0 and max, inclusive. * @param name a String to help identify the Process * @param max the upper bound */ public PrimeFinder(String name, int max) { super(name); min = 2; //smallest prime this.max = max; current = this.min; isFinished = false; }

/** Searches for primes in the interval between current and runDuration. * @param runDuration how many numbers to search */ public synchronized void run(int runDuration) { for (int i = 0; i< runDuration; i++) { boolean isPrime = true; for (int k = 2; k*k <= current; k++) { if (current % k == 0) { isPrime = false; } } if (isPrime) System.out.println(this.getName() + " found a prime: " + current); current++; if(current>max) { System.out.println(this.getName() + " has finished!"); isFinished = true; break; } } }

@Override public boolean isFinished() { return isFinished; }

}

CirculalarLinked List:

public class CircularLinkedList { private static class Node { private E element; private Node next; //constructor public Node(E element, Node next) { this.element = element; this.next = next; } public E getElement() { return element; } //setter for the data public Node getNext() { return next; } //get next Node public void setNext(Node next) { this.next = next; } //change next Node }

private Node tail = null; private int size = 0;

//default constructor, constructs empty list public CircularLinkedList() {}

public int size() { return size; } public boolean isEmpty() { return size==0; }

public E first() { if(isEmpty()) return null; return tail.getNext().getElement(); }

public E last() { if(isEmpty()) return null; return tail.getElement(); }

public void rotate() { if(tail!=null) tail = tail.getNext(); }

public void addFirst(E e) { if(size == 0) { //because we now have only one element tail is both head and tail tail = new Node<>(e, null); tail.setNext(tail); //links to itself } else { Node newest = new Node<>(e, tail.getNext()); tail.setNext(newest); } size++; }

public void addLast(E e) { addFirst(e); tail = tail.getNext(); }

public E removeFirst() { if(isEmpty()) return null; Node head = tail.getNext(); if(head==tail) tail = null; //head==tail means only one element else tail.setNext(head.getNext()); size--; return head.getElement(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!