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
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