0% found this document useful (0 votes)
14 views2 pages

Non Preemptive Priority

CPU no problem using non preemtive priority scheduling algorithm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Non Preemptive Priority

CPU no problem using non preemtive priority scheduling algorithm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

class Process {
int pid, burstTime, priority, waitingTime, turnAroundTime;

Process(int pid, int burstTime, int priority) {


this.pid = pid;
this.burstTime = burstTime;
this.priority = priority;
}
}

public class NonPreemptivePriority {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
Process[] processes = new Process[n];

for (int i = 0; i < n; i++) {


System.out.print("Enter burst time and priority for process "
+ (i + 1) + ": ");
processes[i] = new Process(i + 1, sc.nextInt(),
sc.nextInt());
}

// Sort by priority (lower number = higher priority)


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].priority > processes[j].priority) {
Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}

int totalWait = 0, totalTurnAround = 0;


// Calculate waiting and turnaround time
processes[0].waitingTime = 0;
for (int i = 1; i < n; i++) {
processes[i].waitingTime = processes[i - 1].waitingTime +
processes[i - 1].burstTime;
totalWait += processes[i].waitingTime;
}
for (int i = 0; i < n; i++) {
processes[i].turnAroundTime = processes[i].waitingTime +
processes[i].burstTime;
totalTurnAround += processes[i].turnAroundTime;
}

// Output
System.out.println("\nProcess\tBurst\tPriority\tWait\
tTurnaround");
for (Process p : processes) {
System.out.println(p.pid + "\t" + p.burstTime + "\t" +
p.priority + "\t\t" + p.waitingTime + "\t" + p.turnAroundTime);
}

System.out.println("\nAverage Waiting Time: " + (totalWait /


(float) n));
System.out.println("Average Turnaround Time: " + (totalTurnAround
/ (float) n));
}
}

output:
java -cp /tmp/veP5CEogC1/NonPreemptivePriority
Enter number of processes: 7
Enter burst time and priority for process 1: 8 3
Enter burst time and priority for process 2: 2 4
Enter burst time and priority for process 3: 4 4
Enter burst time and priority for process 4: 1 5
Enter burst time and priority for process 5: 6 2
Enter burst time and priority for process 6: 5 6
Enter burst time and priority for process 7: 1 1

Process Burst Priority Wait Turnaround


7 1 1 0 1
5 6 2 1 7
1 8 3 7 15
2 2 4 15 17
3 4 4 17 21
4 1 5 21 22
6 5 6 22 27

Average Waiting Time: 11.857142


Average Turnaround Time: 15.714286

=== Code Execution Successful ===

You might also like