Question

Create a non-preemptive Priority process scheduler in java that considers the arrival time, burst time, and...

Create a non-preemptive Priority process scheduler in java that considers the arrival time, burst time, and priority (1 being highest priority). Output the waiting time, turn around time, average waiting time, and average turn around time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Process {

{arrivalTime}, {burstTime}, {priority}

}

Steps:

1. input process size and then input all the process fields for each process.

2. Sort processes on the basis of arrival time, if that is same consider the priority(consider the highest priority first, 1>5 priority).

3. Compute waiting time for all processes. the waiting time for the ith process = ( summation of all previous burst time ) - (arrival time of ith process)  

4. turn around time = waiting + burst time for all the processes

5. Average waiting time = (total waiting time) / (number of processes)
6. Turn Around waiting time = (total turn around time) / (number of processes)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

public class NonPreemptivePriorityBasedScheduler {

    private static BufferedReader bufferedReader;

    public NonPreemptivePriorityBasedScheduler() {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    }

    public static void main(String[] args) throws Exception {
        NonPreemptivePriorityBasedScheduler scheduler = new NonPreemptivePriorityBasedScheduler();
        scheduler.solve();
        bufferedReader.close();
    }

    private void solve() throws IOException {
        System.out.println("Enter number of processes: ");
        int n = Integer.parseInt(bufferedReader.readLine());
        Process[] processes = new Process[n];
        System.out.println("Enter 3 numbers (space separated): {arrivalTime}, {burstTime}, {priority} for " + n + " lines." );
        for (int i = 0; i < n; i++) {
            String line = bufferedReader.readLine();
            String[] split = line.split(" ");
            assert split.length == 3;
            processes[i] = new Process(i+1, Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
        }
        Arrays.sort(processes, new ProcessComparator());
        int[] waiting = new int[n];
        int[] turnAround = new int[n];
        double averageWaitingTime = 0.0;
        double averageTurnAroundTime = 0.0;
        getWaitingTime(waiting, processes, n);
        getTurnArroundTime(turnAround, waiting, processes, n);
        int[] startTime = new int[n];
        int[] completeTime = new int[n];
        startTime[0]=1;
        completeTime[0]=startTime[0]+turnAround[0];
        // calculating starting and ending time
        for (int i = 1; i < n; i++) {
            startTime[i] = completeTime[i - 1];
            completeTime[i] = startTime[i] + turnAround[i] - waiting[i];
        }
        System.out.println("Process No\tStart Time\tComplete Time\tTurn Around Time\tWaiting Time");

        // display the process details
        for (int i = 0; i < n; i++) {
            averageWaitingTime += waiting[i];
            averageTurnAroundTime += turnAround[i];

            System.out.println("\t"+processes[i].getProcessNo() + "\t\t\t" +
                    startTime[i] + "\t\t\t" + completeTime[i] + "\t\t\t\t" +
                    turnAround[i] + "\t\t\t\t" + waiting[i] );
        }
        System.out.println("Average waiting time is : " + ((Double)averageWaitingTime/n));
        System.out.println("Average turnaround time is : " + ((Double)averageTurnAroundTime/n));
    }

    private void getTurnArroundTime(int[] turnAround,int[] waiting, Process[] processes, int n) {
        // Filling turnaroundtime array
        for (int i = 0; i < n; i++) {
            turnAround[i] = processes[i].getBurstTime() + waiting[i];
        }

    }

    private void getWaitingTime(int[] wt, Process[] processes, int n) {
        // service array to store cumulative burst time
        int[] service = new int[n];
        service[0] = 0;
        wt[0] = 0;

        for (int i = 1; i < n; i++) {
            service[i] = processes[i - 1].getBurstTime() + service[i - 1];
            wt[i] = service[i] - processes[i].getArrivalTime() + 1;
            // if waiting is -ve, make that 0, means no wait
            if (wt[i] < 0) {
                wt[i] = 0;
            }
        }
    }

    private class ProcessComparator implements Comparator<Process> {
        @Override
        public int compare(Process p1, Process p2) {
            if (p1.getArrivalTime() < p2.getArrivalTime()) {
                return (-1);
            } else if (p1.getArrivalTime() == p2.getArrivalTime() && p1.getPriority() < p2.getPriority()) { // reverse priority condition if 1 < 5 as priority
                return (-1);
            } else {
                return (1);
            }
        }
    }

    private class Process {
        private int processNo;
        private int arrivalTime;
        private int burstTime;
        private int priority;

        public Process(int processNo, int arrivalTime, int burstTime, int priority) {
            this.processNo = processNo;
            this.arrivalTime = arrivalTime;
            this.burstTime = burstTime;
            this.priority = priority;
        }

        public int getArrivalTime() {
            return arrivalTime;
        }

        public void setArrivalTime(int arrivalTime) {
            this.arrivalTime = arrivalTime;
        }

        public int getBurstTime() {
            return burstTime;
        }

        public void setBurstTime(int burstTime) {
            this.burstTime = burstTime;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public int getProcessNo() {
            return processNo;
        }

        public void setProcessNo(int processNo) {
            this.processNo = processNo;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Process process = (Process) o;
            return processNo == process.processNo &&
                    arrivalTime == process.arrivalTime &&
                    burstTime == process.burstTime &&
                    priority == process.priority;
        }

        @Override
        public int hashCode() {
            return Objects.hash(processNo, arrivalTime, burstTime, priority);
        }

        @Override
        public String toString() {
            return "Process{" +
                    "processNo=" + processNo +
                    ", arrivalTime=" + arrivalTime +
                    ", burstTime=" + burstTime +
                    ", priority=" + priority +
                    '}';
        }
    }
}

Sample Run:

Enter number of processes:

5
Enter 3 numbers (space separated): {arrivalTime}, {burstTime}, {priority} for 5 lines.
1 3 3
2 5 4
3 1 1
4 7 7
5 4 8
Process No   Start Time   Complete Time   Turn Around Time   Waiting Time
   1 1 4 3 0
   2 4 9 7 2
   3 9 10 7 6
   4 10 17 13 6
   5 17 21 16 12
Average waiting time is : 5.2
Average turnaround time is : 9.2

Add a comment
Know the answer?
Add Answer to:
Create a non-preemptive Priority process scheduler in java that considers the arrival time, burst time, and...
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
  • Implement a program for SJF(non-preemptive) scheduling. Given n processes with their burst times and Arrival Time,...

    Implement a program for SJF(non-preemptive) scheduling. Given n processes with their burst times and Arrival Time, the task is to find average waiting time and average turn around time and completion time using SJF scheduling algorithm. Language in C • Completion Time: Time at which process completes its execution. • Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time • Waiting Time: Time Difference between turn around time and...

  • Write a java program that represents a preemptive low priority scheduler, and calculate the waiting time...

    Write a java program that represents a preemptive low priority scheduler, and calculate the waiting time for each process. Low priority means that the low the number the higher the priority. Also assume that processes can arrive at different times. Hint: Use a priority queue.

  • (Algorithm Computation Question) Given the following processes and associated arrival time and burst time. Process   Arrival...

    (Algorithm Computation Question) Given the following processes and associated arrival time and burst time. Process   Arrival Time     Burst Time P1                0.0                    6 P2                2.0                    5 P3                3.0                    2 P4                4.0                    3 1) Calculate the average waiting time if the CPU scheduling uses FCFS algorithm. 2) Calculate the average waiting time if the CPU scheduling uses Non-Preemptive SJF algorithm.

  • 4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1...

    4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1 Priority P2 P3 P4 P5 For each of the following CPU scheduling algorithms, determine the turnaround and average waiting time for each of the process :- a. Shortest remaining Time First b. Shortest Job First C. Priority Scheduling (Both pre-emptive and non-preemptive) d. Round Robin (quantum is 1 ms)

  • Answer the following using the processes and their corresponding arrival time and burst time. These processes...

    Answer the following using the processes and their corresponding arrival time and burst time. These processes are being scheduled using a preemptive, round-robin scheduling algorithm with a time quantum of 2 units. PID Arrival Time Burst Time P1 0 4 P2 1 5 P3 2 3 P4 3 2 P5 4 6 1. Use a Gantt chart to represent the above processes (5 points) 2. Calculate the average waiting time for each process (5 points) 3. Calculate the completion time...

  • Consider 5 processes whose Arrival time and CPU Burst time is as follows (Process no: Arrival...

    Consider 5 processes whose Arrival time and CPU Burst time is as follows (Process no: Arrival time: Burst time): P1:0,6 P2:2,4 P3:3,2 P4: 5,5 P5: 6,9 Assume that the processes are scheduled using SJF (Non-preemptive) scheduling algorithm. The average waiting and turaround time is O 5.45, 11.60 O 5.40, 10.60 O 6.50, 10.99 O 5.45, 11.60

  • 8. Consider the following 5 processes in the ready queue: Process Burst Priority Arrival time n...

    8. Consider the following 5 processes in the ready queue: Process Burst Priority Arrival time n w P1 P2 w N P3 P P - W WE O P4 N P5 N A O Draw Gantt charts illustrating the execution of these processes for each of the following algorithms: (a) preemptive SJF, (b) RR (with quantum = 1), (C) FCFS, and (d) preemptive priority, and calculate the respective turnaround and waiting times.

  • The following processes are being scheduled using a pre-emptive, priority-based, round-robin scheduling algorithm. Process Burst Time...

    The following processes are being scheduled using a pre-emptive, priority-based, round-robin scheduling algorithm. Process Burst Time Priority Arrival 20 20 0 20 25 45 55 5 5 5 15 Each process is assigned a numerical priority, with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is pre-empted by a higher-priority process,...

  • 2. Consider the following processes with the arrival time, burst time, and priority. Draw Gantt chart...

    2. Consider the following processes with the arrival time, burst time, and priority. Draw Gantt chart for a priority-based preemptive CPU scheduling algorithm (the highest number refers the highest priority, in case of same priority, follow FCFS) (4 points) Priority PIDATBT PI 0 10 P3 10 6 3

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:Processburst TimePriorityP1103P211P323P414P552For each of the scheduling algorithms, FCFS, Shortest-Job-First (SJF, non-preemptive), Priority (smaller priority number implies higher scheduling priority), and RR (quantum = 1) do the following.Draw a Gantt chart to show how these processes would be scheduled.Give the turnaround time (total time from the first arrival into ready state until CPU-burst is completed) of each process.Give the waiting time (total time spent in the Ready state) of each process.Give...

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