Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose: Write a program simulating a simple CPU scheduler to calculate average

ID: 3813452 • Letter: P

Question

Purpose: Write a program simulating a simple CPU scheduler to calculate average waiting time. Problem: Initial ready queue has 5 processes where each process has the following attributes: an identifier (value between 0 and 10, randomly assigned-however, no two processes can have the same 1 a burst length between 20 100, randomly assigned when it is created, a priority between 1 and 10, randomly assigned when it is created, and low value indicates higher priority. Two or more processes may have same priority rank. Requirement: Display initial snapshot of the system as follows: Burst-length Process ID Priority Allow user of the program to enter attributes of another process (make sure to have proper check for duplicate process ID). If duplicate ID entered, allow user to enter acorrect process ID. Display updated snapshot of the system in the same format as above. Assuming all processes are ready for scheduling, calculate individual waiting time for each process and average waiting time using the following algorithms: (i Non-preemptive SJF, (i) Non-preemptive priority, and (iii) round robin with time quantum 20. Display the following: Process ID I Priority I Burst-length l Scheduling algorithm I Total waiting time Based on your snapshot and calculations, display average waiting time for each algorithm and order the algorithms from lowest to highest average waiting time. Notes: You may work with absolute values ignoring the time unit. That is, a process may have burst length 46-you may ignore whether it is 46 seconds or 46 milliseconds. This program may not even need multithreading. Simple data structures (arrays, lists etc) should be fine.

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.omg.CORBA.INTERNAL;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class Processor {

    private void disp() {
        System.out.println("Process id Priority burst length");
        for (Process p:processList)
             System.out.println(p.processId+" "+p.priority+" "+p.burstLength);
    }
    class Process {
        int processId;
        int priority;
        int burstLength;
    }
  
    ArrayList<Process> processList;

    public Processor() {
        processList = new ArrayList<>();
      
        for (int i = 0; i<5 ; i++){
            Process newProcess = new Process();
            int tempProcessId;
            l1:while(true){
                tempProcessId = (int)Math.round(Math.random()*9999);
                for (Process p:processList)
                    if (p.processId == tempProcessId)
                        continue l1;
                break;
            }
            newProcess.processId = tempProcessId;
            newProcess.priority = (int)Math.round(Math.random()*9)+1;
            newProcess.burstLength = (int)Math.round(Math.random()*50);
            processList.add(newProcess);
        }
        disp();
    }
  
    void addProcess() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tempProcessId, tempPriority,tempBurstLength;
        l1:while(true){
            System.out.println("Enter new process id");
                tempProcessId = Integer.parseInt(br.readLine());
                for (Process p:processList)
                    if (p.processId == tempProcessId)
                        continue l1;
                break;
            }
        do{
            System.out.println("Enter new process priority");
            tempPriority = Integer.parseInt(br.readLine());
        }while (tempPriority<1 || tempPriority>10);
      
            System.out.println("Enter new process burstLength");
            tempBurstLength = Integer.parseInt(br.readLine());
            disp();
    }
  
    void execute(){
        ArrayList<Process> copyList = new ArrayList<>(processList.size());
        Collections.copy(processList, copyList);
        int waitingTime[][] = new int[processList.size()][3];
        System.out.println("Process id priority burstlength algorithm waiting time");
        //For SJF
        Collections.sort(processList, (Process p2, Process p1) -> p1.burstLength - p2.burstLength);
        int wait = 0;
        for (Process p:processList){
            System.out.println(p.processId+" "+p.priority+" "+p.burstLength+" SJF "+wait);
            wait += p.burstLength;
        }
        Collections.sort(processList, (Process p2, Process p1) -> p1.priority - p2.priority);
        for (Process p:processList){
            System.out.println(p.processId+" "+p.priority+" "+p.burstLength+" SJF "+wait);
            wait += p.burstLength;
        }
      
        int waitAr[] = new int[copyList.size()];
        int left = copyList.size();
        Process[] processAr = processList.toArray(new Process[processList.size()]);
        for (int i = 0; left>0; i++){
            if (processAr[i].burstLength > 20){
                processAr[i].burstLength -= 20;
                for (int j=0; j<processAr.length; j++)
                    if (j!=i && processAr[j].burstLength>0)
                        waitAr[j]+=20;
            }
            else{
                left--;
                for (int j=0; j<processAr.length; j++)
                    if (j!=i && processAr[j].burstLength>0)
                        waitAr[j]+=processAr[i].burstLength;
                processAr[i].burstLength = 0;
            }
        }
      
        for (int j=0; j<processAr.length; j++){
            System.out.println(processAr[j].processId+" "+processAr[j].priority+" "+processAr[j].burstLength+" SJF "+waitAr[j]);
        }
      
    }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote