Please provide me with the solution to this problem, Thank you in advance: The o
ID: 3639457 • Letter: P
Question
Please provide me with the solution to this problem, Thank you in advance:
The operating system (OS) manages the processes (i.e. running programs) using a queue called the ready queue. Processes that are ready to execute are inserted at the tail of the queue to wait for the availability of the CPU. When the CPU becomes available, the process that is stored at the head of the queue is selected for execution. Each node of the queue stores the unique id of each process (i.e. pi d) and a reference to the next node. In addition to the ready queue, a process table is used to maintain state information about each process. In this assignment, you are going to simulate the behavior of a multitasking OS by implementing a ready queue in Java. Each node will store an integer key that will represent the pi d of a process. Initially, the queue will be filled with 100 keys chosen at random using the Java.util. Random class. Every time a key is inserted for the first time in the queue you must record its start time in a log table indexed 0..99. Once all the keys have been inserted into the queue then we begin removing them one by one. If a removed key value is a prime number then it terminates its execution immediately. If the key value is odd (but not prime) then it is inserted a second time in the queue and will terminate only after its second visit on the queue. If the key value is even (but not prime) then it has to visit the queue two more times before terminating. When a key is terminated the end time must also be recorded in its log table entry. The log table must also show other useful state information about each key such as the running status, the number of visits in the queue, etc. When the queue is exhausted, you must compute the following statistics : (i) The average running time for all keys, (ii) the key that has the smallest running time and (iii) the key that has the largest running time. Requirements. You must submit an ADT for your queue data structure showing all the methods along with a brief description of their functionality, You must output the log table along with the required statistics, Your Java code must be well-formatted and documented.Explanation / Answer
import java.util.*; public class Sched extends Thread { private Queue q; private int burst; private Thread current; public Sched() { this.setPriority(6); System.out.println("Set to priority 6: " + this.getName()); burst = 1000; q = new LinkedList(); } public Sched(int b) { burst = b; q = new LinkedList(); } public void addThread(Thread t){ q.offer(t); //add process/ thread to the Q System.out.println("Thread added: " + t.getName()); } public void executeThread(){ try{ System.out.println("Executing: " + current.getName()); Thread.sleep(burst); } catch(InterruptedException ex){}; } public void run(){ while(true){ // current = (Thread)q.poll(); //remove the process/ thread from the Q if(current!=null){ System.out.println("Current thread to execute: " + current.getName()); executeThread(); } else{ if(q.isEmpty()){ System.out.println("Queue is already empty..."); break; } } } } } public class CPUSched { public static void main(String[] args) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Sched CPUScheduler = new Sched(); CPUScheduler.start(); Thread t1 = new Thread("Thread 1"); t1.start(); CPUScheduler.addThread(t1); Thread t2 = new Thread("Thread 2"); t2.start(); CPUScheduler.addThread(t2); Thread t3 = new Thread("Thread 3"); t3.start(); CPUScheduler.addThread(t3); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.