Highest Response Ratio Next (HRN): This non-preemptive scheduling algorithm assigns a process to the CPU based on the ratio: ( Wi+ Bi ) / Bi Where Wi is the waiting time for process Pi and Bi is the Pi’s burst, a.k.a. service time.
import java.util.*;
public class two {
public static void main(String[] args) {
int aa, b, c,i,ii,iii,iiii;
int no_Process,total_Burst_Time=0;
int totalwaitingTime=0,totalturnaroundtime=0;;
int count=0;
double large,averagewaitingTime=0,averageturnaroundtime=0;
double num;
Scanner vinnu = new Scanner(System.in);
System.out.print("Enter the number of process = ");
no_Process = vinnu.nextInt();
ArrayList<process> process = new ArrayList<>();
for (i = 0; i < no_Process; i++) {
process.add(new process());
}
for(ii=0;ii<no_Process;ii++)
{
total_Burst_Time+=process.get(ii).burstTime;
}
while(count<total_Burst_Time)
{
b=0;
for(aa=0;aa<no_Process;aa++)
{
if(process.get(aa).arrivalTime > count )
{
b++;
}
}
if(b==no_Process)
{
count++;
continue;
}
for(iii=0;iii<no_Process;iii++)
{
if(process.get(iii).arrivalTime > count || process.get(iii).burstTime<=0)
{
process.get(iii).ratio=0;
}
else
{
process.get(iii).waitingTime=(count) - (process.get(iii).arrivalTime);
process.get(iii).ratio=(process.get(iii).burstTime+process.get(iii).waitingTime)/(process.get(iii).burstTime);
}
}
large=0;
for(iiii=0;iiii<no_Process;iiii++)
{
if(process.get(iiii).ratio>large)
{
large=process.get(iiii).ratio;
}
}
for(int iiiii=0;iiiii<no_Process;iiiii++)
{
if(large==process.get(iiiii).ratio)
{
System.out.println("Actual count time = "+count);
process.get(iiiii).waitingTime=count-process.get(iiiii).arrivalTime;
count=count+process.get(iiiii).burstTime;
process.get(iiiii).turnaroundtime=count-process.get(iiiii).arrivalTime;
System.out.println("process "+process.get(iiiii).processName+" has completed its burst time "+process.get(iiiii).burstTime);
process.get(iiiii).burstTime=0;
System.out.println();
System.out.println("Actual count time = "+count);
}
}
}
for(int tt=0;tt<no_Process;tt++)
{
totalwaitingTime=process.get(tt).waitingTime;
totalturnaroundtime=process.get(tt).turnaroundtime;
}
averagewaitingTime=totalwaitingTime/no_Process;
averageturnaroundtime=totalturnaroundtime/no_Process;
System.out.println("Average waiting time = "+averagewaitingTime);
System.out.println("Average turnaround time = "+averageturnaroundtime);
}
// priority function:p(a, r, t) = a/c
static class process {
String processName;
int arrivalTime;
int burstTime;
int waitingTime;
int startTime;
double ratio;
double num;
int turnaroundtime;
Scanner vinnu1 = new Scanner(System.in);
process() {
System.out.print("Enter the name of the process = ");
processName = vinnu1.next();
System.out.print("Enter the Arrival time of the process = ");
arrivalTime = vinnu1.nextInt();
System.out.print("Enter the Burst time of the process = ");
burstTime = vinnu1.nextInt();
System.out.println();
}
}
}
// waitingTime = startTime - arrivalTime
// startTime = Time at which the process started executing
// finishTime = Time at which the process finished executing
Highest Response Ratio Next (HRN): This non-preemptive scheduling algorithm assigns a process to the CPU based on the ratio: ( Wi+ Bi ) / Bi Where Wi is the waiting time for process Pi and Bi is the Pi’s burst, a.k.a. service time.
Reviewed by Unknown
on
03:09
Rating:
No comments: