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

4. Priority queue You have been asked to test the effectiveness of the following

ID: 3833611 • Letter: 4

Question

4. Priority queue You have been asked to test the effectiveness of the following different scheduling algorithms for different types of queuing applications. First-In-First-out (FIFO) The jobs are processed in the arriving order. The job at the front of the queue is served until it has completed. That job is then removed from the queue. The next job at the front of the queue is served until it has completed and then it is removed from the queue. This process is continued until the queue is empty 2) Shortest Job-First (SJF): The job with the shortest processing time is processed first until it has completed. That job is then removed from the queue. The next job with smallest processing is then served until it has completed and then it is removed from the queue This process is continued until the queue is empty.

Explanation / Answer

package qeffectiveness;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.Random;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.time.*;

class job{
public int jobID;
public int processTime;
public job(int id,int tm){//constructor
jobID=id;
processTime=tm;
}
public Duration processJob(){
System.out.println("Starting Job "+jobID+"...");
Instant start=Instant.now(); //save current time in start
try{
TimeUnit.SECONDS.sleep(processTime);//pause for 'processTime' seconds
}catch(InterruptedException e){}
Instant end=Instant.now();
Duration timetaken=Duration.between(start, end);
System.out.println("Exiting Job "+jobID+"...");
return timetaken; //return time taken in seconds to complete the job
}
}
public class QEffectiveness {
static class PQ implements Comparator<job>{
public int compare(job one, job two){
return two.processTime-one.processTime;
}
}
public static void main(String[] args) {
Queue<job> q=new LinkedList<>(); //create a simple queue of jobs
Random rnd=new Random();
//add 10 jobs to the queue
for(int i=0;i<10;i++){
int pTime=rnd.nextInt((10-9)+1)+1; //generate a random number between 1 & 10 for processtime
job jb=new job(i,pTime); //create a new job with id, value of i and process time pTime
q.add(jb); //add this job to the queue
}
//now create a priority q of the same jobs, lowest process time has the highest priority
PQ pqs=new PQ();
PriorityQueue<job> pq=new PriorityQueue<job>(10,pqs);//create priority q of length 10 using comparator pqs
for(job x:q){//add thejobs in the simple q into this new priority q
pq.offer(x);
}
System.out.println("Processing jobs in the FIFO queue:-");
Instant start=Instant.now(); //save current time in start
for(job x:q){
System.out.println("Time taken for Job "+x.jobID+" is "+ x.processJob());
}
Instant end=Instant.now();
Duration timetakenQ=Duration.between(start, end);
System.out.println("Total time taken for FIFO queue is "+timetakenQ+ " sec.");
  
System.out.println("Processing jobs in the SJF queue:-");
start=Instant.now(); //save current time in start
for(job x:q){
System.out.println("Time taken for Job "+x.jobID+" is "+ x.processJob());
}
end=Instant.now();
Duration timetakenPQ=Duration.between(start, end);
System.out.println("Total time taken for SJF queue is "+timetakenPQ+ " sec.");
  
if(timetakenQ.compareTo(timetakenPQ)<0)
System.out.println("FIFO is more effective.");
else
if(timetakenQ.compareTo(timetakenPQ)>0)
System.out.println("SJF is more effective.");
else
System.out.println("Both FIFO and SJF are equally effective.");
}
}

Output:-

run:
Processing jobs in the FIFO queue:-
Starting Job 0...
Exiting Job 0...
Time taken for Job 0 is PT1.119S
Starting Job 1...
Exiting Job 1...
Time taken for Job 1 is PT2S
Starting Job 2...
Exiting Job 2...
Time taken for Job 2 is PT1.001S
Starting Job 3...
Exiting Job 3...
Time taken for Job 3 is PT1S
Starting Job 4...
Exiting Job 4...
Time taken for Job 4 is PT2.001S
Starting Job 5...
Exiting Job 5...
Time taken for Job 5 is PT2S
Starting Job 6...
Exiting Job 6...
Time taken for Job 6 is PT1.001S
Starting Job 7...
Exiting Job 7...
Time taken for Job 7 is PT1S
Starting Job 8...
Exiting Job 8...
Time taken for Job 8 is PT2.001S
Starting Job 9...
Exiting Job 9...
Time taken for Job 9 is PT2S
Total time taken for FIFO queue is PT16.171S sec.
Processing jobs in the SJF queue:-
Starting Job 0...
Exiting Job 0...
Time taken for Job 0 is PT1S
Starting Job 1...
Exiting Job 1...
Time taken for Job 1 is PT2S
Starting Job 2...
Exiting Job 2...
Time taken for Job 2 is PT1.001S
Starting Job 3...
Exiting Job 3...
Time taken for Job 3 is PT1S
Starting Job 4...
Exiting Job 4...
Time taken for Job 4 is PT2.001S
Starting Job 5...
Exiting Job 5...
Time taken for Job 5 is PT2.001S
Starting Job 6...
Exiting Job 6...
Time taken for Job 6 is PT1.001S
Starting Job 7...
Exiting Job 7...
Time taken for Job 7 is PT1S
Starting Job 8...
Exiting Job 8...
Time taken for Job 8 is PT2.001S
Starting Job 9...
Exiting Job 9...
Time taken for Job 9 is PT2.001S
Total time taken for SJF queue is PT15.007S sec.
SJF is more effective.
BUILD SUCCESSFUL (total time: 45 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