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

Need in java programming And multi threading is not required , it can be done wi

ID: 3815407 • Letter: N

Question

Need in java programming

And multi threading is not required , it can be done with data structurs too

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 id), 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: Process ID I Priority I Burst-length 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 a correct 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 preemptive SJF (ii) 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

Note: I have only implemented non preemptive SJF.

Executable Code:

Scheduling.java

package scheduling;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Scheduling {
public static void main(String[] args) {
ArrayList<Proc> pr = new ArrayList<Proc>();
ArrayList<Proc> pr1 = new ArrayList<Proc>();
int count=0;
Random rn = new Random();
boolean unique=false;
int num=0;
for(int i=0;i<5;i++)
{
unique=false;
Proc p1=new Proc();
while(!unique)
{
num=rn.nextInt(10) + 1;
if(i==0)
break;
for(int j=0;j<i;j++)
{
if(num==pr.get(j).id)
{

  
unique=false;
break;
}
else
unique=true;
}
}
p1.id=num;
p1.burstLength=rn.nextInt(10) + 10;
p1.priority=rn.nextInt(10) + 1;
pr.add(p1);
count++;
}
System.out.println("Process ID | Priority | Burst-length");
for(int i=0;i<count;i++)
{
System.out.println(pr.get(i).id+" "+pr.get(i).priority+" "+pr.get(i).burstLength);
}
  
boolean cont=true;
while(cont)
{
System.out.println("Do you want to add one more process true/false" );
Scanner n = new Scanner(System.in);
cont = n.nextBoolean();
if(cont)
{
  
Proc p1=new Proc();
unique=false;

while(!unique)
{
System.out.println("Enter process id" );
num=n.nextInt();
for(int j=0;j<count;j++)
{
if(num==pr.get(j).id)
{ unique=false;
break;
}
else
unique=true;
}
}
p1.id=num;
System.out.println("Enter process burst length" );
p1.burstLength=n.nextInt();
System.out.println("Enter process priority" );
p1.priority=n.nextInt();
pr.add(p1);
count++;
}
else
break;
  
}
System.out.println("Process ID | Priority | Burst-length");
for(int i=0;i<count;i++)
{
System.out.println(pr.get(i).id+" "+pr.get(i).priority+" "+pr.get(i).burstLength);
}
int max=100;
int ind=0;
int time=0;
while(pr.size()!=0)
{
max=100;
for(int i=0;i<count;i++)
{
if(pr.get(i).priority<max)
{
max=pr.get(i).priority;
ind=i;   
}
else if(pr.get(i).priority==max)
{
if(pr.get(i).burstLength<pr.get(max).burstLength)
{
max=i;
}

}

}
pr.get(ind).TWT=time-0;
time=time+pr.get(ind).burstLength;
pr1.add(pr.get(ind));
pr.remove(ind);
count--;
}
  
System.out.println("Process ID | Priority | Burst-length | Scheduling algorithm | Total waiting time");
for(int i=0;i<pr1.size();i++)
{
System.out.println(pr1.get(i).id+" "+pr1.get(i).priority+" "+pr1.get(i).burstLength+" "+"SJF"+" "+pr1.get(i).TWT);
}
  
float awt;
float total=0;
for(int i=0;i<pr1.size();i++)
{
total=total+pr1.get(i).TWT;
}
awt=total/pr1.size();
System.out.println("The avearge waiting time of SJF is "+ awt);
}
  
}

Proc.java

package scheduling;

public class Proc {
int id;
int burstLength;
int priority;
int TWT;
Proc()
{
  
}
void setId(int Id)
{
id=Id;
}
int getId()
{
return id;
}
void setburstLength(int burstlen)
{
burstLength=burstlen;
}
int getburstLength()
{
return burstLength;
}
void setpriority(int pri)
{
priority=pri;
}
int getpriority()
{
return priority;
}
  
}

Output:

run:
Process ID | Priority | Burst-length
8 9 19
7 1 14
1 3 14
5 10 11
4 6 12
Do you want to add one more process true/false
true
Enter process id
7
Enter process id
2
Enter process burst length
12
Enter process priority
2
Do you want to add one more process true/false
false
Process ID | Priority | Burst-length
8 9 19
7 1 14
1 3 14
5 10 11
4 6 12
2 2 12
Process ID | Priority | Burst-length | Scheduling algorithm | Total waiting time
7 1 14 SJF 0
2 2 12 SJF 14
1 3 14 SJF 26
4 6 12 SJF 40
8 9 19 SJF 52
5 10 11 SJF 71
The avearge waiting time of SJF is 33.833332
BUILD SUCCESSFUL (total time: 38 seconds)

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